2014-09-30 4 views
0

자바에서 간단한 인터페이스를 만들려고하는데 프레임에 여러 개의 패널을 추가 할 때 문제가 있습니다. 카페 소프트웨어가되도록 여러 테이블이 필요합니다. 여기 내 코드입니다프레임에 여러 개의 작은 패널 추가

public class CafeView extends JFrame{ 

private JButton takeMoneyBtn = new JButton("Hesabı Al"); 

public CafeView(){ 
    JPanel table1 = new JPanel(); 
    this.setSize(800,600); 
    this.setLocation(600, 200); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setVisible(true); 

    table1.setBounds(100, 100, 150, 150); 
    table1.setLayout(null); 
    this.add(table1); 
    table1.add(takeMoneyBtn); 

} 
} 

내가 그것을 실행하면, 난 그냥 내가 그것을 할 수 있다면 내가 어떻게 할 수 think.So 너무 다른 사람을 추가 할 수 있습니다, 빈 frame.In에게 난 그냥 하나 개의 간단한 패널을 추가하려고이 코드를 참조 나는 그것을 해결하고 프레임에 작고 많은 패널을 추가한다. 나는 무엇을 놓치고 있는가? 도움을 주셔서 감사합니다. (다른 클래스에서이 인터페이스 클래스를 호출하는 주 방법이 없습니다.)

+2

를 참조 어쩌면 당신의 프레임은 모두 비어 있지 않습니다. JPanel을 실제로 볼 수 있도록 setBorder()를 사용하여 패널에 테두리를 추가해보십시오. – MarsAtomic

+3

모든 시각적 구성 요소를 함께 모으기 전에 setVisible (true)을 호출하는 것은 잘못입니다. – ControlAltDel

+0

Java GUI는 다른 OS, 화면 크기, 화면 해상도 등에서 작동해야합니다. 따라서 픽셀 완전성에 도움이되지 않습니다 형세. 대신 레이아웃 관리자 또는 [조합] (http://stackoverflow.com/a/5630271/418556)과 [공백] 레이아웃 채우기 및 테두리 (http://stackoverflow.com/a/17874718/)를 사용하십시오. 418556). –

답변

1

당신은 .setBounds(..,..)을 사용하여이 경우 JPanel에있는 구성 요소의 위치를 ​​배치하고, 대신 최상위 컨테이너에 사용한다 (JFrame, JWindow, JDialog, 또는 JApplet.)하지 JPanel에 . 그래서 제거 :

table1.setBounds(100, 100, 150, 150); 

우리는 구성 요소를 배치하는 LayoutManager에서 제공하는이 LayoutManager

public class CafeView extends JFrame{ 

private JButton takeMoneyBtn = new JButton("Hesabı Al"); 

public CafeView(){ 
    JPanel table1 = new JPanel(); 
    this.setSize(800,600); 
    this.setLocation(600, 200); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setVisible(true); 

    //table1.setBounds(100, 100, 150, 150); 
    //table1.setLayout(null); 
    this.add(table1,BorderLayout.CENTER); 
    table1.add(takeMoneyBtn); 

} 
1

모든 컨트롤을 배치하고 내용 창에 직접 액세스하려면 LayoutManager를 사용합니다. How to use FlowLayout

public class CafeView extends JFrame{ 

private JButton takeMoneyBtn = new JButton("Hesabı Al"); 

public CafeView(){ 
    JPanel table1 = new JPanel(); 
    this.setSize(800,600); 
    this.setLocation(600, 200); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Container c = this.getContentPanel(); 
    c.setLayout(new FlowLayout()); 

    c.add(table1); 
    c.add(takeMoneyBtn); 
    //c.add() more stuff.. 

    this.setVisible(true); 
    } 
} 
+0

+1. 추가주의 사항. 스윙 GUI는 EDT에서 생성되고 업데이트되어야합니다. 자세한 내용은 [동시성의 동시성] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)을 참조하십시오. –