Handle JButton click event in another class

后端 未结 3 1803
[愿得一人]
[愿得一人] 2021-01-12 10:47

I\'m new to java coming from C# so I\'m not familiar with java best practices.

I have a main class that opens a JFrame to get several input strings from a user. When

3条回答
  •  灰色年华
    2021-01-12 11:31

    First main method in java always must be public static void. Below is example how this can be done.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    /**
     * Main class is frame but also implements ActionListener interface.
     */
    public class Main extends JFrame implements ActionListener {
        private JButton btnStart;
        private static Main frame;
    
        public Main() {
            JPanel panel = new JPanel();
            btnStart = new JButton("Press me");
    
            // Add action listener. Listener could be any class that implements
            // ActionListener
            btnStart.addActionListener(this);
    
            // This means add button btnStart to panel
            panel.add(btnStart);
    
            // This means add panel to frame
            this.add(panel);
        }
    
        // main method in java always must be public, static and void. You forgot to
        // put static.
        public static void main(String[] args) {
            if (args.length == 0) {
                frame = new Main();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            } else
                frame.startProcess(args);
        }
    
        public void startProcess(String[] args) {
            // TODO
        }
    
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // Here you put your code that is executed every time you press button.
            // For example you just want to show message.
            JOptionPane.showMessageDialog(this, "You pressed Button.");
    
        }
    }
    

    But it is much better to have special class. For example:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    
        public class ButtonListener implements ActionListener {
            JFrame parent;
    
            public ButtonListener(JFrame parent) {
                this.parent = parent;
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(parent, "You pressed Button");
            }
    
        }
    

    And in the main class you just add action listener to button:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    /**
     * Main class is frame but also implements ActionListener interface.
     */
    public class Main extends JFrame {
        private JButton btnStart;
        private static Main frame;
    
        public Main() {
            JPanel panel = new JPanel();
            btnStart = new JButton("Press me");
    
    
            ButtonListener listener = new ButtonListener(this);
            // Add action listener. Listener could be any class that implements
            // ActionListener
            btnStart.addActionListener(listener);
    
            // This means add button btnStart to panel
            panel.add(btnStart);
    
            // This means add panel to frame
            this.add(panel);
        }
    
        // main method in java always must be public, static and void. You forgot to
        // put static.
        public static void main(String[] args) {
            if (args.length == 0) {
                frame = new Main();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            } else
                frame.startProcess(args);
        }
    
        public void startProcess(String[] args) {
            // TODO
        }
    }
    

提交回复
热议问题