本文介绍了线程中的异常“AWT-EventQueue-0” java.awt.IllegalComponentStateException:框架是可显示的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个java代码:
I have this java code:
Editor()
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// Create Menu
JMenuBar menuBar = drawMenu();
frame.setJMenuBar(menuBar);
frame.setUndecorated(true);
frame.add(desktop, BorderLayout.CENTER);
frame.add(toolbar,BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.getRootPane().setWindowDecorationStyle(JRootPane.INFORMATION_DIALOG);
frame.setTitle("JIL");
frame.pack();
frame.setVisible(true);
}
});
}
public JMenuBar drawMenu()
{
// Create the menu bar
JMenuBar menuBar = new JMenuBar();
// Create a menu
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu run = new JMenu("Build");
JMenu help = new JMenu("Help");
menuBar.add(file);
menuBar.add(edit);
menuBar.add(run);
menuBar.add(help);
JMenu _new = new JMenu("New");
file.add(_new);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
JButton clsbutton=new JButton(new ImageIcon (classLoader.getResource ("img/cls.jpg")));
clsbutton.setSize(2, 2);
clsbutton.setToolTipText("New Class");
toolbar.add(clsbutton);
clsbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
new newClass();
}
});
}
newClass()
{
contentPanelC.removeAll();
getContentPane().removeAll();
setTitle("New Class");
setBounds(100, 100, 470, 400);
getContentPane().setLayout(new BorderLayout());
contentPanelC.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanelC, BorderLayout.CENTER);
contentPanelC.setLayout(null);
JLabel lblNewLabel = new JLabel("Class Name");
lblNewLabel.setBounds(50, 51, 120, 17);
contentPanelC.add(lblNewLabel);
contentPanelC.revalidate();
contentPanelC.repaint();
textField = new JTextField();
textField.setBounds(170, 51, 228, 20);
contentPanelC.add(textField);
contentPanelC.revalidate();
contentPanelC.repaint();
textField.setColumns(10);
textField.setText(null);
textField.revalidate();
textField.repaint();
String fnam=textField.getText();
System.out.println("Initial value TextField: "+fnam);
JLabel lblNewLabel_1 = new JLabel("Location");
lblNewLabel_1.setBounds(50, 111, 120, 17);
contentPanelC.add(lblNewLabel_1);
contentPanelC.revalidate();
contentPanelC.repaint();
textField_1 = new JTextField();
textField_1.setBounds(170, 111, 131, 20);
contentPanelC.add(textField_1);
contentPanelC.revalidate();
contentPanelC.repaint();
textField_1.setColumns(10);
textField_1.setText(null);
textField_1.revalidate();
textField_1.repaint();
final JButton btnNewButton = new JButton("Browse");
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
String workingdir = System.getProperty("user.dir");
chooser = new JFileChooser(new File(workingdir));
chooser.setDialogTitle("Select Location");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File selectedFile = chooser.getSelectedFile();
System.out.println(selectedFile.getName());
textField_1.setText(selectedFile.getPath());
}
else
{
System.out.println("No Selection ");
}
btnNewButton.revalidate();
}
});
btnNewButton.setBounds(320, 111, 89, 20);
contentPanelC.add(btnNewButton);
contentPanelC.revalidate();
contentPanelC.repaint();
{
JPanel buttonPaneC = new JPanel();
buttonPaneC.removeAll();
buttonPaneC.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPaneC, BorderLayout.SOUTH);
{
final JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
{
// some file operations
}
textField.setText("");
textField.revalidate();
textField.repaint();
textField_1.setText("");
textField_1.revalidate();
textField_1.repaint();
dispose();
}
});
okButton.setActionCommand("Ok");
buttonPaneC.add(okButton);
buttonPaneC.revalidate();
buttonPaneC.repaint();
getRootPane().setDefaultButton(okButton);
}
{
final JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPaneC.add(cancelButton);
buttonPaneC.revalidate();
buttonPaneC.repaint();
contentPanelC.validate();
contentPanelC.repaint();
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
dispose();
}
});
}
}
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
contentPanelC.validate();
contentPanelC.repaint();
}
}
此代码所有时间都会导致以下异常:
All time this code results in the following exception:
<pre lang="java">
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.
at java.awt.Frame.setUndecorated(Frame.java:825)
at Editor$1.run(Editor.java:93)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:673)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:643)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
我尽我所能。这是什么?
即使在控制台中显示此异常,也会出现输出窗口。但是当我尝试调试代码时,这会遇到麻烦。有人请帮助我。
I have tried my best. What is this?
Even this exception is shown in console the output window appears. But this troubles when I'm trying to debug the code. Somebody please help me.
推荐答案
这篇关于线程中的异常“AWT-EventQueue-0” java.awt.IllegalComponentStateException:框架是可显示的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!