【问题标题】:Clear components of JFrame and add new components清除 JFrame 的组件并添加新组件
【发布时间】:2011-12-23 02:13:58
【问题描述】:

我有一个JFrame,它有一些选项。当按下确定按钮时,我想要相同的JFrame 来清除内容并添加新内容。我已经尝试过了,但问题是弹出了新的JFrame。我做错了什么?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class GuiFrame extends JFrame {

    final JFrame f = new JFrame("Test");

    public void Starter(){
        ImageIcon img = new ImageIcon("C:\\Users\\neal\\Desktop\\no.png");
        f.setIconImage(img.getImage());
        ButtonGroup group = new ButtonGroup();
        final JRadioButton Acess = new JRadioButton("Acess");
        final JRadioButton Chat = new JRadioButton("Chat");
        group.add(Acess);
        group.add(Chat);
        f.setSize(400,100);
        f.setLocationRelativeTo(null);
        JButton OptionOk = new JButton("OK");

Label option = new Label("Choose a Option");

        final Container content = f.getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout());

        content.add(option);
        content.add(Acess);
        content.add(Chat);
        content.add(OptionOk);
          f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              OptionOk.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {


                try {
                    new GuiFrame().Initiate();
                } catch (UnknownHostException ex) {
                    Logger.getLogger(GuiFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
              });
    }

    public void Initiate() throws UnknownHostException {

        f.removeAll();
        ButtonGroup group = new ButtonGroup();

        final JRadioButton ButtonServer = new JRadioButton("Server");
        final JRadioButton ButtonClient = new JRadioButton("Client");
        group.add(ButtonServer);
        group.add(ButtonClient);

        f.setSize(400, 100);
        f.setLocationRelativeTo(null);
        InetAddress thisIp = InetAddress.getLocalHost();

        ImageIcon img = new ImageIcon("C:\\Users\\neal\\Desktop\\no.png");
        f.setIconImage(img.getImage());
        Label lip = new Label("Your IP is : " + thisIp.getHostAddress());
        Label setup = new Label("Setup as ");
        JButton ButtonOk = new JButton("OK");

        final Container content = f.getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout());
        content.add(lip);
        content.add(setup);
        content.add(ButtonServer);
        content.add(ButtonClient);
        content.add(ButtonOk);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws UnknownHostException {

        GuiFrame gf = new GuiFrame();
        gf.Starter();
    }
}

【问题讨论】:

  • 有一些java代码约定:先用小写char写变量和字段,先用大写写类名。对象 obj = new Object();

标签: java swing jframe layout-manager cardlayout


【解决方案1】:

固定(但仍然很脏)版本:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class GuiFrame implements ActionListener{

    final JFrame f = new JFrame("Test");

    public void start(){
        ImageIcon img = new ImageIcon("C:\\Users\\neal\\Desktop\\no.png");
        f.setIconImage(img.getImage());
        ButtonGroup group = new ButtonGroup();
        final JRadioButton Acess = new JRadioButton("Acess");
        final JRadioButton Chat = new JRadioButton("Chat");
        group.add(Acess);
        group.add(Chat);
        f.setSize(400,100);
        f.setLocationRelativeTo(null);
        JButton OptionOk = new JButton("OK");

        Label option = new Label("Choose a Option");

        final Container content = f.getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout());

        content.add(option);
        content.add(Acess);
        content.add(Chat);
        content.add(OptionOk);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        OptionOk.addActionListener(this);

    }

    public void initiate() throws UnknownHostException {

        //f.removeAll();
        ButtonGroup group = new ButtonGroup();

        final JRadioButton ButtonServer = new JRadioButton("Server");
        final JRadioButton ButtonClient = new JRadioButton("Client");
        group.add(ButtonServer);
        group.add(ButtonClient);

        f.setSize(400, 100);
        f.setLocationRelativeTo(null);
        InetAddress thisIp = InetAddress.getLocalHost();

        ImageIcon img = new ImageIcon("C:\\Users\\neal\\Desktop\\no.png");
        f.setIconImage(img.getImage());
        Label lip = new Label("Your IP is : " + thisIp.getHostAddress());
        Label setup = new Label("Setup as ");
        JButton ButtonOk = new JButton("OK");



        final Container content = f.getContentPane();
        content.removeAll();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout());
        content.add(lip);
        content.add(setup);
        content.add(ButtonServer);
        content.add(ButtonClient);
        content.add(ButtonOk);
        f.repaint();

    }

    public void actionPerformed(ActionEvent arg0) {
        try {
            initiate();
        } catch (UnknownHostException ex) {
            Logger.getLogger(GuiFrame.class.getName()).log(Level.SEVERE, null, ex);
        }       
    }

    public static void main(String[] args) throws UnknownHostException {

        GuiFrame gf = new GuiFrame();
        gf.start();
    }
}

【讨论】:

    【解决方案2】:

    解决方案很简单:使用 CardLayout 并让这个布局管理器为您完成所有繁重的工作。更多详细操作请看教程:How to use CardLayout

    至于您的代码,请注意,您实际上是在启动时创建了 2 个 JFrame,如果按下 JButton,则会创建另外两个:

    GuiFrame 类本身扩展了 JFrame,但它似乎是一个您从不使用的 JFrame,因此被浪费了,但它仍然是在程序启动时创建的,并且无论何时创建 GuiFrame 实例,例如按下按钮时。然后在这个类中你创建另一个 JFrame f,一个在程序启动时,再一次在按钮按下时,我不认为这是你想要做的。

    因此,请更改您的代码,以便该类不扩展 JFrame,并且不要在按钮的 ActionListener 中创建该类的新实例。而是使用 CardLayout 来交换视图。

    例如:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class GuiFrame {
    
       private static final String FIRST_PANEL = "First Panel";
       private static final String SECOND_PANEL = "Second Panel";
       final JFrame f = new JFrame("Test");
       private CardLayout cardLayout = new CardLayout();
       private JPanel content;
    
       public void Starter() {
          f.setSize(400, 100);
          f.setLocationRelativeTo(null);
          JButton OptionOk = new JButton("OK");
    
          Label option = new Label("Choose a Option");
    
          content = (JPanel) f.getContentPane();
          content.setLayout(cardLayout);
    
          JPanel firstPanel = new JPanel();
          firstPanel.setBackground(Color.white);
          firstPanel.setLayout(new FlowLayout());
    
          firstPanel.add(option);
          firstPanel.add(OptionOk);
    
          content.add(firstPanel, FIRST_PANEL);
          content.add(createSecondPanel(), SECOND_PANEL);
          f.setVisible(true);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          OptionOk.addActionListener(new ActionListener() {
    
             public void actionPerformed(ActionEvent e) {
    
                cardLayout.show(content, SECOND_PANEL);
    
             }
          });
    
       }
    
       private JPanel createSecondPanel() {
          JPanel secondPanel = new JPanel();
          secondPanel.add(new JButton(new AbstractAction("Go Back") {
             public void actionPerformed(ActionEvent e) {
                cardLayout.show(content, FIRST_PANEL);
             }
          }));
          return secondPanel;
       }
    
    
       public static void main(String[] args) {
    
          GuiFrame gf = new GuiFrame();
          gf.Starter();
       }
    
    }
    

    【讨论】:

    • 很好解释,为 CardLayout +1。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多