创建简单的JFrame

时间:2016-12-30 07:25:40

标签: java swing jframe event-dispatch-thread

即使我的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);

0 个答案:

没有答案