即使我的JFrame
很简单,我也有问题:当我创建框架时,我希望Frame {2} JPanel
一个TextArea
,另一个带两个按钮(保存,关闭) TextArea
只显示没有按钮,为了解决这个问题,我使用setVisible(true)
两个面板出现,但出现此错误:
(Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
This operation is permitted on the event thread only; currentThread = AWT-EventQueue-0
at com.sun.glass.ui.Application.checkEventThread(Application.java:443)
at com.sun.glass.ui.Cursor.setVisible(Cursor.java:107) for using setVisible(true))
这是我的代码:
JPanel textAreaGrid = new JPanel(new GridLayout(1, 0, 3, 3)); // gridlayout 1 row
textAreaGrid.add(new JScrollPane(textArea));
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 3, 3));
buttonPanel.add(new JButton("Save"));
buttonPanel.add(new JButton("End"));
JPanel mainPanel = new JPanel();
mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
mainPanel.setLayout(new BorderLayout(3, 3)); // main GUI uses border layout
mainPanel.add(textAreaGrid, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.PAGE_END);
JFrame frame = new JFrame("The Results");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
setVisible(true);