2015-04-17 57 views
0

我想知道如何在第二個JFrame(2)中單擊按鈕時打開此JFrame表單(1)。問題是我無法獲取Form 2中的.setVisible方法。請幫忙。謝謝&關心! :)通過單擊按鈕打開新的JFrame

表1(當點擊表格按鈕2

public class FlightForm { 

    public FlightForm() { 
     initialize(); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        FlightForm window = new FlightForm(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

表2

public class MainMenu{ 

private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       MainMenu window = new MainMenu(); 
       window.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public MainMenu() { 
    frame = new JFrame("Main Menu"); 
    setBounds(100, 100, 830, 574); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JButton btnNewButton = new JButton("Flight Form"); 
    ); 
    btnNewButton.setFont(new Font("Candara", Font.BOLD, 15)); 
    btnNewButton.setBounds(169, 328, 193, 77); 
    frame.getContentPane().add(btnNewButton); 

    JButton btnNewButton_1 = new JButton("Passenger Form"); 
    btnNewButton_1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      PassengerForm window = new PassengerForm(); 
       window.setVisible(true); // This is not working 
+0

[Java的可能重複通過單擊按鈕打開一個新窗口](http://stackoverflow.com/questions/11273267/java-open-a-new-window-by-clicking-a-button) – ControlAltDel

+1

請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/q/9554636/418556) –

回答

0

您可以在PassengerForm()只有PassengerForm類叫setVisible(true)被打開擴展JFrame。如果不是,你應該使用類似於:

PassengerForm window = new PassengerForm(); 
window.getFrame().setVisible(true) 
+0

這不工作的人。 我已經解決了我自己的問題:D 我需要首先以我想要打開的形式擴展JPanel。 – niibz