打开 jdialog 的 JFrame 按钮:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
打开对话框的 JDialog 方法:
public void run() {
AddClient dialog = new AddClient(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);
我的 JDialog 类名为 AddClient,我的 JFrame 类名为 MainWindow。这些所在的包名为 my.javaapp
我将如何继续这样做?我对 java 还很陌生,到目前为止一直在使用 python,并使用 netbeans 深入研究 java swing gui 构建器,我计划观看教程并阅读 java 文档以了解有关 java 的更多信息,但我想首先能够打开这个 jdialog。
在问这个问题之前,我进行了很多搜索,并且我阅读了其他被要求解决或多或少相同问题的问题,但我仍然无法解决我的问题,所以我问。
解决方案:
从您想要弹出的 gui 所在的类中删除 main 函数,例如
public static void main(String args[]) {
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(NewJDialog1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJDialog1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJDialog1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJDialog1.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() {
NewJDialog1 dialog = new NewJDialog1(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);
}
});
}
在删除之前,请复制此行:
NewJDialog1 dialog = new NewJDialog1(new javax.swing.JFrame(), true);
右键单击您想要在按下后弹出此窗口的按钮,事件>操作>执行的操作。像这样粘贴到那里:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
NewJDialog1 dialog = new NewJDialog1(new javax.swing.JFrame(), true);
dialog.setVisible(true);
}
记住添加最后一行,这就是显示对话框的内容。
最佳答案
JDialog 上的构造函数接受父对象。但是您使用新的 JFrame 对象。这是错误(逻辑错误)。
AddClient dialog = new AddClient(MainWindow.this);
如果您想要模式对话框,请调用
dialog.setModal(true);
并设置默认关闭操作。
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
不需要使用线程。
Ps:抱歉英语不好
编辑: 这是对话框中退出代码的简单示例
public class AddClient extends JDialog {
public static final int ID_OK = 1;
public static final int ID_CANCEL = 0;
private int exitCode = ID_CANCEL;
public AddClient(Frame owner) {
super(owner);
createGUI();
}
public AddClient(Dialog owner) {
super(owner);
createGUI();
}
private void createGUI() {
setPreferredSize(new Dimension(600, 400));
setTitle(getClass().getSimpleName());
pack();
setLocationRelativeTo(getParent());
}
@Override
public void dispose() {
super.dispose();
}
public int doModal() {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModal(true);
setVisible(true);
return exitCode;
}
}
和主窗口
public class MainWindow extends JFrame {
public MainWindow() throws HeadlessException {
super();
createGUI();
}
private void createGUI() {
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle(getClass().getSimpleName());
JButton addClientButton = new JButton("Add client");
addClientButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
AddClient dialog = new AddClient(MainWindow.this);
if(dialog.doModal() == AddClient.ID_OK) {
}
}
});
}
});
JToolBar toolBar = new JToolBar();
toolBar.add(addClientButton);
add(toolBar, BorderLayout.PAGE_START);
}
@Override
public void dispose() {
super.dispose();
}
}
关于java - 如何从 jframe 打开 jdialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30381351/