.add无法共享BoxLayout

时间:2019-05-08 13:10:36

标签: java swing layout-manager boxlayout

我正在尝试为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个子面板,它们在一个盒子中。每个THOSE面板在流程中都有一个标签和一个微调器。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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, ...))