我正在整合一個小程序,我需要破解一個對話框並改變它的模態。改變現有的JDialog的模式
我的問題是我不知道Swing,而我的嘗試在實踐中沒有任何效果。
當前實現:
dialog.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
dialog.repaint();
也試過
dialog.setModal(false);
所以有我的問題。我如何動態地改變現有JDialog的模式?
我正在整合一個小程序,我需要破解一個對話框並改變它的模態。改變現有的JDialog的模式
我的問題是我不知道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();
}
}
我猜你沒有獲得你的小程序AWTPermission.toolkitModality
權限得到的東西。您可以使用Toolkit.isModalExclusionTypeSupported(java.awt.Dialog.ModalExclusionType)
來檢查此問題。
要更改對話框是模態還是非模態,請使用setModalityType
方法。
setModal(true)
時,模態類型與調用setModalityType(Dialog.DEFAULT_MODALITY_TYPE)
相同。默認值是ModalityType.APPLICATION_MODAL
。setModal(true)
時,模式類型設置爲ModalityType.MODELESS
。當您更改模態時,該對話框應該不可見。否則,直到對話框被隱藏然後再次顯示它纔會生效。
此外,對話框本身必須進行編程以支持不同的模態模式。
dialog.setVisible(true)
,並且在對話框關閉之前此方法不會返回。然後你使用對話框中的數據。 dialog.setVisible(true)
立即返回(在屏幕上顯示對話框後)。按下對話框中的按鈕通常會對其他窗口和對話框產生一些影響。在顯示對話框時,您可以與應用程序的其他窗口進行交互。 如果您需要更多的幫助,我可以告訴你有對話框,在兩種模式下工作的工作示例:模式和無模式。
一個黑客的破解:
java.awt.Dialog.hideAndDisposePreHandler();
要調用這個私有方法 - 作爲一個例子:
您可以通過調用私有方法更改現有對話的方式
private void executeMethod(final Class<?> clazz, final String methodName, final Object instance)
{
final Method method =
Iterables.getOnlyElement(Iterables.filter(
Arrays.asList(clazz.getDeclaredMethods()), new Predicate<Method>()
{
public boolean apply(final Method method)
{
return method.getName().equals(methodName);
}
}));
method.setAccessible(true);
try
{
method.invoke(instance);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
{
throw Throwables.propagate(e);
}
}
(此代碼需要番石榴)
最後稱之爲:
final Dialog myDialog = ...;
executeMethod(Dialog.class, "hideAndDisposePreHandler", myDialog);
爲什麼? (15個字符) – kleopatra
請注意,當原始開發人員將對話框設計爲模態時,更改這可能會影響應用程序的正確工作 – Robin