2013-07-01 33 views
0

我的程序是關於一家超市。當我編譯程序時,屏幕上出現JFrame窗口'f1'和'f2'。不過,我希望JFrame窗口'f1'先出現,然後在單擊'f1'窗口的JButton'b1'後,我想讓JFrame窗口'f2'出現。下面是我的節目的傳送()方法:如何僅在按下前一個JFrame窗口的JButton之後才顯示JFrame窗口?

public static void delivery() 
{ 
    final JFrame f1 = new JFrame("Name"); 
    GridLayout grid = new GridLayout(20, 40, 10, 8); 
    f1.setLayout(grid); 
    f1.setVisible(true); 
    f1.setSize(600,200); 
    f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    f1.setLocation(700,450); 

    JPanel p1 = new JPanel(); 

    final JLabel l1 = new JLabel("Enter your name: "); 

    final JTextField jt1 = new JTextField(20); 

    JButton b1 = new JButton("Ok"); 
    b1.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      input1 = jt1.getText(); 
      f1.dispose(); 
     } 
    }); 

    p1.add(b1); 
    p1.add(l1); 
    p1.add(jt1); 
    f1.add(p1); 

    final JFrame f2 = new JFrame("Address"); 
    f2.setVisible(true); 
    f2.setSize(600,200); 
    f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    f2.setLocation(700,450); 

    JPanel p2 = new JPanel(); 

    final JLabel l2 = new JLabel("Enter your address: "); 

    final JTextField jt2 = new JTextField(20); 

    JButton b2 = new JButton("Ok"); 
    b2.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      input2 = jt2.getText(); 
      f2.dispose(); 
     } 
    }); 

    p2.add(b1); 
    p2.add(l2); 
    p2.add(jt2); 
    f2.add(p2); 

    JOptionPane.showMessageDialog(null, "The ordered stuff will be delivered to " +input1+ " who lives in: " +input2 , "Delivery" , JOptionPane.PLAIN_MESSAGE); 
    JOptionPane.showMessageDialog(null, "Thank you for shopping at Paradise 24/7. Hope to see you again." , "Shopping Done!" , JOptionPane.PLAIN_MESSAGE); 
} 
+0

參見[多重JFrames使用,好/壞實踐?](http://stackoverflow.com/a/9554657/418556) –

回答

1

的代碼行進行幀出現在這個

f1.setVisible(true); 

你有這樣對您的交付方法中兩個框架。

爲了使一個出現在此,這樣一個被設置爲可見,另一個獲取該按鈕的代碼中取消設置其他變更後,即(你會明顯有這雖然之前聲明F2)

b1.addActionListener(new ActionListener() 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     input1 = jt1.getText(); 
     f1.dispose(); 
     //f1.setVisible(false); // or dispose if you no longer need it 
     f2.setVisible(true); 
    } 
}); 

只是一個建議:更好的方法可能使用JDialog。這將允許您獲取用戶等待響應的輸入形式,然後提示輸入下一個輸入。 Click here for tutorial on Dialogs

您可能還需要添加組件的幀/面板時看一些佈局。 GridLayoutBorderLayoutFlowLayout

+0

我是一個Java初學者。我實際上對JFrame和ActionListener沒有任何線索,但我設法通過YouTube學習它。所以我並沒有太多的Java。我只知道它的基本知識。 – siddhu99

+1

@ siddhu99然後我會建議花時間通過[Java教程](http://docs.oracle.com/javase/tutorial/)來讀取和[創建與Swing的GUI(http://docs.oracle。 COM/JavaSE的/教程/ uiswing /) – MadProgrammer

0

只需在Button的actionPerformed()中添加代碼f2.setVisible(true);即可。

例如,

f1.setBounds(whatever); 
f2.setBounds(whatever); 

//add button in JFrame and actionListener 
f1.setVisible(true); 
f2.setVisible(false); 

actionPerformed(ActionEvent e) 
{ 
    f2.setVisible(true); 
} 
0

那麼你必須閱讀JavaDocs第一和閱讀Java初學者像Java 2完全參考了一些很好的電子書,O'Really - 的Java Swing將會對你有所幫助