我正在尝试制作一个简单的计算器,以熟悉我刚刚在计算机科学课上介绍的GUI的创建。我有一个问题,让我按照我想要的方式安排GUI。我认为BoxLayout会很完美,但每当我尝试使用它时,我的程序会崩溃并显示错误日志:
Exception in thread "AWT-EventQueue-0" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(Unknown Source)
at javax.swing.BoxLayout.invalidateLayout(Unknown Source)
at javax.swing.BoxLayout.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at GUI.run(GUI.java:64)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这是我的代码:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class GUI implements Runnable{
@Override
public void run() {
JFrame window = new JFrame("Calculator");
window.setLayout(new BoxLayout(window, BoxLayout.Y_AXIS));
JPanel display = new JPanel();
JLabel label = new JLabel("display test");
display.add(label);
JPanel row1 = new JPanel();
JButton sevenButton = new JButton("7");
JButton eightButton = new JButton("8");
JButton nineButton = new JButton("9");
JButton divideButton = new JButton("÷");
JPanel row2 = new JPanel();
JButton fourButton = new JButton("4");
JButton fiveButton = new JButton("5");
JButton sixButton = new JButton("6");
JButton multiplyButton = new JButton("x");
JPanel row3 = new JPanel();
JButton oneButton = new JButton("1");
JButton twoButton = new JButton("2");
JButton threeButton = new JButton("3");
JButton minusButton = new JButton("-");
JPanel row4 = new JPanel();
JButton periodButton = new JButton(".");
JButton zeroButton = new JButton("0");
JButton enterButton = new JButton("=");
JButton addButton = new JButton("+");
row1.add(sevenButton);
row1.add(eightButton);
row1.add(nineButton);
row1.add(divideButton);
row2.add(fourButton);
row2.add(fiveButton);
row2.add(sixButton);
row2.add(multiplyButton);
row3.add(oneButton);
row3.add(twoButton);
row3.add(threeButton);
row3.add(minusButton);
row4.add(periodButton);
row4.add(zeroButton);
row4.add(enterButton);
row4.add(addButton);
window.add(display);
window.add(row1);
window.add(row2);
window.add(row3);
window.add(row4);
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
当我将BoxLayout更改为FlowLayout时,程序运行正常,但这不是我希望我的程序所需的布局类型。
答案 0 :(得分:1)
尝试类似:
//window.setLayout(new BoxLayout(window, BoxLayout.Y_AXIS));
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
window.setContentPane(contentPane);
通过这种方式,您可以确保BoxLayout
已添加到面板中,以用作框架的内容窗格。
另一种选择是将所有组件直接添加到" contentPane"面板,然后将此面板添加到框架:
//window.setLayout(new BoxLayout(window, BoxLayout.Y_AXIS));
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
...
//window.add(display);
//window.add(row1);
//window.add(row2);
//window.add(row3);
//window.add(row4);
contentPane.add(display);
contentPane.add(row1);
contentPane.add(row2);
contentPane.add(row3);
contentPane.add(row4);
window.add(contentPane);