简体   繁体   中英

close window on button click

Hello,

I am using Java Swing and I want to close a window on a button click. I don't know using an actionlistener as the best way to do this but currently I am having compilation errors with it so its must be incorrect.

Here my code:

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

}

The problem I am having is with the last class listed goBackListener , which effectively just closes the current window so the main menu screen is displayed again. I need a static reference to addItemFrame created in the addItemListener class. But changing it to static is an invalid modifier?

How can I fix it?

try this

//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();
    }
}

and send an instance of addItemFrame to it's constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM