Java GUI编程关闭当前帧

时间:2015-04-19 02:55:16

标签: java jframe

我是Java编程新手,现在我必须处理GUI编程。我有这个简单的程序,管理员现在创建播放器。我有一个用于显示菜单的视图类,因此当用户单击创建播放器时,将显示该界面。

然后在该菜单中我有一个actionListener,它转到另一个方法并显示弹出菜单,然后显示播放器已成功创建。现在的问题是如何重新显示主菜单,而不是创建播放器菜单。

管理员可以使用一些功能来处理,但现在我只需完成第一个功能,其余功能将类似。当点击主菜单(创建播放器按钮)时,它将转到另一个函数(JFrame)以请求输入,再次从文本字段转到另一个函数getText并将其保存到文件

我的代码为tpublic void show(){

    JFrame frame = new JFrame("Admin");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainPanel = new JPanel();
    mainPanel.setPreferredSize(new Dimension(300,300));
    panel1 = new JPanel(new GridLayout(6,1));


    text = new JLabel("Admin Main Menu");
    text.setFont(new Font("Lucida",Font.PLAIN,24));
    //set panel layout (rows,cols,hgap,vgap)
    panel1.setLayout(new GridLayout(0,1,10,10));

    button1 = new JButton("Create a player");
    button2 = new JButton("Delete a player");
    button3 = new JButton("Top up Player's Chips");
    button4 = new JButton("Reset Player's password");
    button5 = new JButton("Change admin's password");
    button6 = new JButton("Logout");

    mainPanel.add(text);
    panel1.add(button1);
    panel1.add(button2);
    panel1.add(button3);
    panel1.add(button4);
    panel1.add(button5);
    panel1.add(button6);
    mainPanel.add(panel1);

    frame.add(mainPanel);
    frame.pack();
    frame.setVisible(true);

    button1.addActionListener(new createPlayerListener());

button1将创建播放器菜单

private class createPlayerListener implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent event) 
    {
        JFrame frame1 = new JFrame("Admin");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        text = new JLabel("Create a Player");
        text.setFont(new Font("Lucida",Font.PLAIN,24));

        mainPanel = new JPanel(new GridLayout(3,1));
        mainPanel.setPreferredSize(new Dimension(500,400));
        panel1 = new JPanel();
        //panel1.setPreferredSize(new Dimension(300,200));
        panel1.setLayout(new GridLayout(4,2,10,10));
        name = new JLabel("Enter new player name:");
        nameTextfield = new JTextField();
        pw = new JLabel("Enter new player password:");
        pwTextfield = new JTextField();
        chip = new JLabel("Enter new player chips:");
        chipTextfield = new JTextField();
        button1 = new JButton("Create Player");

        mainPanel.add(text);
        panel1.add(name);
        panel1.add(nameTextfield);
        panel1.add(pw);
        panel1.add(pwTextfield);
        panel1.add(chip);
        panel1.add(chipTextfield);
        mainPanel.add(panel1);
        mainPanel.add(button1);

        frame1.add(mainPanel);
        frame1.pack();
        frame1.setVisible(true);

        button1.addActionListener(new playerListener());

    }
}


private class playerListener implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent event) 
    {
        String name = nameTextfield.getText();
        String pw = pwTextfield.getText();
        String chip = chipTextfield.getText();
        int chips = Integer.parseInt(chip);
        //System.out.println(text);
        controller.createPlayer(name, pw, chips);

        //pop up window player created
        JFrame frame2 = new JFrame();
        JOptionPane.showMessageDialog(frame2, "Player Created Successfully!");




    }
}

1 个答案:

答案 0 :(得分:1)

你的代码有两个问题,两次创建mainPanel,你可以这样做,如果你在本地定义它不是全局的,否则将名称更改为mainPanel1和mainPanel2,第二个问题是你正在调用frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);关闭frame1后关闭应用程序。我尝试了这个代码并且它有效,看看我对它做了哪些更改,

JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300,300));
JPanel panel1 = new JPanel(new GridLayout(6,1));


JLabel text = new JLabel("Admin Main Menu");
text.setFont(new Font("Lucida",Font.PLAIN,24));
//set panel layout (rows,cols,hgap,vgap)
panel1.setLayout(new GridLayout(0,1,10,10));

JButton button1 = new JButton("Create a player");
JButton button2 = new JButton("Delete a player");
JButton button3 = new JButton("Top up Player's Chips");
JButton button4 = new JButton("Reset Player's password");
JButton button5 = new JButton("Change admin's password");
JButton button6 = new JButton("Logout");

mainPanel.add(text);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
mainPanel.add(panel1);

add(mainPanel, BorderLayout.CENTER);

button1.addActionListener(new createPlayerListener());

这是你修改的createPlayerListner,playerListner保持不变,

private class createPlayerListener implements ActionListener
   {

    @Override
    public void actionPerformed(ActionEvent event) 
    {
        JFrame frame1 = new JFrame("Admin");
        JLabel text = new JLabel("Create a Player");
        text.setFont(new Font("Lucida",Font.PLAIN,24));

        JPanel mainPanel = new JPanel(new GridLayout(3,1));
        mainPanel.setPreferredSize(new Dimension(500,400));
        JPanel panel1 = new JPanel();
        //panel1.setPreferredSize(new Dimension(300,200));
        panel1.setLayout(new GridLayout(4,2,10,10));
        JLabel name = new JLabel("Enter new player name:");
        nameTextfield = new JTextField();
        JLabel pw = new JLabel("Enter new player password:");
        pwTextfield = new JTextField();
        JLabel chip = new JLabel("Enter new player chips:");
        chipTextfield = new JTextField();
        JButton button1 = new JButton("Create Player");

        mainPanel.add(text);
        panel1.add(name);
        panel1.add(nameTextfield);
        panel1.add(pw);
        panel1.add(pwTextfield);
        panel1.add(chip);
        panel1.add(chipTextfield);
        mainPanel.add(panel1);
        mainPanel.add(button1);

        frame1.add(mainPanel);
        frame1.pack();
        frame1.setVisible(true);

        button1.addActionListener(new playerListener());

    }
  }