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

时间:2016-01-16 01:46:29

标签: java swing events actionlistener

好的,所以我有三节课。 1.)程序驱动程序 - 主类2.)TopPanel 3.)框架 我的注销按钮的动作监听器工作正常但我只是想到可能有更好的方法来分离动作监听器的块。因为我是新手,请耐心等待。 分离动作侦听器块的最佳方法是什么?我是否需要每次在课堂上实现Action Listener,或者我可以做同样的事情

这是我的代码。 TopPanel类

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
}
}

我很感激任何解释或例子。

感谢。

1 个答案:

答案 0 :(得分:1)

很多情况归结为背景。

通过包含内部和匿名类,使ActionListener变得更容易,lambda表达式也可以减少很多混乱(但恕我直言可以使其更难以阅读和确定API合同) 。

在你的情况下,当ActionListener的内容只有几行时,所以你所拥有的匿名课程更合适,它是孤立的和上下文的(现在你想象一下)为了看到这几行,我不得不挖出一个单独的课程:P)

更复杂的操作可能需要内部类,以使代码更易于阅读,它仍然是上下文的(因为非静态内部类可以访问外部类的方法和字段),但不会混淆码。

我可能考虑使用外部类的唯一一次是ActionListener的功能是否可以某种方式重复使用,但是,我会使用Actions API,它们是自成一体的工作单位。这里的问题是,你需要通过传递包含你需要使用的方法/字段的对象的引用来为它们提供上下文,此时,你需要仔细决定你的设计,这就是{ {1}}可以真正帮助

因此,根据您当前的功能,您可以创建一个"泛型" "注销"行动,例如......

interfaces

然后你可以使用类似......

的东西
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();
        }
    }

}

就是这样,当点击按钮时,它会弹出问题,如果用户选择 Ok ,关联的窗口将被关闭(如果它是最后一个窗口配置为在退出时关闭,JVM将退出)

相关问题