gpt4 book ai didi

java - “BoxLayout 无法与 .add 共享”

转载 作者:行者123 更新时间:2023-12-02 09:55:21 25 4
gpt4 key购买 nike

我正在尝试为 java swing 程序编写一些代码,但我的程序抛出错误。我查看了有类似问题的帖子,但没有一个适用于这种情况。我究竟做错了什么?

我已将面板重新排列为更符合逻辑的格式,但没有帮助。

这是我的代码,问题在评论中指出。每个缩进都是另一个面板级别。

BaseballStats()
{
JFrame myFrame = new JFrame();
FlowLayout myLayout = new FlowLayout();
myFrame.setLayout(myLayout);

// configure JFrame with basic settings
myFrame.setTitle("JFrame Window");
myFrame.setSize(400,200);
myFrame.setLocation(200, 300);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel myPanel = (JPanel)myFrame.getContentPane();
myPanel.setSize(200,200);
myPanel.setLayout(new BorderLayout());

JPanel topmiddlePanel = new JPanel();
topmiddlePanel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.Y_AXIS));

JPanel game1panel = new JPanel();
game1panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
game1panel.setAlignmentX(Component.TOP_ALIGNMENT);

JLabel G1Label = new JLabel("Game 1 hits:");
G1Label.setAlignmentX(Component.LEFT_ALIGNMENT);
//problem here ---->// game1panel.add(G1Label);
JSpinner G1Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
G1Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
game1panel.add(G1Spinner);
topmiddlePanel.add(game1panel);

JPanel game2panel = new JPanel();
game2panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
game2panel.setAlignmentX(Component.TOP_ALIGNMENT);

JLabel G2Label = new JLabel("Game 2 hits:");
G2Label.setAlignmentX(Component.LEFT_ALIGNMENT);
game2panel.add(G2Label);
JSpinner G2Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
G2Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
game2panel.add(G2Spinner);
topmiddlePanel.add(game2panel);

JPanel game3panel = new JPanel();
game3panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
game3panel.setAlignmentX(Component.CENTER_ALIGNMENT);

JLabel G3Label = new JLabel("Game 3 hits:");
G3Label.setAlignmentX(Component.LEFT_ALIGNMENT);
game3panel.add(G3Label);
JSpinner G3Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
G3Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
game3panel.add(G3Spinner);
topmiddlePanel.add(game3panel);

JPanel game4panel = new JPanel();
game4panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
game4panel.setAlignmentX(Component.BOTTOM_ALIGNMENT);

JLabel G4Label = new JLabel("Game 4 hits:");
G4Label.setAlignmentX(Component.LEFT_ALIGNMENT);
game4panel.add(G4Label);
JSpinner G4Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
G4Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
game4panel.add(G4Spinner);
topmiddlePanel.add(game4panel);

JPanel game5panel = new JPanel();
game5panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
game5panel.setAlignmentX(Component.BOTTOM_ALIGNMENT);

JLabel G5Label = new JLabel("Game 5 hits:");
G5Label.setAlignmentX(Component.LEFT_ALIGNMENT);
game5panel.add(G5Label);
JSpinner G5Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
G5Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
game3panel.add(G5Spinner);
topmiddlePanel.add(game5panel);

myPanel.add(topmiddlePanel, BorderLayout.CENTER);

它应该生成一个整体面板,带有 3 个子面板(其中 2 个未显示)。所示面板应在盒式布局中具有 5 个辅助面板。每个面板都有一个标签和一个流中的旋转器。

感谢您的帮助!

最佳答案

BoxLayout 有点特殊,因为布局需要给出它正在布局的组件实例。 “BoxLayout 无法共享”异常始终表示已使用特定组件创建了 BoxLayout 实例,但随后将其作为布局添加到不同的组件。

在您的情况下,相关行是:

    JPanel topmiddlePanel = new JPanel();
JPanel game1panel = new JPanel();
game1panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));

看到 BoxLayout 是用 topmiddlePanel 实例化的,但随后在 game1panel 中设置为布局?

我不会提供更正的代码,因为我不确定在这种情况下您到底想要做什么,但正确的解决方案必须采用以下形式:

x.setLayout(new BoxLayout(x, ...))

关于java - “BoxLayout 无法与 .add 共享”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041635/

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