我在java中有一个Swing应用程序,我在点击事件中打开一个Dialog
,然后在第一个对话框按钮点击时打开另一个对话框。
但是当我点击第二个对话框时,第一个对话框变得可见,第二个对话框被禁用,当我试图关闭那个没有关闭时。但是当我关闭第一个对话框然后启用它时。
那么我该如何解决这个问题。
我只需要在打开时只激活第一个对话框,打开它时激活第二个对话框。
对话序列。
JFrame-->Dialog 1 --> Dialog 2
我该如何解决?
答案 0 :(得分:2)
好吧,既然你坚持要求代码,我已经为你做了一个简单的例子,希望它有所帮助!
package com.learningjava;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* a simple demo to using two dialog
*
*/
public class DialogFrameDemo extends JFrame{
public static void main(String[] args) {
DialogFrameDemo frame = new DialogFrameDemo();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public DialogFrameDemo() {
super("Dialog Demo");
//prepare button in frame
JButton btnInFrame = new JButton("Click to open a Dialog");
JPanel framePanel = new JPanel();
framePanel.add(btnInFrame);
this.add(framePanel,BorderLayout.NORTH);
btnInFrame.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// generate dialog one
dialogOne = new JDialog(DialogFrameDemo.this,"Dialog one",false);
JPanel dialogPanel = new JPanel();
dialogPanel.setLayout(new GridLayout(3,1));
//prepare button in dialog one
JButton btnInDialogOne = new JButton("Click to open another Dialog");
btnInDialogOne.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//generate dialog second
JDialog dialogTwo = new JDialog(dialogOne,"Dialog Two",true);
JPanel dialogPanel = new JPanel();
dialogPanel.setLayout(new GridLayout(3,1));
dialogPanel.add(new JLabel("Dialog Two"));
dialogPanel.add(new JLabel("Lable 21"));
dialogPanel.add(new JLabel("Lable 22"));
dialogTwo.setContentPane(dialogPanel);
dialogTwo.pack();
dialogTwo.setVisible(true);
dialogTwo.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
});
dialogPanel.add(btnInDialogOne);
dialogPanel.add(new JLabel("Lable 11"));
dialogPanel.add(new JLabel("Lable 12"));
dialogOne.setContentPane(dialogPanel);
dialogOne.pack();
dialogOne.setVisible(true);
dialogOne.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
});
}
private static final long serialVersionUID = 1L;
private JDialog dialogOne;
}
答案 1 :(得分:1)
当按下JDialog1的按钮时,也许你应该尝试将第二个JDialog预览为JDialog1的InternalXXXDialog。 在这里寻找解释。 http://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html#showInternalOptionDialog-java.awt.Component-java.lang.Object-java.lang.String-int-int-javax.swing.Icon-java.lang.Object:A-java.lang.Object-