2012-03-05 27 views
14

アプリケーション起動時にJFrame(クラスStartUpWindowはJFrameを拡張するクラス)を表示する必要があるnetbeans Javaアプリケーションがあります。 JFrameを閉じて新しいクラス(MainWindowクラス)を開く必要があります。ボタンをクリックすると、Javaスイングアプリケーション、ウィンドウを閉じて別のウィンドウを開く

これを正しく行うにはどうすればよいですか。私は明らかにStartupWindowのボタンのクリックハンドラを設定しましたが、StartUpWindowを閉じてMainWindowを開くことができるように、このハンドラには何を入れますか?すべてのウィンドウが独自のスレッドを持っているように見えます...または、スレッドの問題がJFrames自身によって自動的に処理されるようになります。

+3

[複数のJFramesの使用、良い/悪い練習ですか?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657) [実行時にトップレベルコンテナを削除](http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime) – mKorbel

答えて

22

は一例です:

enter image description here

enter image description here

StartupWindow.java

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 


public class StartupWindow extends JFrame implements ActionListener 
{ 
    private JButton btn; 

    public StartupWindow() 
    { 
     super("Simple GUI"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     btn = new JButton("Open the other JFrame!"); 
     btn.addActionListener(this); 
     btn.setActionCommand("Open"); 
     add(btn); 
     pack(); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     String cmd = e.getActionCommand(); 

     if(cmd.equals("Open")) 
     { 
      dispose(); 
      new AnotherJFrame(); 
     } 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable(){ 

      @Override 
      public void run() 
      { 
       new StartupWindow().setVisible(true); 
      } 

     }); 
    } 
} 

AnotherJFrame.java

import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class AnotherJFrame extends JFrame 
{ 
    public AnotherJFrame() 
    { 
     super("Another GUI"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     add(new JLabel("Empty JFrame")); 
     pack(); 
     setVisible(true); 
    } 
} 
+0

素晴らしい例、Upvoted !!! – ron

+0

@ Eng.Fouad 'AnotherJFrame'から' StartupWindow'を開き、 'AnotherJFrame'に' StartupWindow'のオブジェクトを作成すると動作しません –

9

dispose()は現在のウィンドウで、setVisible(true)は表示したい

+0

JFrame変数をクラススコープにしておきますか?または、2番目のJFrameが表示されたらガベージコレクションを選択できるようにしますか? –

+0

dispose()は、他の参照を保持していない限り、ガベージコレクション可能にします。 –

9

これは、明らかにCardLayoutを使用するシナリオです。ここでは2つのJFrameを開く代わりに、CardLayoutを使ってJPanelを変更するだけです。

GUIの作成と表示を担当するコードは、SwingUtilities.invokeLater(...)の内部にある必要があります。メソッドをスレッドセーフにする必要があります。詳細については、Concurrency in Swingについてお読みください。

あなたのアプローチに固執したい場合は、ここにヘルプのサンプルコードがあります。

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class TwoFrames 
{ 
    private JFrame frame1, frame2; 
    private ActionListener action; 
    private JButton showButton, hideButton; 

    public void createAndDisplayGUI() 
    { 
     frame1 = new JFrame("FRAME 1"); 
     frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     frame1.setLocationByPlatform(true); 

     JPanel contentPane1 = new JPanel(); 
     contentPane1.setBackground(Color.BLUE); 

     showButton = new JButton("OPEN FRAME 2"); 
     hideButton = new JButton("HIDE FRAME 2"); 

     action = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       JButton button = (JButton) ae.getSource(); 

       /* 
       * If this button is clicked, we will create a new JFrame, 
       * and hide the previous one. 
       */ 
       if (button == showButton) 
       { 
        frame2 = new JFrame("FRAME 2"); 
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame2.setLocationByPlatform(true); 

        JPanel contentPane2 = new JPanel(); 
        contentPane2.setBackground(Color.DARK_GRAY); 

        contentPane2.add(hideButton); 
        frame2.getContentPane().add(contentPane2); 
        frame2.setSize(300, 300); 
        frame2.setVisible(true); 
        frame1.setVisible(false); 
       } 
       /* 
       * Here we will dispose the previous frame, 
       * show the previous JFrame. 
       */ 
       else if (button == hideButton) 
       { 
        frame1.setVisible(true); 
        frame2.setVisible(false); 
        frame2.dispose(); 
       } 
      } 
     }; 

     showButton.addActionListener(action); 
     hideButton.addActionListener(action); 

     contentPane1.add(showButton); 

     frame1.getContentPane().add(contentPane1); 
     frame1.setSize(300, 300); 
     frame1.setVisible(true); 
    } 
    public static void main(String... args) 
    { 
     /* 
     * Here we are Scheduling a JOB for Event Dispatcher 
     * Thread. The code which is responsible for creating 
     * and displaying our GUI or call to the method which 
     * is responsible for creating and displaying your GUI 
     * goes into this SwingUtilities.invokeLater(...) thingy. 
     */ 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new TwoFrames().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

と出力は次のようになります。ここでは

Frame1Frame2

+1

もう一度会いましょう。もう一つの素晴らしい例です。 – ron

+1

@ron:この[例](http://stackoverflow.com/a/9443609/1057230)では、ユーザーが複数の 'JFrame'を開くことを許可していません。これは' JFrame'新しいものを開かないようにしてください。 –

+0

ラブリー、ありがとう! – ron

1
 final File open = new File("PicDic.exe"); 
     if (open.exists() == true) { 
      if (Desktop.isDesktopSupported()) { 
       javax.swing.SwingUtilities.invokeLater(new Runnable() { 

        public void run() { 
         try { 
          Desktop.getDesktop().open(open); 
         } catch (IOException ex) { 
          return; 
         } 
        } 
       }); 

       javax.swing.SwingUtilities.invokeLater(new Runnable() { 

        public void run() { 
         //DocumentEditorView.this.getFrame().dispose(); 
         System.exit(0); 
        } 

       }); 
      } else { 
       JOptionPane.showMessageDialog(this.getFrame(), "Desktop is not support to open editor\n You should try manualy"); 
      } 
     } else { 
      JOptionPane.showMessageDialog(this.getFrame(), "PicDic.exe is not found"); 
     } 

//あなたはそれを使用して別のアプリを起動することができ、多くのアプリで、あなたのプロジェクト全体をスリットすることができます。それは たくさん

0

使用this.dispose現在のウィンドウのために閉じていきますとnext_window.setVisible(true)がボタンプロパティActionPerformedの背後にある次のウィンドウを表示するには、例があなたの助けのためのPICで以下に示されています。方法下記

enter image description here

+0

あなたが開いているよりもまず 'this.dispose()'が書かれていることを確認してくださいあなたの次の 'JFrame' –

0

コールだけで新しいウィンドウを開くためのメソッドを呼び出した後に、これは現在のウィンドウを閉じます。 JFrameののプロパティでも

private void close(){ 
    WindowEvent windowEventClosing = new WindowEvent(this, WindowEvent.WINDOW_CLOSING); 
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowEventClosing); 
} 

defaultCloseOperationDISPOSEとして設定されていることを確認してください。

関連する問題