Swing 中設定模態窗體和啟動位置
阿新 • • 發佈:2019-01-22
{
private JFrame frame =null;
privateboolean modal =false;
private String title =null;
public MyModalFrame() {
this(null, false);
}
public MyModalFrame(JFrame frame) {
this(frame, false);
}
public MyModalFrame(JFrame frame, boolean modal) {
this(frame, modal, "");
}
public MyModalFrame(JFrame frame, boolean modal, String title) {
super(title);
this.frame = frame;
this.modal = modal;
this.title = title;
this.init();
}
privatevoid init() {
if(modal)
frame.setEnabled(false);
this.addWindowListener( this);
}
publicvoid windowOpened(WindowEvent windowEvent) {
}
publicvoid windowClosing(WindowEvent windowEvent) {
if(modal)
frame.setEnabled(true);
}
publicvoid windowClosed(WindowEvent windowEvent) {
}
publicvoid windowIconified(WindowEvent windowEvent) {
}
publicvoid windowDeiconified(WindowEvent windowEvent) {
}
publicvoid windowActivated(WindowEvent windowEvent) {
}
publicvoid windowDeactivated(WindowEvent windowEvent) {
if(modal)
this.requestFocus();
}
}
private JFrame frame =null;
privateboolean modal =false;
private String title =null;
public MyModalFrame() {
this(null, false);
}
public MyModalFrame(JFrame frame) {
this(frame, false);
}
public MyModalFrame(JFrame frame, boolean modal) {
this(frame, modal,
}
public MyModalFrame(JFrame frame, boolean modal, String title) {
super(title);
this.frame = frame;
this.modal = modal;
this.title = title;
this.init();
}
privatevoid init() {
if(modal)
frame.setEnabled(false);
this.addWindowListener(
}
publicvoid windowOpened(WindowEvent windowEvent) {
}
publicvoid windowClosing(WindowEvent windowEvent) {
if(modal)
frame.setEnabled(true);
}
publicvoid windowClosed(WindowEvent windowEvent) {
}
publicvoid windowIconified(WindowEvent windowEvent)
}
publicvoid windowDeiconified(WindowEvent windowEvent) {
}
publicvoid windowActivated(WindowEvent windowEvent) {
}
publicvoid windowDeactivated(WindowEvent windowEvent) {
if(modal)
this.requestFocus();
}
}