-2

我正在创建一个具有 1 个 JFrame java 文件和 1 个 JDialog java 文件的应用程序。在 JFrame 中,我有一个按钮,按下时我希望它显示我在 JDialog 中设计的内容。

例如,我的 JFrame java 文件称为 MMainView.java,而我的 JDialog 称为 OptionView.java。因此,当按下 MMainView.java 中的按钮时,我想显示我在 OptionView.java 中设计的 JDialog。

所以在我的 MMainView.java 文件中,我有一个在按下该按钮时调用的函数。如何在 OptionView.java 中显示对话框?

解决了

对于那些想知道的人。这就是我所做的:

private JDialog optionView; ~~> JDialog Declaration 
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:          
           if (optionView == null) {
        JFrame mainFrame = myApp.getApplication().getMainFrame();
        optionView = new OptionView(mainFrame, true);
        optionView.setLocationRelativeTo(mainFrame);
    }
    myApp.getApplication().show(optionView);
}  
4

3 回答 3

1

听起来您想为按钮创建一个 ActionListener,并在按下按钮时将 JDialog 的可见性设置为 true。

这些线上的东西:

    final JButton button = new JButton();

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionevent)
        {
            //set the visibility of the JDialog to true in here
        }
    });
于 2013-10-22T10:55:26.583 回答
0

假设您的按钮名为 myBtn。

类应该是这样的。

public class MMainView extends JFrame
    implements ActionListener

您应该为按钮使用侦听器。

JButton myBtn = new JButton();
myBtn.addActionListener(this);

最后:

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == myBtn) {
        new OptionView();

您实际上并不需要 if,只是以防万一您想为 actionPerformed 添加更多按钮。

于 2013-10-22T10:57:10.123 回答
0

首先在“MainView.java”中注册按钮,如下所示。

b1.addActionListener(this);
b1.setName("OpenJDialog"); 
//this is to read in actionperformed method incase you have more than one button

// in action performed method call the dialogue class
public void actionPerformed(ActionEvent ae){

  JButton jb = (JButton)ae.getSource();
  String str = jb.getName();
  if(str.equals("OpenJDialog"){
     new OptionView(); 
      //I  am assuming u are configuring jdialog content in OptionView constructor
   }
}
于 2013-10-22T11:00:18.683 回答