按钮覆盖彼此

时间:2013-10-29 03:04:17

标签: java swing awt

所以我的按钮会相互覆盖,而不是像工具栏一样向上移动。 如果有意义的话,我正试图让按钮向北移动。我知道我的GUI非常糟糕,一旦我完成这个原型,我就会重新连接它。

    // panels
    mainPuzzlerPanel = new Panel();
    mainPuzzlerPanel.setLayout(new BorderLayout());
    puzzlePanel = new Panel();

    //mainPuzzlerPanel.setLayout(null);
    puzzlePanel.setLocation(100, 120);

    // text fields
    debugTxt = new TextArea(null,6,40,1);
    debugTxt.setEditable(false);
    mainPuzzlerPanel.add(debugTxt,BorderLayout.NORTH);

    // buttons
    Button newPuzzle = new Button("New Puzzle");
    Button loadImage = new Button("Load Image");
    Button assignLocation = new Button("Assign Location");
    Button assignTimestamp = new Button("Assign Timestamp");
    Button savePuzzle = new Button("Save Puzzle");
    Button clearPuzzleCreator = new Button("Clear");

    newPuzzle.addActionListener(this);
    loadImage.addActionListener(this);
    assignLocation.addActionListener(this);
    assignTimestamp.addActionListener(this);
    savePuzzle.addActionListener(this);
    clearPuzzleCreator.addActionListener(this);

    mainPuzzlerPanel.add(assignLocation,BorderLayout.NORTH);
    mainPuzzlerPanel.add(assignTimestamp,BorderLayout.NORTH);

    mainPuzzlerPanel.add(loadImage,BorderLayout.NORTH);
    mainPuzzlerPanel.add(savePuzzle,BorderLayout.NORTH);
    mainPuzzlerPanel.add(clearPuzzleCreator,BorderLayout.NORTH);
    mainPuzzlerPanel.add(newPuzzle,BorderLayout.NORTH);
    mainPuzzlerPanel.add(puzzlePanel,BorderLayout.CENTER);

    add(mainPuzzlerPanel, "Controls");

    setSize(1200, 700);
    setVisible(true);

1 个答案:

答案 0 :(得分:4)

你无法添加所有组件BorderLayout.NORTH,没有任何意义。相反,将JButtons添加到使用不同布局的JPanel,比如GridLayout,然后添加JPanel BorderLayout.NORTH。但最重要的是 - 阅读有关如何使用布局管理器的教程。看起来你在猜这个,这不是学习如何使用这些复杂工具的有效方法。

Regading,

  

我知道我的GUI非常糟糕,一旦我完成这个原型,我就会重新连接它。

也不是一个好的计划。第一次写出来要好得多。

如,

// after creating all of your JButtons, put them in an array...
JButton[] btnArray = {newPuzzle, loadImage, assignLocation, assignTimestamp, 
        savePuzzle, clearPuzzleCreator};
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
for (JButton btn : btnArray) {
  buttonPanel.add(btn);
}
mainPuzzlerPanel.add(buttonPanel, BorderLayout.NORTH);

编辑:糟糕,我注意到你现在使用的是按钮和面板,而不是JButton和JPanel。我建议您将应用程序更改为Swing应用程序而不是AWT应用程序。


布局管理器教程:Laying Out Components Within a Container

相关问题