gpt4 book ai didi

java - setUndecorated(true) 到从 JOptionPane 实例创建的 JDialog

转载 作者:搜寻专家 更新时间:2023-11-01 01:17:24 25 4
gpt4 key购买 nike

我目前有一个 JDialog,它是通过从我的 JOptionPane 实例调用 createDialog() 方法创建的:

JOptionPane pane = new JOptionPane(myPanel, JOptionPane.PLAIN_MESSAGE,JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
dialog = pane.createDialog(null, "");

我希望能够通过调用 JDialog 上的 setUndecorated(true)JDialog 中删除标题栏,但是我得到了当我尝试运行我的程序时出现 IllegalComponentStateException: The dialog is displayable 异常。

据我所知,在我调用 dialog.show() 之前,对话框没有显示,这让我相信通过 实例化对话框时,对话框确实是“可显示的” code>pane.createDialog() 远远超出了我对 JDialog API 的理解。

我试图在使用 setUndecorated(true) 之前调用 setVisible(false),但无济于事。

对于如何或完全有可能删除此类 JDialog 的标题栏的任何帮助,我们将不胜感激。从普通的 JDialog 中删除标题栏很容易,从许多其他此类问题的答案中可以看出,但我似乎无法让它为 JDialog 工作通过 createDialog() 创建。

相关代码:

            input= new JTextField(50);

input.addKeyListener(new ConsoleKeyListener());


input.addAncestorListener( new RequestFocusListener() );
field = new JTextArea();
field.setEditable(false);
field.setLineWrap(true);
JScrollPane area = new JScrollPane(field, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
field.setRows(10);
field.setText(consoleText);
JPanel myPanel = new JPanel();

myPanel.setLayout(new BorderLayout(0,0));
myPanel.add(input, BorderLayout.PAGE_END);
myPanel.add(area, BorderLayout.PAGE_START);
input.setFocusable(true);
input.requestFocus();
int result = 101;
//int result = JOptionPane.showOptionDialog(null, myPanel,"", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
JOptionPane pane = new JOptionPane(myPanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);

dialog = pane.createDialog(null, "");
dialog.setVisible(false);
dialog.setUndecorated(true);
//dialog.undecorated = true;


//dialog.setOpacity(0.55f);
removeMinMaxClose(dialog);
removeMinMaxClose(pane);
removeMinMaxClose(myPanel);
dialog.getRootPane().setOpaque(false);

//JDialog dialog = new JDialog();
//dialog.setVisible(false);
//dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
//myPanel.setUndecorated(true);
//dialog.setUndecorated(true);
//dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
//dialog.setBounds( 100, 100, 300, 200 );
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.out.println("yo");
}
});
dialog.setVisible(true);
dialog.show();

最佳答案

您需要阅读 JavaDoc entry on Component#isDisplayable , 然后看看创建对话框的源代码

"A component is made displayable either when it is added to a displayable containment hierarchy or when its containment hierarchy is made displayable. A containment hierarchy is made displayable when its ancestor window is either packed or made visible."

基本上,对话框被打包为 createDialog 方法的一部分

可能的解决方案

一个可能的解决方案是创建您自己的对话框...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestOptionPane11 {

public static void main(String[] args) {
new TestOptionPane11();
}

public TestOptionPane11() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

final JDialog dialog = new JDialog((Frame)null, "Boo");

JOptionPane op = new JOptionPane("Look ma, no hands", JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
op.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
String name = evt.getPropertyName();
if ("value".equals(name)) {

dialog.dispose();

}
}
});

dialog.setUndecorated(true);
dialog.setLayout(new BorderLayout());
dialog.add(op);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
});
}

}

关于java - setUndecorated(true) 到从 JOptionPane 实例创建的 JDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17100587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com