2011-11-06 113 views
2

我有一個JFrame,它有一些選項。當確定按鈕被按下時,我想要相同的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(); 
    } 
} 
+0

請參閱例如在編輯 –

+0

有一些Java代碼約定:先寫變量和字段用小寫字符,首先用大寫寫入類名。 Object obj = new Object(); – MartinL

回答

5

解決方案很簡單:使用CardLayout並讓這個佈局管理器爲您完成所有繁重工作。有關如何做到這一點的詳細信息,請參見教程:How to use CardLayout

至於你的代碼,請注意,您實際上是創建2個JFrames啓動時,另外兩個如果將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(); 
    } 

} 
+0

很好解釋,CardLayout爲+1。謝謝 – nebula

+0

不客氣。祝你好運! –

0

固定的(但仍然髒)版本:

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

Opps !!沒有工作! – nebula