2012-10-31 5 views
2

아무도 내가이 자바 스윙 GUI 코드로 잘못 가고있다 지적 할 수 있습니다. 나는 JPanel에 두 개의 버튼을 추가하고 크기를 설정 한 후 프레임에 추가하려고하지만 즉각적인 질문에 대답하지 않을 수 있습니다 그것은자바 스윙 설정 JPanel 크기

public Test() { 
    GridLayout layout = new GridLayout(1, 2); 
    //this.setLayout(layout); 
    this.setSize(700, 700); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    buttonPanel = new JPanel(); 
    buttonPanel.setSize(new Dimension(30, 100)); 

    JButton rectButton = new JButton("Rectangle"); 
    JButton ovalButton = new JButton("Oval"); 
    buttonPanel.add(rectButton); 
    buttonPanel.add(ovalButton); 
    this.add(buttonPanel); 
    this.add(new PaintSurface()); 
    this.setVisible(true); 
} 
+1

귀하의 코드를 확인하지 않습니다 많은 감각. GridLayout을 사용하고 있지만 BorderLayout.CENTER에 PaintSurface를 추가하고 있습니다. 크기를 설정하면 안됩니다. 레이아웃을 통해 레이아웃하는 구성 요소의 크기에 따라 적절한 크기가 결정됩니다. –

+0

그 편집도 좋지 않았습니다. –

답변

4

이 전달 된 setSize 값에 응답하지 않을 것으로 보인다. ..하지만 ...

GridLayout layout = new GridLayout(1, 2); 
this.setLayout(layout); 
// You're original code... 
// Why are you using `BorderLayout.CENTER` on a `GridLayout` 
this.add(new PaintSurface(), BorderLayout.CENTER); 

당신은 GridLayout로 레이아웃을 설정,하지만 당신은 구성 요소 중 하나를 적용 할 BorderLayout 제약을 사용하는 ?? 이 (질문 변경에서) 업데이트 setSize

의 값을 대체하므로

또한,

이 기본을 기억 코드에서 다른 Test#pack에 대한 호출이없는 것을 확인 JFrame의 레이아웃 관리자는 BorderLayout이므로 buttonPanel.setSize으로 전화를 걸어도 어쨌든 레이아웃 관리자가 우선 적용을 시작한 것 같습니다.

귀하의 요구 사항에 가장 적합한 레이아웃 관리자를 찾으려면 A Visual Guide to Layout ManagersUsing Layout Managers을 읽어야합니다.

하나가없는 경우 레이아웃 관리자가 다른 복합 요소를 사용하여 레이아웃을 달성하려는 목표에 더 가깝게 만듭니다.

1

업데이트 된 답변에 따르면 레이아웃에 아무 것도 설정하지 않은 것입니다.

어쨌든 LayoutManager를 사용해야한다면 (실제로해야 할) setSize()/setBounds()/setLocation()을 호출하는 것은 무의미합니다. LayoutManager (실제로는 해당 작업)에 의해 재정의 될 것이기 때문입니다.

과 같은 제약 조건이 개 구성 요소를 추가 this.add(buttonPanel); this.add(new PaintSurface());를 호출하여, 당신의 Test 클래스 JFrame를 확장 추측 컨텐츠 창에 (BorderLayout 때문에, BorderLayout.CENTER하면 JFrame의 컨텐츠 구획의 기본 LayoutManager에있다).

LayoutManager tutorial을 읽어보십시오. 그냥 정보

, 지금까지 완벽한에서,이 뭔가 "작업"을 보여줍니다 있지만 :

import java.awt.Dimension; 
import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Test extends JFrame { 
    private JPanel buttonPanel; 

    public class PaintSurface extends JButton { 
     public PaintSurface() { 
      super("Paint surface dummy"); 
     } 
    } 

    public Test() { 
     GridLayout layout = new GridLayout(1, 2); 
     this.setLayout(layout); 
     this.setSize(700, 700); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     buttonPanel = new JPanel(); 
     buttonPanel.setSize(new Dimension(30, 100)); 

     JButton rectButton = new JButton("Rectangle"); 
     JButton ovalButton = new JButton("Oval"); 
     buttonPanel.add(rectButton); 
     buttonPanel.add(ovalButton); 
     this.add(buttonPanel); 
     this.add(new PaintSurface()); 
     this.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Test(); 
      } 
     }); 
    } 
} 
1

좋아, 난 그냥 당신에게 해결책을주지 :

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Cobie extends JFrame{ 
    JButton rectButton = new JButton("Rectangle"); 
    JButton ovalButton = new JButton("Oval"); 

    JPanel buttonPanel = new JPanel(); 
    JPanel paintSurface = new JPanel(); 

    public Cobie(){ 
     setLayout(new GridLayout(2,1)); 
     buttonPanel.setBackground(Color.RED); 
     paintSurface.setBackground(Color.BLUE); 
     buttonPanel.add(rectButton); 
     buttonPanel.add(ovalButton); 
     add(buttonPanel); 
     add(paintSurface); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable(){ 
      public void run(){ 
       Cobie c = new Cobie(); 
       c.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       c.setSize(600,400); //Avoid using this method 
       c.setVisible(true); 
      } 
     }); 

    } 
}