关闭按钮单击窗口

时间:2012-10-02 10:46:48

标签: java swing awt

您好,

我正在使用Java Swing,我想在按钮点击时关闭一个窗口。我不知道使用actionlistener作为执行此操作的最佳方法,但目前我遇到了编译错误,所以它必须是错误的。

这是我的代码:

    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,它实际上只关闭了当前窗口,因此再次显示主菜单屏幕。我需要对addItemFrame类中创建的addItemListener的静态引用。但是将它更改为静态是一个无效的修饰符?

我该如何解决?

1 个答案:

答案 0 :(得分:4)

试试这个

//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的实例发送给它的构造函数