Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将事件侦听器放置在何处。?_Java_User Interface_Event Handling_Subclass - Fatal编程技术网

Java 将事件侦听器放置在何处。?

Java 将事件侦听器放置在何处。?,java,user-interface,event-handling,subclass,Java,User Interface,Event Handling,Subclass,哪一个更好?为什么? 在我未受过教育的观点中,最好将它们放在单独的文件中,因为如果你有,比如说,10个按钮,5个组合框和一两个列表,将所有这些类放在一个文件中会变得混乱。我这样想对吗?为什么你会选择一个而不是另一个 SimpleGUI.java: public class simpleGUI extends JFrame { public JButton button; public JLabel label; public simpleGUI() { Container contentPan

哪一个更好?为什么? 在我未受过教育的观点中,最好将它们放在单独的文件中,因为如果你有,比如说,10个按钮,5个组合框和一两个列表,将所有这些类放在一个文件中会变得混乱。我这样想对吗?为什么你会选择一个而不是另一个

SimpleGUI.java:

public class simpleGUI extends JFrame {

public JButton button;
public JLabel label;
public simpleGUI() {
Container contentPane = getContentPane();
JPanel panel = new JPanel();
label = new JLabel("123abc");
button = new JButton("click me");

    simpleEventListener c = new simpleEventListener();
    c.setParams(label);
    button.addActionListener(c);

    panel.add(button);
    panel.add(label);

    contentPane.add(panel);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,300);
    setTitle("simpleGUI");
    setVisible(true);
}
public static void main(String[]args) {
    JFrame frame = new simpleGUI();
}
}
simpleEventListener.java:

 public class simpleEventListener implements ActionListener {
        private JLabel label;
        public void actionPerformed(ActionEvent e) {
            label.setText("Hello World!");
        }
        public void setParams(JLabel label) {
            this.label = label;
        }
    }
public class simpleGUI extends JFrame {
    public JButton button;
    public JLabel label;
    public simpleGUI() {
        Container contentPane = getContentPane();
        JPanel panel = new JPanel();
        label = new JLabel("123abc");
        button = new JButton("click me");

        simpleEventListener c = new simpleEventListener();
        button.addActionListener(c);

        panel.add(button);
        panel.add(label);

        contentPane.add(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,300);
        setTitle("simpleGUI");
        setVisible(true);
    }
    public class simpleEventListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            label.setText("Hello World!");
        }
    }
    public static void main(String[]args) {
        JFrame frame = new simpleGUI();
    }
}
或:

 public class simpleEventListener implements ActionListener {
        private JLabel label;
        public void actionPerformed(ActionEvent e) {
            label.setText("Hello World!");
        }
        public void setParams(JLabel label) {
            this.label = label;
        }
    }
public class simpleGUI extends JFrame {
    public JButton button;
    public JLabel label;
    public simpleGUI() {
        Container contentPane = getContentPane();
        JPanel panel = new JPanel();
        label = new JLabel("123abc");
        button = new JButton("click me");

        simpleEventListener c = new simpleEventListener();
        button.addActionListener(c);

        panel.add(button);
        panel.add(label);

        contentPane.add(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,300);
        setTitle("simpleGUI");
        setVisible(true);
    }
    public class simpleEventListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            label.setText("Hello World!");
        }
    }
    public static void main(String[]args) {
        JFrame frame = new simpleGUI();
    }
}
最好将它们放在单独的文件中,因为如果有10个按钮、5个组合框和一两个将所有这些类都放在一个文件中的列表,就会变得很混乱。我这样想对吗

不,没什么乱七八糟的。 包含类的文件将被结构化, 其中有一个主类和几个其他类, 除了类之外,其他任何地方都无法访问

为什么你会选择一个而不是另一个

封装。 如果侦听器不用于项目中的任何其他类, 那就没有必要暴露它们了。事实上,暴露它们(让它们可见)可能会很混乱。 使用主类的其他代码也将看到侦听器类, 即使他们不能用它们做任何事

即使您将类保持为内部类,例如像您所做的那样
simpleEventListener
, 您应该将其设置为
私有
,而不是
公共
。 项目中的其他类不需要知道它。 封装与信息隐藏密切相关。 它保持你的界面干净