无法摆动菜单,有什么不对?

时间:2015-06-08 18:14:09

标签: java swing

我没有多少时间,所以我会简洁。

import java.awt.*;
import javax.swing.*;

  public class GameMenu {
    public static void addComponentsToPane(Container cone){     
  JPanel panelA = new JPanel();
  cone.add(panelA);

  panelA.setLayout(new BoxLayout(800, 800));
  JButton b1 = new JButton("one");
  JButton b2 = new JButton("two");
  JButton b3 = new JButton("three");  
  JButton b4 = new JButton("four");
  JButton b5 = new JButton("five");
  JButton b6 = new JButton("six");
  JButton b7 = new JButton("seven");
  JButton b8 = new JButton("eight");
  JButton b9 = new JButton("nine");  

  public GameMenu(){

panelA.setLayout(null);
panelA.setBounds(800, 800);
b1.setBounds(50, 200);
b2.setBounds(350, 200);
b3.setBounds(650, 200);
b4.setBounds(50, 400);
b5.setBounds(350, 400);
b6.setBounds(650, 400);
b7.setBounds(50, 600);
b8.setBounds(350, 600);
b9.setBounds(650, 600);

}

public static void main(String[] args) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      createAndShowGUI();

        }

构造函数javax.swing.BoxLayout未定义,并且无法解析整个公共GameMenu()。帮助解决这个问题?

2 个答案:

答案 0 :(得分:0)

您无法“全局”设置JPanel的布局;你必须在构造函数中完成它。

另外,我不知道你为什么要两次更改布局类型(首先是BoxLayout,然后是Null布局)。

答案 1 :(得分:0)

您没有将按钮添加到JPanel。在面板中添加按钮,如下所示:

panelA.add(b1);
panelA.add(b2);
// and so on