java 如何从按钮关闭 jdialog?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13289116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to close jdialog from a button?
提问by mopr mopr
i have a Jframe (Mainz),
我有一个 Jframe(美因茨),
it have a button (showDialog),
它有一个按钮(showDialog),
when user click the button,
当用户点击按钮时,
jdialog (Dialogz) will show,
jdialog (Dialogz) 将显示,
that jdialog have a button
jdialog 有一个按钮
- how to close jdialog from that button (inside jdialog)?
- can i change the modal of dialog after i create instance of it?
- 如何从该按钮(在 jdialog 内)关闭 jdialog?
- 创建对话框实例后,我可以更改对话框的模式吗?
i need to block the owner of that jdialog
我需要阻止那个 jdialog 的所有者
heres i try ...
继承人我尝试...
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class Mainz extends JFrame implements ActionListener{
JButton showDialog = new JButton("show dialog");
public Mainz() {
setLayout(new FlowLayout());
showDialog.addActionListener(this);
add(showDialog);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
new Dialogz(this, true);
}
public static void main(String[]args){
new Mainz();
}
}
class Dialogz extends JDialog{
JButton close = new JButton("close");
public Dialogz(JFrame owner,boolean modal) {
super(owner, modal);
System.out.println(this.getModalityType());
add(close);
setLocationRelativeTo(owner);
setVisible(true);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
closez();
}
});
}
void closez(){
setModal(false);
this.dispose();
System.out.println("Method Done");
}
}
thanks a lot for any kind of help
非常感谢任何形式的帮助
回答by mKorbel
can i change the modal of dialog after i create instance of it?
创建对话框实例后,我可以更改对话框的模式吗?
yes you can to change setModal
or ModalityTypes
on runtime, but for code in this form doesn't make me any sence
是的,您可以更改setModal
或ModalityTypes
在运行时更改,但是对于这种形式的代码,我没有任何意义
how to close jdialog from that button (inside jdialog)?
如何从该按钮(在 jdialog 内)关闭 jdialog?
in this case doesn't matter if you'll to call setVisible
or dispose()
在这种情况下,您是否要打电话setVisible
或dispose()
create
JDialog
only one time,create that as local variable
change
myDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
, thenbutton
in thetoolbar
(withX
) to hideJDialog
toothen you can to call from
actionPerformed
onlymyDialog.setVisible(true)
andModalityType
too if required, setVisible shoud be wrapped ininvokeLater()
, instead of creating a new instance (new Dialogz(this, true);
)JButton
placed inJDialog
will be called onlymyDialog.setVisible(false)
JDialog
只创建一次,将其创建为局部变量
变化
myDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
,则button
在toolbar
(与X
)隐藏JDialog
过那么您可以
actionPerformed
只调用 frommyDialog.setVisible(true)
,ModalityType
如果需要,也可以调用setVisibleinvokeLater()
,而不是创建一个新实例 (new Dialogz(this, true);
)JButton
放入JDialog
只会被调用myDialog.setVisible(false)
回答by mopr mopr
somebody told me..
某人告诉我..
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class Mainz extends JFrame implements ActionListener{
JButton showDialog = new JButton("show dialog");
public Mainz() {
setLayout(new FlowLayout());
showDialog.addActionListener(this);
add(showDialog);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
new Dialogz(this, false);
setEnabled(false);
}
public static void main(String[]args){
new Mainz();
}
}
class Dialogz extends JDialog{
JButton close = new JButton("close");
public Dialogz(JFrame owner,boolean modal) {
super(owner, modal);
add(close);
setLocationRelativeTo(owner);
setVisible(true);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
closez();
}
});
}
void closez(){
System.out.println("before ="+getModalityType());
setModal(true);
System.out.println("after ="+getModalityType());
getOwner().setEnabled(true);
Dialogz.this.dispose();
}
}