Java 什么是包含Action Listner块的适当位置

Java 什么是包含Action Listner块的适当位置,java,swing,events,actionlistener,Java,Swing,Events,Actionlistener,好的,我有三节课。 1.)程序驱动程序主类2.)TopPanel 3.)帧 我的Action Listener to logout按钮工作正常,但我只是想也许有更好的方法为Action Listener分隔块。请容忍我,因为我是新手。 分离动作侦听器块的最佳方法是什么?我是否每次都需要在类上实现Action Listener,或者我可以做与这里相同的事情 这是我的密码。 托帕内尔级 public class TopPanel extends JPanel{ //DECLARATION JBut

好的,我有三节课。 1.)程序驱动程序主类2.)TopPanel 3.)帧 我的Action Listener to logout按钮工作正常,但我只是想也许有更好的方法为Action Listener分隔块。请容忍我,因为我是新手。 分离动作侦听器块的最佳方法是什么?我是否每次都需要在类上实现Action Listener,或者我可以做与这里相同的事情

这是我的密码。 托帕内尔级

public class TopPanel extends JPanel{
//DECLARATION

JButton logOutButton = new JButton("Logout");
TopTabbedPane topTabbedPane = new TopTabbedPane();
private final Border myLineBorder = BorderFactory.createLineBorder(Color.BLACK, 2);

//CONSTRUCTOR    
public TopPanel(){
    setPanelInitialProperties();
    addComponents();

}

//METHODS
private void setPanelInitialProperties(){
    setLayout(new GridBagLayout());
    setBorder(myLineBorder); //sets a Line Border for this panel
    //setBackground(Color.red);
}

private void addComponents(){
    GridBagConstraints topTabbedPaneGBC = new GridBagConstraints();
    GridBagConstraints logOutButtonGBC = new GridBagConstraints();

    topTabbedPaneGBC.gridx = 0;
    topTabbedPaneGBC.gridy = 1;
    topTabbedPaneGBC.anchor = GridBagConstraints.CENTER;
    this.add(topTabbedPane,topTabbedPaneGBC); //adds TabbedPane holding Home, Administration... to this Top Panel

     logOutButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            int logOutChoice = JOptionPane.showConfirmDialog(null, "Do you want to logout?");
            if(logOutChoice == 0){
                System.exit(0);
            }
        }
    });

    logOutButtonGBC.gridx = 0;
    logOutButtonGBC.gridy = 0;
    logOutButtonGBC.anchor = GridBagConstraints.FIRST_LINE_END;
    this.add(logOutButton,logOutButtonGBC);
}


}
这是框架类的

public class TopFrame extends JFrame {
//DECLARATION
TopPanel topPanel = new TopPanel(); //create an object 

//CONSTRUCTOR 1:
public TopFrame(){
    setFrameInitialProperties();
    addComponentsToPane();
}

//METHOD 1: 
private void setFrameInitialProperties(){
    this.setLayout(new BorderLayout());
    this.setResizable(true);
    this.setPreferredSize(new Dimension(1300,700)); //set it's dimensions or it's size
    this.setVisible(true); //sets it's initial visibility to true so it shows on the screen when objects are created from this class
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Enrollment System");

    //always put pack() first before setLocationRelativeTo(null)
    this.pack();
    this.setLocationRelativeTo(null);
}
//METHOD 2:
private void addComponentsToPane(){
    Container myContainer = this.getContentPane(); //stores the Frame to a Container we named myContainer
        myContainer.add(topPanel); //adds one panel which we named topPanel
}
}
如果有任何解释或例子,我将不胜感激


谢谢

很多问题都归结于上下文

随着内部类和匿名类的加入,使得
ActionListener
s变得更加简单,lambda表达式也可以减少大量的混乱(但IMHO会使读取和确定API契约变得更加困难)

在您的例子中,当
ActionListener
的内容只有几行长时,您可以使用一个匿名类,它是孤立的和上下文相关的(现在假设您必须挖掘一个单独的类才能看到这几行:p)

更大更复杂的操作可能需要一个内部类,为了使代码更易于阅读,它保持上下文(因为非静态内部类可以访问外部类的方法和字段),但不会使代码混乱

<>我唯一可以考虑使用外部类的是,如果<代码> ActuistListabue/Cuth>的功能在某种程度上是可重用的,那么,我会使用那些包含自包含的工作单元。这里的问题是,您需要通过传递包含您需要使用的方法/字段的对象的引用来为它们提供上下文,此时,您需要对您的设计做出仔细的决定,而这正是
接口
真正有帮助的地方

因此,基于您当前的功能,您可以创建一个“通用”“注销”操作,例如

public class LogoutAction extends AbstractAction {

    private LogoutAction parent;

    public CloseAction(String name, Component parent) {
        super(name);
        this.parent = parent;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int logOutChoice = JOptionPane.showConfirmDialog(parent, "Do you want to logout?");
        if (logOutChoice == JOptionPane.OK_OPTION) {
            SwingUtilities.windowForComponent(parent).dispose();
        }
    }

}
然后你可以用它,比如

public class TopPanel extends JPanel{

    private JButton logOutButton;

    //...
    private void addComponents() {
        //...
        logOutButton = new JButton(new LogoutAction(this, "Logout"));
        //...
也就是说,当点击按钮时,它将弹出问题,如果用户选择Ok,关联的窗口将关闭(如果它是最后一个配置为退出时关闭的窗口,JVM将退出)