2013-10-29 40 views
-1

因此,我的按钮覆盖对方,而不是像工具栏一样北上。 我试图让按钮上北如果是有道理的。我知道我的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); 

回答

4

您不能添加所有的组件BorderLayout.NORTH,没有任何意义。相反,将JButton添加到使用不同布局的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将和JPanels。我恳请您将您的应用程序更改为Swing应用程序,而不是AWT应用程序。


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