1

我有这个java代码:

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)

我已经尽力了。这是什么?即使此异常显示在控制台中,也会出现输出窗口。但是当我尝试调试代码时,这很麻烦。我已经对此有所了解。但没有找到解决方案。有人请帮助我。

4

4 回答 4

5

试着打电话

this.setUndecorated(true);

在 JDialog 构造函数中:

例子:

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

它在这里工作。

于 2014-04-09T04:55:56.337 回答
3

尝试在 .pack() 之后调用 .setUndecorated(true) 时遇到了同样的问题。所以它不仅应该在.setVisible(true)之前,还应该在.pack()之前。

于 2019-03-16T13:46:35.940 回答
1

你必须打电话

setUndecorated(true);

在您将该框架设置为可见之前。

于 2013-03-30T08:50:42.303 回答
1

我做了什么让它在可见后全屏显示,我做了一个方法来初始化和使框架可见,并带有一个全屏的布尔参数。在其中,有一个 if 语句,用于判断布尔值是否为真,setUndecorated(true)以及setExtendedState(JFrame.MAXIMIZED_BOTH)在它内部。在全屏方法中,我使用frame.dispose()然后通过使用true参数调用该方法来重新初始化它,这样它将全屏显示。这对我有用。

于 2016-12-13T01:21:00.493 回答