2012-01-05 602 views
3

我正在學習Java和GUI。我有一些問題,首先是創建JFrame的子類和JFrame的實例之間有什麼主要區別。看起來像一個子類更強大?我也想知道是否有必要使用創建GUI時這個代碼:關於Java GUI並清除JFrame以顯示新內容?

Container contentPane = getContentPane(); 
contentPane.setLayot(new Flowlayout()); 

添加我的GUI類,這是一個簡單的測試,到目前爲止,到我手的任務。當用戶輸入。文本框中的一些文本,然後按按鈕繼續下一步,我該如何清除框架並顯示新內容,或者有沒有一種特殊的方法可以在Java中執行此操作?我想最好是使用相同的窗口而不是創建新的!?幫助ID preciated!由於

// Gui class 

    import java.awt.FlowLayout; // layout 
    import java.awt.event.ActionListener; // listener 
    import java.awt.event.ActionEvent; // event 

    import javax.swing.JFrame; // windows properties 
    import javax.swing.JLabel; // row of text 
    import javax.swing.JTextField; // enter text 
    import javax.swing.JOptionPane; // pop up dialog 
    import javax.swing.JButton; // buttons 

    // import.javax.swing.*; 

    public class Gui extends JFrame { 

    private JLabel text1; 
    private JTextField textInput1; 
    private JTextField textInput2; 
    private JButton nextButton; 

    // constructor creates the window and it's components 
    public Gui() { 
     super("Bank"); // title 
     setLayout(new FlowLayout()); // set default layout 

     text1 = new JLabel("New customer"); 
     add(text1); 

     textInput1 = new JTextField(10); 
     add(textInput1); 

     nextButton = new JButton("Continue"); 
     add(nextButton); 

     // create object to handle the components (action listener object) 
     frameHandler handler = new frameHandler(); 
     textInput1.addActionListener(handler); 
     nextButton.addActionListener(handler); 
    } 

    // handle the events (class inside another class inherits contents from class outside) 
    private class frameHandler implements ActionListener { 

     public void actionPerformed(ActionEvent event){ 

      String input1 = ""; 

      // check if someone hits enter at first textfield 
      if(event.getSource() == textInput1){ 
       input1 = String.format(event.getActionCommand()); 
       JOptionPane.showMessageDialog(null, input1); 
      } 

      else if(event.getSource() == nextButton){ 
       // ?? 
      } 
     } 
    } 
} 

回答

4

這個小代碼可以幫助你交代的事情:

import java.awt.event.*; 
import javax.swing.*; 

public class FrameDisplayTest implements ActionListener 
{ 
    /* 
    * Creating an object of JFrame instead of extending it 
    * has no side effects. 
    */ 
    private JFrame frame; 
    private JPanel panel, panel1; 
    private JTextField tfield; 
    private JButton nextButton, backButton; 

    public FrameDisplayTest() 
    { 
     frame = new JFrame("Frame Display Test"); 
     // If you running your program from cmd, this line lets it comes 
     // out of cmd when you click the top-right RED Button. 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     panel = new JPanel(); 
     panel1 = new JPanel(); 

     tfield = new JTextField(10); 

     nextButton = new JButton("NEXT"); 
     backButton = new JButton("BACK"); 
     nextButton.addActionListener(this); 
     backButton.addActionListener(this); 

     panel.add(tfield); 
     panel.add(nextButton); 
     panel1.add(backButton); 

     frame.setContentPane(panel); 
     frame.setSize(220, 220); 
     frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent ae) 
    { 
     JButton button = (JButton) ae.getSource(); 
     if (tfield.getText().length() > 0) 
     { 
      if (button == nextButton) 
      { 
       /* 
       * this will remove the first panel 
       * and add the new panel to the frame. 
       */ 
       frame.remove(panel); 
       frame.setContentPane(panel1); 
      } 
      else if (button == backButton) 
      { 
       frame.remove(panel1); 
       frame.setContentPane(panel); 
      } 
      frame.validate(); 
      frame.repaint(); // prefer to write this always. 
     } 
    } 

    public static void main(String[] args) 
    { 
     /* 
     * This is the most important part ofyour GUI app, never forget 
     * to schedule a job for your event dispatcher thread : 
     * by calling the function, method or constructor, responsible 
     * for creating and displaying your GUI. 
     */ 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       new FrameDisplayTest(); 
      } 
     }); 
    } 
} 
+0

不錯!有多少面板可以有限制?所以我只是給面板添加不同的內容! – 2012-01-05 22:27:19

+0

不,實際上沒有限制。但是更好的學習java.awt包提供的不同佈局。這可以以某種方式幫助你。由於每個組件都有默認的佈局設置,比如JFrame有BorderLayout和JPanel有FlowLayout。 Regards – 2012-01-05 23:04:51

+0

我不知道他們何時離開ContentPane contentPane = frame.getContentPane();但上面的答案修復了在第二次加載時出現在我的Jframe上的重複數據 – 2017-09-07 19:43:27

4
如果你想切換(ADD然後刪除)JComponents

,那麼你必須

1)添加/刪除JComponents然後調用

revalidate(); 
repaint()// sometimes required 

2)更好的和最容易的選擇將器具CardLayout

+0

+1'CardLayout',這是更好,然後不斷添加/刪除的東西 – Robin 2012-01-05 23:28:29

2

如果你的要求是做一個嚮導,單擊下一步和上一個按鈕面板,並單擊顯示一些組件下一首/上一個按鈕。你可以嘗試使用CardLayout。

的CardLayout管理共享相同的顯示空間的兩個或更多個部件(通常的JPanel實例)。 CardLayout讓用戶在組件之間進行選擇。

How to Use CardLayout

0

如果你的類擴展JFrame中,你可以這樣做:

getContentPane().removeAll();