我正在尝试使BoxLayout充满Jlabel和3个单选按钮,并以降序排列。该程序可以正常编译,但是无法共享错误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();

最佳答案

// 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)
  目标-需要布置的容器

关于java - 创建BoxLayout时java.awt.AWTERROR,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36213011/

10-12 02:37