简体   繁体   中英

Getting Action event for JButtons

Hi i am trying to get the action event for my jbuttons, but when i try the e.get source method for the second button i get and error saying it cant find the symbol myButton2 which is public. I think that it may be a problem with the e.getSource method but i'm not sure.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.JPanel;

class Keypad {    
    public static void main(String[] args) {
        new MyApplication();
    }
}

class MyApplication extends JFrame implements ActionListener {    
    // CLASS INSTANCES AND OBJECTS    
    public MyApplication() {
        setLayout(null);

        JPanel panel1 = new JPanel();
        add(panel1);

        JTextArea text1 = new JTextArea(2, 12);
        panel1.add(text1);
        panel1.setBounds(0, 130, 370, 40);
        Font textFont1 = new Font("Arial Bold", Font.BOLD, 18);

        JButton myButton = new JButton("1");
        myButton.setBounds(20, 190, 60, 60);

        JButton myButton1 = new JButton("<html><center> 2 <br /> ABC </center> </html>");
        myButton1.setBounds(85, 190, 60, 60);
        myButton1.addActionListener(this);

        JButton myButton2 = new JButton("<html><center> 3 <br /> DEF </center> </html>");
        myButton2.setBounds(150, 190, 60, 60);

        JButton myButton3 = new JButton("<html><center> 4 <br /> GHI </center> </html>");
        myButton3.setBounds(20, 260, 60, 60);

        JButton myButton4 = new JButton("<html><center> 5 <br /> JKL </center> </html>");
        myButton4.setBounds(85, 260, 60, 60);

        JButton myButton5 = new JButton("<html><center> 6 <br /> MNO </center> </html>");
        myButton5.setBounds(150, 260, 60, 60);

        JButton myButton6 = new JButton("<html><center> 7 <br /> PQRS </center> </html>");
        myButton6.setBounds(20, 330, 60, 60);

        JButton myButton7 = new JButton("<html><center> 8 <br /> TUV </center> </html>");
        myButton7.setBounds(85, 330, 60, 60);

        JButton myButton8 = new JButton("<html><center> 9 <br /> WXYZ </center> </html>");
        myButton8.setBounds(150, 330, 60, 60);

        JButton myButton9 = new JButton("*");
        myButton9.setBounds(20, 400, 60, 60);

        JButton myButton10 = new JButton("0");
        myButton10.setBounds(85, 400, 60, 60);

        JButton myButton11 = new JButton("#");
        myButton11.setBounds(150, 400, 60, 60);

        add(myButton);
        add(myButton1);
        add(myButton2);
        add(myButton3);
        add(myButton4);
        add(myButton5);
        add(myButton6);
        add(myButton7);
        add(myButton8);
        add(myButton9);
        add(myButton10);
        add(myButton11);

        setTitle("Keypad");
        setSize(300, 500);
        setLocation(250, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if(e.getSource() == myButton2){            
        // EVENT HANDLING !!!!!
        }
    }
}

And also, would i be able to create a method which act when the button is clicked a number of times ie 2 or 3 times like a keypad.

Thanks

You were declaring the buttons inside the class's constructor , which means that they are not visible outside of it. So you need to declare your buttons as member variables , so that they would be accessible to all of the class's methods:

class myApplication extends JFrame implements ActionListener {
    JButton myButton1;
    JButton myButton2;
    JButton myButton3;
    ...

    myApplication() {
        myButton1 = new JButton("<html><center> 2 <br /> ABC </center> </html>");
        ...
        myButton2 = new JButton("<html><center> 3 <br /> DEF </center> </html>");
        ...
    }

    public void actionPerformed(ActionEvent e) { ... }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM