JDialog的使用方法
JDialog是JavaGUI的次级容器。
JDialog 分为模态对话框和非模态对话框。
1.模态对话框
在不处理当前对话框之前无法进行其他操作。
JDialog dialog = new JDialog(jf,"selcting",true);
jf:表示对话框对应的窗体
“selcting”: 表示对话框的名字,出现在左上角。
true:表示该对话框为模态对话框。反之为非模态对话框。
2.模态对话框
在不处理当前对话框之前可以进行其他操作。
JDialog dialog = new JDialog(jf,"selcting",false);
3.对于对话框的其他操作。
dialog.setSize(200, 300);//设置其大小
dialog.setLocation(3, 4)//设置其位置
dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);//设置关闭模式
dialog.setVisible(true);//设置可见
关于JDialog的完整代码:
package UsingJFrame;
import java.awt.Dialog;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Demo02 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
creatFrame();
}
});
}
private static void creatFrame() {
JFrame jf = new JFrame("Playing");
jf.setSize(700,800);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
JDialog dialog = new JDialog(jf,"selcting",true);
dialog.setSize(200,300);
dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
dialog.setVisible(true);
}
}
运行结果: