2012-02-25 28 views
4

我正在開發一個擺動應用程序。我有兩個屏幕,從屏幕截圖1擺動,多屏幕,控制轉移

1.A按鈕將啓動屏幕2.

僞代碼:

ScreenA extends JFrame{ 

    onButtonClick(){ 
     Screen2.setVisible(true); 
    } 
    System.out.println("Hai"); 
} 

Screen2 extends JFrame{ 
    onButtonClick{ 
     Hide this screen; 
    } 
} 

現在輸出的是:

  1. 屏幕2將顯示 2.Hai將被打印。

我的目標是:只有當屏幕2上的一個按鈕被點擊並且屏幕2消失時,我纔會顯示hai。

我該如何實現它?

我試圖設置一個標誌按鈕在屏幕上的兩個按鈕。 但程序只是通過條件並繼續到下一行。 我如何保持控制?

+0

從你問的解決方案是在隱藏後在Screen2.onButtonClick()中打印「Hai」。如果這是錯誤的闡述你的問題。 – 2012-02-25 11:02:49

+0

不是。目標是將完全控制權轉移到可見屏幕上,並從控制權轉移之時繼續執行。 – Achilles 2012-02-25 11:06:45

回答

4

希望這段代碼中的評論能幫助你解釋一些事情。

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

public class TwoFrames 
{ 
    // Making our first JFrame. 
    JFrame frame1 = new JFrame("FRAME 1"); 
    // Declaring our second JFrame. 
    JFrame frame2 ; 

    public void createAndDisplayGUI() 
    {    
     // Used to close the JFrame graciously. 
     frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  

     // Used to position the JFrame at the middle of the screen. 
     //frame1.setLocationRelativeTo(null);  

     // Use this instead for placing windows, as determined by the OS. 
     frame1.setLocationByPlatform(true);  

     // Calling this method to create our frame2. 
     makeNewFrame(); 

     // Button to show the second JFrame. 
     JButton showButton = new JButton("SHOW NEW FRAME"); 
     showButton.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       // Checking if the frame2 is already visible 
       // on the screen, if YES then we won't 
       // create a new frame, else a new frame 
       // will be created. This will prevent multiple 
       // JFrame to be created at the click of this button.     
       if (!(frame2.isShowing())) 
       { 
        // If you had already disposed it previously 
        // by clicking the hide Button on the other frame 
        // then the click on this button will recreate 
        // a new frame to be displayed. 
        makeNewFrame(); 
        frame2.setVisible(true); 
       } 
      } 
     }); 

     // Adding the button to the South side of the frame1. 
     frame1.add(showButton, BorderLayout.PAGE_END); 
     frame1.pack(); 
     frame1.setVisible(true); 
    } 

    private void makeNewFrame() 
    {  
     frame2 = new JFrame("FRAME 2"); 
     frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame2.setLocationByPlatform(true); 

     // Creating a JButton to be shown on the JFrame. 
     JButton hideButton = new JButton("HIDE FRAME"); 
     hideButton.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       // On the click of this button, frame2 will 
       // disappear and HAI will be displayed on the console. 
       frame2.dispose(); 
       System.out.println("HAI"); 
      } 
     }); 

     // Adding the button to the South side of the frame1. 
     frame2.add(hideButton, BorderLayout.PAGE_END); 
     frame2.pack(); 
    } 

    public static void main(String... args) 
    { 
     /* Here we are Secheduling a JOB for 
     * Event Dispatcher Thread, since Swing 
     * is not Thread Safe. This is used to place 
     * the code which is responsible for 
     * creating and diaplaying your GUI. 
     */ 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       TwoFrames tf = new TwoFrames(); 
       tf.createAndDisplayGUI(); 
      } 
     }); 
    } 
} 
+1

儘管我不喜歡這種方法(2幀)和所有大寫字符串,但只是爲評論投票。 :)請注意,第二個評論應該是// //聲明我們的第二個JFrame. – 2012-08-12 06:07:30

+0

@AndrewThompson:Ahha,thankx指出,很好的捕獲:-)我愚蠢的使用那個地方的評論:-) – 2012-08-12 06:31:18