2014-09-02 87 views
0

我可以在GridBagLayout中向上或向後跨越按鈕嗎? 這裏,按鈕五的高度是2,寬度是1,但它不起作用。有一個空間向上,這是我想要按鈕五向上的空間。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

能否請您發表您預期的結果爲PIC? – 2014-09-02 03:52:46

+0

'gridWidth'和'gridHeight'跨越左/下分別... – MadProgrammer 2014-09-02 03:56:04

+0

我沒有足夠的信譽張貼圖片 – mhrzn 2014-09-02 03:58:41

回答

3

根據你的意見,你是通過你的工廠方法沒有意義

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行的座標...,然後跨越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 

會導致你的意見要求,例如...

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); 
    } 
}