我正在集成一个小程序,我需要破解其中一个对话框并更改其模式。
我的问题是我不懂Swing,我的尝试在实践中没有效果。
当前实现:
dialog.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
dialog.repaint();
也尝试过
dialog.setModal(false);
所以这是我的问题。如何动态更改现有 JDialog 的模式?
最佳答案
不知道你想做什么... 但也许你可以从这里得到一些东西
public class Mainz extends JFrame implements ActionListener{
JButton showDialog = new JButton("show dialog");
public Mainz() {
setLayout(new FlowLayout());
showDialog.addActionListener(this);
add(showDialog);
setSize(200, 300);
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);
setSize(100, 200);
add(close);
setLocationRelativeTo(owner);
setVisible(true);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
closez();
}
});
}
void closez(){
setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
System.out.println("modal exclusion befor = "+getModalExclusionType());
setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
System.out.println("modal exclusion after = "+getModalExclusionType());
System.out.println("modality before ="+getModalityType());
setModal(true);
System.out.println("modality after ="+getModalityType());
getOwner().setEnabled(true);
Dialogz.this.dispose();
}
}
关于java - 更改现有 JDialog 的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13293836/