2012-01-05 8 views
3

私はJavaとGUIを学んでいます。私はいくつかの質問があります。最初は、JFrameのサブクラスとJFrameのインスタンスの作成に大きな違いがある場合です。サブクラスがより強力であるように思えますか?私それはGUIを作成するときに、このコードを使用する必要があります場合も不思議:新しいコンテンツを表示するためにJava GUIとJFrameをクリアしますか?

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

私は私のGUIクラスを追加し、それは私が手に持っているタスクに、これまでの簡単なテストだ、ユーザが入力しました。テキストフィールドにテキストを入力し、ボタンを押して次のステップに進んでください。フレームをクリアして新しいコンテンツを表示するにはどうすればよいですか、これを行う特別な方法はJavaにありますか?私は、新しいウィンドウを作成するのではなく、同じウィンドウを使用する方がいいでしょうか?ヘルプid preciate!おかげ

// 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

ニース!パネルの数に制限はありますか?ですから、私はパネルに異なるコンテンツを追加するだけです! –

+0

いいえ実際には制限はありません。しかし、java.awtパッケージが提供するさまざまなレイアウトについてもっとよく学びましょう。それは何らかの形であなたを助けることができます。すべてのコンポーネントにデフォルトのレイアウト設定があるので、JFrameにはBorderLayoutがあり、JPanelにはFlowLayoutがあります。よろしくお願いします。 –

+0

私は彼らがContentPaneから離れた時を知りませんcontentPane = frame.getContentPane();上記の答えは、2番目の負荷でJframeに表示される重複データを修正しました –

4

あなたはJComponentのを(そして追加と削除])切り替えたい場合は、

1)を追加する/ JComponentのを削除してから呼び出す必要があり

revalidate(); 
repaint()// sometimes required 

2)より良く、最も簡単な選択肢実装されるでしょうCardLayout

+0

+1ですCardLayout'、 'のためにより良いし、常に追加/削除スタッフ – Robin

2

あなたの要件は、次と前のボタンを持つパネル、および次の/前のボタンをクリックすると、いくつかのコンポーネントを示すことです。あなたはCardLayoutを試してみることができます。

CardLayoutは、同じ表示スペースを共有する2つ以上のコンポーネント(通常はJPanelインスタンス)を管理します。 CardLayoutを使用すると、ユーザーはコンポーネント間で選択できます。この小さなコードは、あなたが物事を説明するのに役立つかもしれない

How to Use CardLayout

0

あなたのクラスはJFrameのを拡張する場合は、あなたが行うことができます:

getContentPane().removeAll(); 
関連する問題