2013-03-30 43 views
0

我有這樣的Java代碼:在異常線程 「AWT-EventQueue的-0」 java.awt.IllegalComponentStateException:幀是可顯示的

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

所有時間該代碼產生以下情況除外:

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) 

我已經盡力了。這是什麼? 即使此控件在控制檯中顯示,也會顯示輸出窗口。但是當我試圖調試代碼時,這會遇到麻煩。我已經看到了這個。但沒有找到解決辦法。有人請幫助我。

回答

1

必須調用

setUndecorated(true); 

您設置框架可見之前。

+1

我試過了。但同樣的例外! – Salini

1

嘗試調用

this.setUndecorated(true); 

在JDialog的構造:

例子:

public JDialog() { 
    this.setUndecorated(true); 
    initComponents(); 
} 

它在這裏工作。

0

我做了什麼讓它變得全屏後可見,我做了一個方法只是初始化和使框架可見,用全屏布爾參數。其中有一個if語句,表示布爾值是否爲true,其內部爲setUndecorated(true)setExtendedState(JFrame.MAXIMIZED_BOTH)。在全屏方法中,我使用frame.dispose(),然後通過使用true參數調用方法來重新初始化它,這樣它就會全屏顯示。這對我有效。

相關問題