Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

java - Clear components of JFrame and add new components

I have a JFrame, which have some options. When OK button is pressed i want the same JFrame to clear the contents and add new contents. I have tried it but the problem is new JFrame is popped out. What am I doing wrong?

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution is simple: use a CardLayout and let this layout manager do all the heavy lifting for you. For more details on how to do this, please see the tutorial: How to use CardLayout

As for your code, please note that you are actually creating 2 JFrames when it starts and two more if the JButton is pushed:

The GuiFrame class itself extends JFrame, but it appears to be a JFrame that you never use and thus is wasted, but it is created nonetheless on program start up and whenever GuiFrame instance is created, such as when the button is pressed. Then inside of this class you create another JFrame f, one on program start up and once again on button press, and I don't think that this is what you want to do.

So change your code so that the class does not extend JFrame, and don't create a new instance of the class in your buttons' ActionListener. Instead use a CardLayout to swap views.

For example:

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...