你如何重新启动gui? (JAVA)

时间:2015-07-12 17:29:25

标签: java swing jframe jpanel

例如,我希望程序在有人按下&#34之后不停地运行多次;请点击完成流程"。但是,当我再次运行GUI方法时,只显示第一个面板。不确定为什么会这样。

  import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;


public class Hotel {
    public static JFrame frame;
    public static JPanel pan;
    public static JPanel endPanel;
    public static JPanel buttonPanel;
    public static GridBagConstraints c;
    public static GridBagConstraints f;
    public static JButton econ;
    public static Boolean openA = true;


    public Hotel(){

        Frame();
        GUI();

    }

    public static void Frame(){
        frame = new JFrame ("Kiosk");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(600,400);

    }

    public static void GUI (){

        pan =  new JPanel(new GridBagLayout());
        c = new GridBagConstraints();
        frame.add(pan);


        JLabel l2 = new JLabel("Please fill out your name and room number");
        l2.setVisible(false);
        pan.add(l2);

        buttonPanel = new JPanel(new GridBagLayout());
        GridBagConstraints GB = new GridBagConstraints();


        JButton b1 = new JButton("Check in?");
        c.gridx=0;
        c.gridy=0;
        c.insets = new Insets(10,10,10,10);
        buttonPanel.add(b1, c);

        JButton b2 = new JButton("Check out?");
        c.gridx=1;
        c.gridy=0;
        c.insets = new Insets(10,10,10,10);
        buttonPanel.add(b2, c);

        frame.add(buttonPanel);

        b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    buttonPanel.setVisible(false);
                    Checkin();
                }
            });      


        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                l2.setVisible(true);
                b1.setVisible(false);
                b2.setVisible(false);
            }
        });      


    }

    public static void Checkin(){

        JLabel name = new JLabel("What is your name?");
        c.gridx=0;
        c.gridy=0;
        pan.add(name,c);

        JLabel number = new JLabel("How many people in your party?");
        c.gridx=0;
        c.gridy=3;
        pan.add(number,c);

        JTextField namefield = new JTextField(20);
        c.insets = new Insets(10,10,10,10);
        c.gridx=1;
        c.gridy=0;
        pan.add(namefield,c);

        JTextField numberfield = new JTextField(2);
        c.insets = new Insets(10,10,10,10);
        c.gridx=1;
        c.gridy=3;
        pan.add(numberfield,c);

        JButton confirm = new JButton("Confirm");
        c.insets = new Insets(10,10,10,10);
        c.gridx=1;
        c.gridy=4;
        pan.add(confirm,c);

        JPanel errorPanel = new JPanel(new GridBagLayout());

        GridBagConstraints d = new GridBagConstraints();

        errorPanel.setVisible(false);
        frame.add(errorPanel);

        JButton econ = new JButton("Confirm");
        d.insets = new Insets(10,10,10,10);
        d.gridx=1;
        d.gridy=4;
        errorPanel.add(econ,d);

        JLabel error = new JLabel("Sorry you must fill in all the fields");
        d.insets = new Insets(10,10,10,10);
        d.gridx=1;
        d.gridy=0;
        errorPanel.add(error,d);

        confirm.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if((namefield.getText().equals("")) ||
        (numberfield.getText().equals(""))){
                    pan.setVisible(false);
                    errorPanel.setVisible(true);
                    econ.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            errorPanel.setVisible(false);
                            pan.setVisible(true);
                        }
                    });

                }
                else{

                    endPanel = new JPanel(new GridBagLayout());
                    f = new GridBagConstraints();
                    pan.setVisible(false);
                    frame.add(endPanel);

                    String hey = namefield.getText();
                    Memory(hey); 

                    int people =0;
                    try {
                        people = Integer.parseInt(numberfield.getText());
                    }
                    catch(NumberFormatException ex)
                    {
                        System.out.println("Exception : "+ex);
                    }
                    Rooms(people);

                }   
            }
        });

    }

    public static void Memory(String h){
        ArrayList<String> Guest = new ArrayList<String>();
        for(int x=0;x<Guest.size();x++){
            if(Guest.get(x).equals(h)){
                JLabel duplicate = new JLabel("Duplicate Name - Please refer
       to the manager " + h);

                JPanel dupPanel = new JPanel();

                pan.setVisible(false);
                dupPanel.setVisible(true);
                frame.add(dupPanel);
                econ.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        dupPanel.setVisible(false);
                        pan.setVisible(true);
                    }
                });


            }
            else{
                JLabel nameLab = new JLabel("Have a nice day " + h);
                f.insets = new Insets(10,10,10,10);
                f.gridx = 0;
                f.gridy=0;
                endPanel.add(nameLab);
            }

        Guest.add(h);
        }
    }

    public static void Rooms(Integer n){

     JLabel roomLab = new JLabel("Sorry there are no rooms with that
        occupany available");
        f.insets = new Insets(10,10,10,10);
        f.gridx = 0;
        f.gridy=0;
        endPanel.add(roomLab,f);

        JButton restart = new JButton("Please hit to finish process");
        f.insets = new Insets(10,10,10,10);
        f.gridx = 0;
        f.gridy=4;
        endPanel.add(restart,f);

        int x=0;

        if(openA == true && n<2){
            openA = false;
            x=1;
        }

        switch(x){
            case 1 : roomLab.setText("You have been assigned room A"); 
        }

        restart.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                endPanel.setVisible(false);
                GUI();
            }
        });


    }

    public static void main(String[] args) {

        new Hotel();

    }

}

1 个答案:

答案 0 :(得分:-1)

如果要干净地重新启动GUI,并确保新GUI与前一个GUI完全独立,最好的办法是丢弃前一个GUI,并创建一个全新的实例。

您的案例中最大的问题是您的所有字段和方法都是静态的。这是错的。你通常应该有:

public class Hotel extends JFrame {

    public JPanel pan;
    ...

    public Hotel() {
       super("Kiosk");
       createGUI();
       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       setVisible(true);
       setSize(600,400);
    }

    public void createGUI(){

       pan =  new JPanel(new GridBagLayout());
       c = new GridBagConstraints();
       add(pan);
       ...

如果您想重新启动GUI,那么非常简单:

    restart.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            dispose();
            new Hotel();
        }
    });

如果您只想重置框架中的特定面板,可以将其从容器中移除,然后添加新实例:

somepanel.remove(somesubpanel);
somepanel.add(createSubpanel());