문제

안녕 모두들. 나는 버튼과 라벨이 달린 스윙 gui를 만들려고 노력하고 있습니다. 국경 레이아웃을 사용하고 레이블 (북쪽 필드)이 잘 표시되지만 버튼은 프레임의 나머지 부분을 차지합니다 (중앙 필드에 있습니다). 이 문제를 해결하는 방법이 있습니까?

도움이 되었습니까?

해결책

버튼을 다른 패널에 추가 한 다음 해당 패널을 프레임에 추가해야합니다.

국경 전지가 중간에있는 구성 요소가 무엇인지 확장하는 것으로 나타났습니다.

코드는 지금 이렇게 보일 것입니다.

전에

public static void main( String [] args ) {
    JLabel label = new JLabel("Some info");
    JButton button = new JButton("Ok");

    JFrame frame = ... 

    frame.add( label, BorderLayout.NORTH );
    frame.add( button , BorderLayout.CENTER );
    ....

}

다음과 같은 것으로 변경하십시오.

public static void main( String [] args ) {
    JLabel label = new JLabel("Some info");
    JButton button = new JButton("Ok");
    JPanel panel = new JPanel();
     panel.add( button );

    JFrame frame = ... 

    frame.add( label, BorderLayout.NORTH );
    frame.add( panel , BorderLayout.CENTER);
    ....

}

전 후

http://img372.imageshack.us/img372/2860/beforedl1.png 이전 http://img508.imageshack.us/img508/341/aftergq7.png 이후

다른 팁

또는 절대 레이아웃 만 사용하십시오. 레이아웃 팔레트에 있습니다.

또는 다음과 같이 활성화합니다.

frame = new JFrame();
... //your code here

// to set absolute layout.
frame.getContentPane().setLayout(null);

이렇게하면 원하는 곳에 어디서나 컨트롤을 자유롭게 배치 할 수 있습니다.

다시 :)


    import javax.swing.*;

    public class TestFrame extends JFrame {
        public TestFrame() {
            JLabel label = new JLabel("Some info");
            JButton button = new JButton("Ok");
            Box b = new Box(BoxLayout.Y_AXIS);
            b.add(label);
            b.add(button);
            getContentPane().add(b);

        }
        public static void main(String[] args) {
            JFrame f = new TestFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.setVisible(true);

        }
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top