2014-04-14 2 views
0

저는 Java에서 새로운 기능으로 GridBag-Layout을 이해하기 어렵습니다. 내가하고 싶은 것은 버튼 1을 3 개의 버튼 너비로 리사이즈하는 것입니다. 즉, 버튼 1의 음영은 x = 0, y = 0 좌표에서 시작하여 버튼 3 (x = 0, y = 3)의 끝에서 끝납니다.Java에서 GridBagLayout에 gridwidth가있는 JButton의 크기를 올바르게 조정하지 못했습니다.

package footballQuestioner; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingConstants; 

public class asd { 

    public static void main(String[] args) { 
     JFrame frame = new Houdini(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
//  frame.setSize(250, 250); 
     System.out.println(frame.getWidth()); 
     frame.setResizable(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 


class Houdini extends JFrame { 

    private JButton one=new JButton("one"); 
    private JButton two=new JButton("two"); 
    private JButton three=new JButton("three"); 
    private JButton four=new JButton("four"); 
    private JButton five=new JButton("five"); 

    private GridBagConstraints gbc=new GridBagConstraints(); 
    private JPanel panel=new JPanel(new GridBagLayout()); 

    public Houdini() { 



     //  Insets insets=new Insets(30, 0, 30, 0); 
//  gbc.insets=insets; 

     gbc.gridx=0; 
     gbc.gridy=0; 


     gbc.gridwidth=1; 
     gbc.gridheight=1; 


     gbc.fill=0; 

     panel.add(one,gbc); 


     gbc.gridx=1; 
     gbc.gridy=1; 

     gbc.gridwidth=1; 
     gbc.gridheight=1; 


     gbc.fill=0; 

     panel.add(two,gbc); 


     gbc.gridx=2; 
     gbc.gridy=2; 

     gbc.gridwidth=1; 
     gbc.gridheight=2; 

     gbc.fill=0; 

     panel.add(three,gbc); 


//  gbc.gridx=3; 
//  gbc.gridy=3; 
//  
//  gbc.gridwidth=-4; 
//  gbc.gridheight=1; 
//  
// 
//  gbc.fill=GridBagConstraints.HORIZONTAL; 
//  
//  panel.add(four,gbc); 

     panel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 

     add(panel); 

    } 
} 

그것은 다음과 같아야합니다 : 여기

코드의

enter image description here

답변

2

당신 말은 내가 사용하는 것이 같은 더 ...

enter image description here

...

gbc.gridx = 0; 
gbc.gridy = 0; 

gbc.gridwidth = 3; 
gbc.gridheight = 1; 

gbc.fill = 0; 

panel.add(one, gbc); 

또는 내가 사용

enter image description here

...

gbc.gridx = 0; 
gbc.gridy = 0; 

gbc.gridwidth = 3; 
gbc.gridheight = 1; 

gbc.fill = GridBagConstraints.HORIZONTAL; 

panel.add(one, gbc); 

달성하기 위해?

당신이해야 할 일은

문제는이 압축 된 왜이는 ... 열 0은 폭이없는, GridBagLayout의 관점에서입니다 ... 업데이트

For example

업데이트 ... 그것으로 공간을 넣어하게됩니다 열로 필러 구성 요소의 어떤 종류를 추가 속임수 당신이, 당신은 또한 부정 행위를 할 수 달성 할 무엇인지에 따라

과)

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridLayout; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class TestLayout { 

    public static void main(String[] args) { 
     JFrame frame = new Houdini(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
//  frame.setSize(250, 250); 
     System.out.println(frame.getWidth()); 
     frame.setResizable(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static class Houdini extends JFrame { 

     private JButton one = new JButton("one"); 
     private JButton two = new JButton("two"); 
     private JButton three = new JButton("three"); 
     private JButton four = new JButton("four"); 
     private JButton five = new JButton("five"); 

     private GridBagConstraints gbc = new GridBagConstraints(); 
     private JPanel panel = new JPanel(new BorderLayout()); 

     public Houdini() { 

      JPanel top = new JPanel(new BorderLayout()); 
      top.add(one); 
      JPanel center = new JPanel(new GridLayout(0, 3)); 

      center.add(empty()); 
      center.add(two); 
      center.add(empty()); 
      center.add(empty()); 
      center.add(empty()); 
      center.add(three); 

      panel.add(top, BorderLayout.NORTH); 
      panel.add(center); 

      panel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 

      add(panel); 

     } 

     private JComponent empty() { 
      JPanel panel = new JPanel(); 
      return panel; 
     } 
    } 
} 
+0

내가 GridBagLayout을 사용하려고 마지막 시간이 10 배 이상이었다 맹세 이것보다 복잡한 ... – Matthew

+1

@Human Nothings 변경 ... 그냥 당신의 이해;). 15 년 넘게'GridBagLayout'을 사용하고 있었고 여전히 새로운 것을 가르쳐주고 있습니다.) – MadProgrammer

+0

아, 이제 그 이유를 기억합니다. 나는 상단에 스트레칭 버튼 1, 센터에 버튼 2, 하단 오른쪽에 버튼 3을 사용하여 3 x 3 격자를 얻으려고합니다. 그 일에 대해 어떻게 생각하세요? 그것이 OP가 원했던 것처럼 보입니다. – Matthew

관련 문제