2014-09-02 6 views
0

GridBagLayout에서 버튼을 위 또는 뒤로 스팬 할 수 있습니까? 버튼 5 높이가 2이고 너비가 1이지만 작동하지 않습니다. 위쪽에 공간이 있습니다. 여기는 버튼 5가 위쪽으로 확장되기를 원합니다.GridBagLayOut 열을 위쪽으로 확장합니까?

public class GridBag2 extends JPanel 
{ 
    GridBagConstraints constraints = new GridBagConstraints(); 

    public static void main (String args []) 
    { 
     JFrame fr = new JFrame("GridBag2 Span R&C"); 
     fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE); 
     fr.setSize(600,600); 
     fr.add(new GridBag2()); 
     fr.setVisible(true); 

    } 

    GridBag2() 
    { 
     setLayout(new GridBagLayout()); 
     constraints.fill=GridBagConstraints.BOTH; 
     addGB(new JButton("one"),0,0,1.0,1.0,1,1); 
     addGB(new JButton("two"),1,0,1.0,1.0,1,1); 
     addGB(new JButton("three"),2,0,1.0,1.0,1,1); 
     addGB(new JButton("four"),1,1,1,1,1,2); //spans 2 rows 
     addGB(new JButton("five"),0,2,1,1,2,1); //spans 2 columns 

    } 

    void addGB(Component comp,int gx, int gy, double wx, double wy, int h, int w) 
    { 
     constraints.gridx=gx; 
     constraints.gridy=gy; 
     constraints.weightx=wx; 
     constraints.weighty=wy; 
     constraints.gridheight=h; 
     constraints.gridwidth=w; 
     this.add(comp,constraints); 
    } 
} 
+0

당신은 그림과 같이 예상 된 결과를 게시하시기 바랍니다 수 있을까요? –

+0

'gridWidth'와'gridHeight'는 각각 왼쪽/아래로 스팬합니다 ... – MadProgrammer

+0

그림을 게시하기에 충분한 평판이 있습니다 – mhrzn

답변

3

당신을 바탕으로 코멘트, 당신은 2 행에 걸쳐 할 말을하는지 기본적으로

addGB(new JButton("four"),1,1,1,1,1,2); //spans 2 rows 
addGB(new JButton("five"),0,2,1,1,2,1); //spans 2 columns 

당신이 이해가되지 않는 당신의 팩토리 메소드를 전달하는 좌표는 ..., 다음 2 걸쳐 열,하지만 매개 변수는 ...
addGB(new JButton("four"),1,1,1,1,2,1); //spans 2 rows 
addGB(new JButton("five"),0,2,1,1,1,2); //spans 2 columns 

는 귀하의 의견은 예를 들어, 무엇을 요구 초래 ... 라운드 잘못된 방법입니다

,536,913 63,210 GridBagLayout

import java.awt.Component; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class GridBag2 extends JPanel { 

    GridBagConstraints constraints = new GridBagConstraints(); 

    public static void main(String args[]) { 
     JFrame fr = new JFrame("GridBag2 Span R&C"); 
     fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE); 
     fr.setSize(600, 600); 
     fr.add(new GridBag2()); 
     fr.setVisible(true); 

    } 

    GridBag2() { 
     setLayout(new GridBagLayout()); 
     constraints.fill = GridBagConstraints.BOTH; 
     addGB(new JButton("one"), 0, 0, 1.0, 1.0, 1, 1); 
     addGB(new JButton("two"), 1, 0, 1.0, 1.0, 1, 1); 
     addGB(new JButton("three"), 2, 0, 1.0, 1.0, 1, 1); 
     addGB(new JButton("four"), 1, 1, 1, 1, 2, 1); //spans 2 rows 
     addGB(new JButton("five"), 0, 2, 1, 1, 1, 2); //spans 2 columns 

    } 

    void addGB(Component comp, int gx, int gy, double wx, double wy, int h, int w) { 
     constraints.gridx = gx; 
     constraints.gridy = gy; 
     constraints.weightx = wx; 
     constraints.weighty = wy; 
     constraints.gridheight = h; 
     constraints.gridwidth = w; 
     this.add(comp, constraints); 
    } 
} 

하지만, 잠깐, 두 번째/세 번째 행에 무슨 일이 있었는지? 두 번째 행은 기본적으로 축소되었으므로 더 이상 아무것도 포함되어 있지 않습니다. (실행 가능한 높이를 계산하는 데 사용할 수 있습니다)

결정을 내리는 데 도움이되는 "빈"구성 요소를 사용하면이 문제를 해결할 수 있습니다.

GridBagLayout

import java.awt.Component; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class GridBag2 extends JPanel { 

    GridBagConstraints constraints = new GridBagConstraints(); 

    public static void main(String args[]) { 
     JFrame fr = new JFrame("GridBag2 Span R&C"); 
     fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE); 
     fr.setSize(600, 600); 
     fr.add(new GridBag2()); 
     fr.setVisible(true); 

    } 

    GridBag2() { 
     setLayout(new GridBagLayout()); 
     constraints.fill = GridBagConstraints.BOTH; 
     addGB(new JButton("one"), 0, 0, 1.0, 1.0, 1, 1); 
     addGB(new JButton("two"), 1, 0, 1.0, 1.0, 1, 1); 
     addGB(new JButton("three"), 2, 0, 1.0, 1.0, 1, 1); 
     addGB(new JLabel(), 0, 1, 1, 1, 1, 1); //spans 2 rows 
     addGB(new JButton("four"), 1, 1, 1, 1, 2, 1); //spans 2 rows 
     addGB(new JButton("five"), 0, 2, 1, 1, 1, 2); //spans 2 columns 

    } 

    void addGB(Component comp, int gx, int gy, double wx, double wy, int h, int w) { 
     constraints.gridx = gx; 
     constraints.gridy = gy; 
     constraints.weightx = wx; 
     constraints.weighty = wy; 
     constraints.gridheight = h; 
     constraints.gridwidth = w; 
     this.add(comp, constraints); 
    } 
}