"BoxLayout 无法共享"与 .add



我正在尝试为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));

请参阅BoxLayouttopmiddlePanel实例化,但然后将其设置为game1panel中的布局?

我不会提供校正的代码,因为在这种情况下,我不确定您要准确地做什么,但是正确的解决方案必须是形式:

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

相关内容

  • 没有找到相关文章

最新更新