2015-08-19 163 views
0

我有一個代碼,打開一個JOptionPane對話框,一旦用戶點擊一個按鈕。我想要做的第一件事是當用戶點擊其中一個按鈕時關閉第一個JFrame。我試過這樣做如何在按好按鈕後打開新窗口?

setVisible(false); // Delete visibility 
     dispose(); //Delete window 

但是原來的JFrame只要按下一個按鈕就不會關閉。目標是有兩個按鈕。當按下時,顯示一個JOptionPane框,而同時關閉第一個JFrame窗口。我該怎麼做?接下來,在新的JOptionPane中插入ok之後,我不會激發什麼是動作偵聽器。我通過調用方法

sinceyoupressedthecoolbutton(); 

在我的JOptionPane聲明後。這是第二個問題開始的地方,它顯示的JOptionPane完美,但不走的方法

sinceyoupressedthecoolbutton(); 

我不知道問題出在方法調用或方法的內容。基本上,在JOptionPane上按下ok後,我不想移動到另一個打開新JLabel的方法。

下面是代碼:

package Buttons; 

import java.awt.Dimension; 
import java.awt.FlowLayout; //layout proper 
import java.awt.event.ActionListener; //Waits for users action 
import java.awt.event.ActionEvent; //Users action 
import javax.swing.JFrame; //Window 
import javax.swing.JLabel; 
import javax.swing.JButton; //BUTTON!!! 
import javax.swing.JDialog; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JOptionPane; //Standard dialogue box 

public class ButtonClass extends JFrame { 

private JButton regular; 
private JButton custom; 

public ButtonClass() { // Constructor 
    super("The title"); // Title 
    setLayout(new FlowLayout()); // Default layout 

    regular = new JButton("Regular Button"); 
    add(regular); 


    custom = new JButton("Custom", b); 

    add(custom); 

    Handlerclass handler = new Handlerclass(); 
    Otherhandlerclass original = new Otherhandlerclass(); 
    regular.addActionListener(handler); 
    custom.addActionListener(original); 

    //THIS WAS MY FIRST PROBLEM, I WANT TO CLOSE THE FIRST JFRAME WINDOW AS THE USER HITS OK 
    setVisible(false); // Close the show message dialog box 
    dispose(); 

} 

public class Handlerclass implements ActionListener { // This class is 
                 // inside the other 
                 // class 

    public void actionPerformed(ActionEvent eventvar) { // This will happen 
                 // when button is 
                 // clicked 
     JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand())); //opens a new window with the name of the button 
    } 
} 

public class Otherhandlerclass implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 

     JOptionPane.showMessageDialog(null,"Since you pressed that button, I will open a new window when you press ok, okay?"); 

     //Code works up until here 

     sinceyoupressedthecoolbutton(); //THIS METHOD SHOULD OPEN A NEW WINDOW! 
    } 

    public void sinceyoupressedthecoolbutton() { 

     JDialog YES = new JDialog(); 
     JLabel label = new JLabel("Here is that new window I promised you!"); 
     add(label); 
     YES.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); 
     YES.setSize(500, 500); 
     YES.setVisible(true); 

    } 

    public class okay implements ActionListener { 
     public void actionPerformed(ActionEvent ok) { 

     } 

    } 

} 

}

幫助將不勝感激!

回答

1

您正在使用事件驅動環境,而不是線性處理環境。這意味着你運行一些代碼,然後等待(等待完成了你)發生某些事件,然後你對它做出響應...

直到用戶按下按鈕custom,沒有什麼可以發生,所以它沒有意義試圖在此之前關閉框架

當您的OtherhandlerclassactionPerformed被觸發時,顯然您會顯示JOptionPane窗格,這將阻止執行流程直到它關閉。此時,您應該有機會處理原始窗口。的

所以不是調用構造函數setVisible(false)dispose,倒不如把它移動到Otherhandlerclass

public class Otherhandlerclass implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 

     dispose(); 
     JOptionPane.showMessageDialog(null, "Since you pressed that button, I will open a new window when you press ok, okay?"); 

     sinceyoupressedthecoolbutton(); 
    } 

    public void sinceyoupressedthecoolbutton() { 

     JDialog YES = new JDialog(); 
     JLabel label = new JLabel("Here is that new window I promised you!"); 
     YES.add(label); 
     YES.setSize(500, 500); 
     YES.setVisible(true); 

    } 

    public class okay implements ActionListener { 

     public void actionPerformed(ActionEvent ok) { 

     } 

    } 

} 

話雖如此,我會鼓勵你有The Use of Multiple JFrames, Good/Bad Practice?讀,也許可以考慮使用像How to Use CardLayout而不是

+0

感謝您的反饋,現在我已將dispose()方法從構造函數中移出,當按下「ok」按鈕時,窗口將關閉。現在還有另一個問題。我sinceyypressedthecoolbutton()方法沒有執行。它應該顯示一個具有JLabel的JDialog「這是我答應給你的新窗口!」這在「ok」被按下後不會顯示。我應該如何解決這個問題? –

+0

'YES.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);'不能應用於對話框 – MadProgrammer

+0

謝謝,所以JDialog已經內置了EXIT_ON_CLOSE功能?現在,當我按下「OK」後,一個新窗口打開,但它是空白的......它應該有一個JLabel說:「這是我答應你的新窗口!」爲什麼它是空白的? –

相關問題