2012-08-24 2 views
2

얼마 동안이 문제로 고생하고 있습니다. 내 방식은 다음과 같습니다.Java GridBagConstraints가 작동하지 않을 수 있습니다.

public Frame(){ 
      JFrame window = new JFrame(); 
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      window.setSize(800, 600); 

      JPanel panel = new JPanel(new GridBagLayout()); 

      GridBagConstraints gbc = new GridBagConstraints(); 
       gbc.gridheight =3; 
       gbc.gridwidth = 3; 


      JButton upButt = new JButton();//Buttons.upButton(); 
       gbc.gridx = 1; 
       gbc.gridy = 0; 
       panel.add(upButt, gbc); 

      JButton downButt = new JButton(); 
       gbc.gridx = 1; 
       gbc.gridy = 2; 
       panel.add(downButt, gbc); 

      JButton leftButt = new JButton();//Buttons.leftButton(); 
       gbc.gridx=0; 
       gbc.gridy = 1; 
       panel.add(leftButt, gbc); 

      JButton rightButt = new JButton();//Buttons.rightButton(); 
       gbc.gridx =2; 
       gbc.gridy = 1; 
       panel.add(rightButt, gbc); 

      window.add(panel); 
      window.setVisible(true); 

      } 

- Java 문서를 읽고 다시 읽은 후. - 이것은 나에게 십자 모양의 4 개의 버튼을 줄 것입니다. 그러나이 경우는 아니며 버튼이 창의 중앙에 서로 쌓여 있습니다. 내가 무엇이 누락 되었습니까?

답변

4

gridheight 및 gridwidth를 3으로 사용하는 이유는 무엇입니까? 적어도 말하기에는 조금 이상합니다. 내 돈 들어

, 나는 일을 단순화 것 간단한 GridLayout과 사용 : 내가 없애 줄

Cross my buttons

:

import java.awt.GridLayout; 
import javax.swing.*; 

public class Foo003 { 
    public static void main(String[] args) { 
     JButton upButton = new JButton("Up"); 
     JButton downButton = new JButton("Down"); 
     JButton leftButton = new JButton("Left"); 
     JButton rightButton = new JButton("Right"); 
     JComponent[][] components = { 
      { new JLabel(), upButton, new JLabel() }, 
      { leftButton, new JLabel(), rightButton }, 
      { new JLabel(), downButton, new JLabel() } }; 

     JPanel panel = new JPanel(new GridLayout(components.length, 
      components[0].length, 8, 8)); 
     for (int i = 0; i < components.length; i++) { 
     for (int j = 0; j < components[i].length; j++) { 
      panel.add(components[i][j]); 
     } 
     } 

     int eb = 15; 
     panel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb)); 

     JFrame frame = new JFrame("Grid e.g."); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(panel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 

enter image description here

+0

+1 * gridheight 및 gridwidth 3 * – MadProgrammer

+0

[이 답변보기] (http://stackoverflow.com/questions/10861852/add-a-complex-image-in-the-panel-with-buttons- around-it-in-one-customized-user/10862262 # 10862262) 3x3'GridLayout'을 번갈아 사용하는 또 다른 예제입니다. –

+1

+1 gridheight 및 gridwidth를 없애는 것이 ** 키 ** – Reimeus

2

당신은 같은 의미를 gridwidthgridheight

public class TestGridBagLayout extends JFrame { 

    public TestGridBagLayout() { 

     setTitle("Test"); 
     setLayout(new GridBagLayout()); 
     setSize(200, 200); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     add(createButton(), gbc); 

     gbc.gridy = 2; 
     add(createButton(), gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 1; 
     add(createButton(), gbc); 

     gbc.gridx = 2; 
     add(createButton(), gbc); 

     setVisible(true); 

    } 

    protected JButton createButton() { 

     return new JButton("+"); 

    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     new TestGridBagLayout(); 

    } 
} 
+0

1 + GridBagLayout 작동 방식에 대한 잘못된 가정하에 저를 수정했습니다. 좋은 간단한 코드. –

0

gridHeightgridWidth을 사용할 때 "이 구성 요소는 3 행 3 열을 차지합니다."라는 말이 있습니다. 자바 문서에서

: 여기

gridheight 
Specifies the number of cells in a column for the component's display area. 

gridwidth 
Specifies the number of cells in a row for the component's display area. 

는 "구성 요소"는 패널에 추가되는 구성 요소입니다. 귀하의 경우 버튼.

다른 포스터가 말하고 제거하고 gridWidth 및 gridHeight 선을 제거하면 모두 잘됩니다.