java-单击按钮关闭窗口

你好,

我正在使用Java Swing,并且想在单击按钮时关闭一个窗口.我不知道使用动作侦听器作为执行此操作的最佳方法,但是目前我正在编译错误,因此它一定是不正确的.

这是我的代码:

    public class assignment2
    {

        public static void main(String[] args){
            MyFrame f = new MyFrame(); //open the inital gui interface
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true); //set it visibile
        }
    }

    //this is the initial gui screen, presenting user with options for which action they would like to take
    //al actions for the gui are commenced here
    class MyFrame extends JFrame{

        public MyFrame(){

            buttonPanel1 p = new buttonPanel1(); // add the buttons for this frame
            add(p);

            setSize(800,600);
            setTitle("Travel Console");
            setLocationRelativeTo(null);
        }
    }

    class buttonPanel1 extends JPanel{
        public buttonPanel1(){
            //create buttons
            JButton addItem = new JButton("Add an Item");
            JButton deleteItem = new JButton("Delete an item");
            JButton listItem = new JButton("List items");
            JButton editItem = new JButton("Edit an item");
            JButton bookFlight = new JButton("Book a flight");
            JButton save = new JButton("Save data");
            JButton load = new JButton("Load data");
            JButton exit = new JButton("Exit");

            //set layout manager for button panel
            setLayout(new GridLayout(8,1,1,5));

            //create buttons
            add(addItem);
            add(deleteItem);
            add(listItem);
            add(editItem);
            add(bookFlight);
            add(load);
            add(save);
            add(exit);

            addItemListener addList = new addItemListener();
            addItem.addActionListener(addList);
            exitListener exitList = new exitListener();
            exit.addActionListener(exitList);
        }

    }

    //listener classes for the inital gui page. each button has its own actionlistener which launches the selected option
    class addItemListener implements ActionListener{
        public void actionPerformed(ActionEvent event){ //launch add item
            addItemFrame addItem = new addItemFrame();
            addItem.setDefaultCloseOperation(addItemFrame.DISPOSE_ON_CLOSE);
            addItem.setVisible(true);
        }

    }
class addItemFrame extends JFrame{
    public addItemFrame(){
        addItemButtonPanel b = new addItemButtonPanel(); // add the buttons for this frame
        add(b);

        setSize(800,500);
        setTitle("Add an Item");
        setLocationRelativeTo(null);
    }
}
//part of addItemFrame class
class addItemButtonPanel extends JPanel{
    public addItemButtonPanel(){
        JLabel selectItem = new JLabel("Select which item you would like to add:");
        JButton newCustomer = new JButton("Customer");
        JButton newflight = new JButton("Flight");
        JButton newMovie = new JButton("Movie");
        JButton goBack = new JButton("Return to main menu");

        setLayout(new GridLayout(5,1,1,5));

        add(selectItem);
        add(newCustomer);
        add(newflight);
        add(newMovie);
        add(goBack);

        goBackListener gbList = new goBackListener();
        goBack.addActionListener(gbList);
    }
}

//listener classes for the addItemFrame
class goBackListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        addItemFrame.dispose();
    }

}

我遇到的问题是最后一个列出的goBackListener类,它实际上仅关闭当前窗口,因此再次显示主菜单屏幕.我需要对在addItemListener类中创建的addItemFrame的静态引用.但是将其更改为静态是无效的修饰符吗?

我该如何解决?

解决方法:

尝试这个

//listener classes for the addItemFrame 
class goBackListener implements ActionListener{
    private addItemFrame frame;
    public goBackListener(addItemFrame frame){
        this.frame= frame;
    }
    public void actionPerformed(ActionEvent event){
        frame.dispose();
    }
}

并将addItemFrame的实例发送到其构造函数

上一篇:Java.awt:未显示第二个TextArea


下一篇:在Java中使用CardLayout