2013-10-29 2 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의와 JPanel의를 사용하고 확인할 수 있습니다. 앱을 AWT 앱이 아닌 스윙 앱으로 변경 하시길 바랍니다.


레이아웃 매니저 튜토리얼 : Laying Out Components Within a Container

관련 문제