java.awt.AWTERROR创建BoxLayout时

时间:2016-03-25 02:11:51

标签: java swing awt layout-manager boxlayout

我正在尝试使用Jlabel和3个单选按钮按降序填充boxlayout。该程序编译正常但随后错误输出错误BoxLayout无法共享。我见过有人说这个错误是因为他们试图将它附加到jframe,但在这种情况下,jpanel被赋予布局而不是框架。这是编译窗口的代码段。

JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    JPanel effortButtons = new JPanel();
    JPanel skillButtons = new JPanel();
    effortButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    skillButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    effortButtons.add(effortHeader);//this is what gives the error
    effortButtons.add(oneEffort);
    effortButtons.add(twoEffort);
    effortButtons.add(threeEffort);
    skillButtons.add(skillHeader);
    skillButtons.add(oneSkill);
    skillButtons.add(twoSkill);
    skillButtons.add(threeSkill);
    mainPanel.add(effortButtons, BorderLayout.WEST);
    mainPanel.add(skillButtons, BorderLayout.EAST);
    mainPanel.add(studentName, BorderLayout.NORTH);
    mainPanel.add(next, BorderLayout.SOUTH);
    add(mainPanel);
    pack();

1 个答案:

答案 0 :(得分:3)

// xxxxxxxxxxxxx                         xxxxxxxxx
   effortButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

您必须将获取布局的组件传递给BoxLayout构造函数。所以这应该是:

effortButtons.setLayout(new BoxLayout(efforButtons, BoxLayout.Y_AXIS));

同样对于我们的其他JPanel - 将其更改为:

skillButtons.setLayout(new BoxLayout(skillButtons, BoxLayout.Y_AXIS));

根据BoxLayout API

  

public BoxLayout(Container target, int axis)
  target - 需要布局的容器