JButton - 如何连接?

时间:2014-01-13 15:08:46

标签: java swing user-interface jbutton actionlistener

我在网上找到了这个代码并对其进行了修改并停止了工作。我认为它与我添加Jpanel时有关,但我正在做的最适合JPanel。如果语句有效,我该如何使动作中的事件发生?

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;

    import javax.swing.*;

    public class GUI extends JFrame implements ActionListener {

    static JPanel panel = new JPanel(new GridLayout(5, 5, 1, 1));

    public GUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //setSize(100, 100);
        //setLocation(100, 100);



        //Button 1
        JButton button1 = new JButton("1");
        button1.addActionListener(this);
        panel.add(button1);

        //Button 2
        JButton button2 = new JButton("2");
        button2.addActionListener(this);
        panel.add(button2);

        //Button 3
        JButton button3 = new JButton("3");
        button3.addActionListener(this);
        panel.add(button3);

        //Button 2
        JButton button4 = new JButton("4");
        button4.addActionListener(this);
        panel.add(button4);

        //Button 2
        JButton button5 = new JButton("5");
        button5.addActionListener(this);
        panel.add(button5);

        //Button 2
        JButton button6 = new JButton("6");
        button6.addActionListener(this);
        panel.add(button6);

        //Button 2
        JButton button7 = new JButton("7");
        button7.addActionListener(this);
        panel.add(button7);

        //Button 2
        JButton button8 = new JButton("8");
        button8.addActionListener(this);
        panel.add(button8);

        //Button 2
        JButton button9 = new JButton("9");
        button9.addActionListener(this);
        panel.add(button9);



        panel.setVisible(true);
    }

    public static void main(String[] args) {
        new GUI();
        JFrame f = new JFrame("Calc");
        f.setContentPane(panel);
        f.setSize(1000, 1000);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    }

    ArrayList numbers = new ArrayList();
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        if (command.equals("button1")) {
            myMethod();
            numbers.add(1);
            System.out.println("1");
        }
        if (command.equals("button1")) {
            numbers.add(2);
            System.out.println("2");
        }
    }

    public void myMethod() {
        JOptionPane.showMessageDialog(this, "Hello, World!!!!!");
        System.out.println("Hey");
    }
}

2 个答案:

答案 0 :(得分:1)

您需要将 actionPerformed 部分更改为:

@Override
public void actionPerformed(ActionEvent e) {
     String command = e.getActionCommand();

        if (command.equals("1")) {
            myMethod();
            numbers.add(1);
            System.out.println("1");
        }
        if (command.equals("2")) {
            numbers.add(2);
            System.out.println("2");
        }

}

这里,当单击一个按钮时,你的e.getActionCommand()将给出构造函数字符串。即“1”,“2”,“3”等等

reference

答案 1 :(得分:1)

我在代码中添加了注释,但您应该首先阅读官方教程。 How to use Buttons

    public class GUI /*extends JFrame implements ActionListener*/ {
         //don't extend JFrame is you don't have too neither implement ActionListener in top-container classes that breaks single responsability principle      
    private JPanel panel = new JPanel(new GridLayout(5, 5, 1, 1)); // why static??           

    public JPanel getPanel(){
        return panel;
    }

    public GUI() {            
         //i use anonymous classes for this, then you don't have to use if-else        

        //Button 1
        JButton button1 = new JButton("1");
        button1.addActionListener(new ActionListener(){
           @Override
           public void actionPerformed(ActionEvent evt){
               myMethod();
               numbers.add(1);
               System.out.println("1");
           }
        });
        panel.add(button1);

        //Button 2
        JButton button2 = new JButton("2");
        button2.addActionListener(new ActionListener(){
           @Override
           public void actionPerformed(ActionEvent evt){
              //put here logic for button2
           }
        });
        panel.add(button2);            
        //and goo on with other buttons    
       //panel.setVisible(true); you don't need to call this!!       
    }

   /**
     * Create the GUI and show it.  For thread safety, 
     * this method should be invoked from the 
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {     
        //Create and set up the window.
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GUI gui = new GUI();
        frame.add(gui.getPanel());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(); 
            }
        });
    }

    private List<Integer> numbers = new ArrayList<>();//use generics! 


    public void myMethod() {
        JOptionPane.showMessageDialog(this, "Hello, World!!!!!");
        System.out.println("Hey");
    }
}