setUndecorated(true)为从JOptionPane实例创建的JDialog

我目前通过从我的JOptionPane实例调用createDialog()方法创建了一个JDialog

 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当我尝试运行我的程序时, IllegalComponentStateException: The dialog is displayableexception。

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

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

任何帮助将被理解为如何或根本可以删除这种类型的JDialog的标题栏。 从正常的JDialog删除标题栏很容易,从许多其他类型问题的答案可以看出,但我似乎无法让它适用于通过createDialog()创建的JDialog

相关代码:

  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(); 

您需要阅读Component#isDisplayable上的JavaDoc条目 ,然后查看create对话框的源代码

“当一个组件被添加到可显示的包含层次结构中或者当它的包含层次结构可显示时,它就可以显示。当一个包含层次结构被打包或可见时,它可以显示。”

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

可能的方案

一种可能的解决方案是创建自己的对话框……

在此处输入图像描述

 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); } }); } }