gpt4 book ai didi

Java:BoxLayout 无法共享错误

转载 作者:行者123 更新时间:2023-12-02 12:05:17 28 4
gpt4 key购买 nike

我正在尝试制作一个简单的计算器,以熟悉我刚刚在计算机科学入门类(class)中学到的 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 时,程序工作正常,但这不是我想要的程序布局类型。

最佳答案

尝试如下:

//window.setLayout(new BoxLayout(window, BoxLayout.Y_AXIS));
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
window.setContentPane(contentPane);

这样您就可以确保将 BoxLayout 添加到面板中以用作框架的内容 Pane 。

另一个选项是将所有组件直接添加到“contentPane”面板,然后将此面板添加到框架:

//window.setLayout(new BoxLayout(window, BoxLayout.Y_AXIS));
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, 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);

关于Java:BoxLayout 无法共享错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46944470/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com