Results 1 to 3 of 3
  1. #1
    jeffrey is offline Member
    Join Date
    Jul 2009
    Rep Power

    Default Action Listener? how to use this?

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;

    import javax.swing.*;
    import javax.swing.JOptionPane;
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    import java.lang.*;

    public class MinesweeperMenu extends JPanel implements ActionListener {
    protected static JButton jbnLeft, jbnMiddle, jbnRight;

    public MinesweeperMenu() {

    jbnLeft = new JButton("Help");
    //jbnLeft.setVerticalTextPosition(AbstractButton.CEN TER);
    //jbnLeft.setHorizontalTextPosition(AbstractButton.L EADING);
    jbnLeft.setMnemonic(KeyEvent.VK_H); // Alt-H clicks the button
    jbnLeft.setToolTipText("For more information"); //Adding Tool tips

    jbnMiddle = new JButton("Play");
    //jbnMiddle.setVerticalTextPosition(AbstractButton.B OTTOM);
    //jbnMiddle.setHorizontalTextPosition(AbstractButton .CENTER);
    jbnMiddle.setMnemonic(KeyEvent.VK_P); // Alt-P clicks the button
    jbnMiddle.setToolTipText("To start the game.");

    jbnRight = new JButton("High Scores");
    jbnRight.setMnemonic(KeyEvent.VK_S); // Alt-S clicks the button
    jbnRight.setToolTipText("To view the high scores.");

    //Listen for actions on Left and Right Buttons

    //Add Components to the frame, using the default FlowLayout.

    public void actionPerformed(ActionEvent e) {
    //show the help menu.
    //if ("Helps".equals(e.getActionCommand()))
    JOptionPane.showMessageDialog(null,"Minesweeper is a mind game based on calculations and some luck.");
    JOptionPane.showMessageDialog(null,"Press Play to start the game.");
    JOptionPane.showMessageDialog(null,"When playing the game, you click on a box which you feel is not a bomb. then by the numbers that are opened you can guess how many bombs are there in its 8 sorrounding boxes wether it is opened or not.");


    //directs to the game
    //if ("Starts".equals(e.getActionCommand()))
    MinesSweeper11 mines = new MinesSweeper11();
    mines.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    mines.setVisible ( true ) ;

    //directs to the high score.
    //if ("Score".equals(e.getActionCommand()))


    public static void main(String[] args) {

    //Create and set up the frame.
    JFrame frame = new JFrame("Menu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    //Create and set up the content pane.
    JButtonDemo buttonContentPane = new JButtonDemo();

    //Display the window.

    /*ImageIcon imageBack = new ImageIcon("Bomb.jpg");
    JLabel background = new JLabel(imageBack);
    background.setBounds(0, 0, imageBack.getIconWidth(), imageBack.getIconHeight());*/

    this is my code.
    the thing is.
    i dont know how to use the ActionEvent. please someone help me here?

    say when i run this.
    and clicked on the play it will go to the main and start the game. but i can handle that my only problem is that how will i do that when it clicks the button it does this etc...

    please help me here.

    and in addition anyone knows how to add background to this?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Rep Power


    Just start the game in the actionPerformed method when they have clicked "Play".

  3. #3
    suhassr is offline Member
    Join Date
    Oct 2009
    Rep Power


    As you can see this line -
    public void actionPerformed(ActionEvent e)

    This function takes an ActionEvent as an argument, which is like a placeholder for an Event.
    This ActionEvent triggers the code that is present inside the actionPerformed()
    function automatically when the button is clicked

    To add background colour use the setBackground() function on the JFrame you have created.
    frame.setBackground(Color.<your colour>);

Similar Threads

  1. Listener for action "team->commit"
    By feva in forum Eclipse
    Replies: 0
    Last Post: 07-27-2009, 07:40 PM
  2. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:14 AM
  3. [SOLVED] action listener and Jtextfield
    By tOpach in forum AWT / Swing
    Replies: 4
    Last Post: 12-16-2008, 01:02 PM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  5. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts