java - 单击另一个 swing gui 中的按钮后打开另一个 swing gui

标签 java swing user-interface

我想在单击按钮后打开另一个 swing gui,但我无法弄清楚在其中包含按钮的主 gui 中执行此操作的代码:

   /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;
import static java.lang.System.out;
/**
 *
 * @author Matthew
 */
public class NewFrame extends java.awt.Frame {
/**
 * Creates new form NewFrame
 */
public NewFrame() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    jButton1.setText("jButton1");

    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm(evt);
        }
    });

    jButton2.setText("jButton2");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });
    jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            form(evt);
        }
    });
    add(jButton2, java.awt.BorderLayout.CENTER);

    pack();
}// </editor-fold>

/**
 * Exit the Application
 */
private void exitForm(java.awt.event.WindowEvent evt) {                          
    System.exit(0);
}                         

private void form(java.awt.event.KeyEvent evt) {

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    out.println("print something");
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new NewFrame().setVisible(true);
        }

    });
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}

这是我想要在单击按钮时打开的对话框 gui 文件:

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author Matthew
 */
public class NewJDialog extends javax.swing.JDialog {
/**
 * Creates new form NewJDialog
 */
public NewJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
}



/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setText("IT WORKS");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(72, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(128, 128, 128)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(97, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the dialog
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}

最佳答案

一种方法是在需要时创建 NewJDialog,将父窗口传递到其构造函数中,然后通过 setVisible(true) 将其设置为可见。但是您不希望原始窗口是一个 Frame(它是旧 AWT 库的一部分),而应该是一个 JFrame(Swing 库中的等效类)。

如果您对此建议有任何疑问,请询问!

关于java - 单击另一个 swing gui 中的按钮后打开另一个 swing gui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10568615/

相关文章:

c++ - 为什么 Qt 给我关于项目 "inability to create directory"的错误?

python - 使用用户输入打破嵌套循环

java - 无法从类 : java 中找到图像文件

java - 如何在 Java 中从字符串设置字体

java - Android 应用程序不合理地旋转为横向,然后又返回纵向

java - 如何制作 dateTime 函数并使用每个 JFrame?

java - 在 Swing 中构建一个类似于 SO 标记的搜索模块 UI

java - 使用 ActiveMQ 5,是否可以在内存和网络连接中配置代理?

java - JDialog取消按钮

java - 如何从 netbeans 中的另一个内部框架打开内部框架