2012-10-29 64 views
3

非常簡單的問題:我如何消除包含兩個JCheckBox的兩個單元之間的垂直間隙?我用紅色邊框標出了圖片中的空白。如何清除MigLayout中兩個單元格之間的垂直間隙?

gap

這裏是代碼:

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

import net.miginfocom.swing.MigLayout; 

public class Main { 
    public static void main(String[] args) { 
     JPanel somePanel = new JPanel(); 
     somePanel.setLayout(new MigLayout("insets 0, debug", "", "")); 
     somePanel.add(new JCheckBox("first option"), "h 20!"); 
     somePanel.add(new JButton("click me"), "spany 2, h 40!, w 60%, wrap"); 
     somePanel.add(new JCheckBox("option two"), "h 20!"); 

     JFrame frame = new JFrame(); 
     frame.setContentPane(somePanel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

回答

3

最小差距在無論是在該行/列約束定義是否應特定行/只列之間施加他們:

new MigLayout("insets 0, debug", "", "[]0[]")); 

(琢磨了一下,這並不爲你工作很在這裏很好:)

或如果他們應該都行之間施加的layoutContraints:

new MigLayout("insets 0, gapy 0, debug")); 

BTW:佈局「編碼」應˚F遵循與所有編碼相同的規則,f.i.幹:-)特別是,我的規則是不重複組件約束,如果你可以實現佈局/行約束的目標。在這個例子中,你可以擺脫所有組件的約束,除了由跨越:

somePanel.setLayout(new MigLayout("insets 0, debug, wrap 2", 
     "[][60%, fill]", "[20!, fill]0")); 
somePanel.add(new JCheckBox("first option")); 
somePanel.add(new JButton("click me"), "spany 2"); 
somePanel.add(new JCheckBox("option two")); 
+0

非常感謝這個答案,這澄清了很多。到目前爲止,我從未使用行/列約束。好像我絕對必須考慮這一點。 :) – brimborium

2

好吧,我只是找到了一個很好的解決方案使用的對接:

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

import net.miginfocom.swing.MigLayout; 

public class Main { 
    public static void main(String[] args) { 
     JPanel somePanel = new JPanel(); 
     somePanel.setLayout(new MigLayout("insets 0, debug", "", "")); 
     somePanel.add(new JButton("click me"), "east"); 
     somePanel.add(new JCheckBox("first option"), "north"); 
     somePanel.add(new JCheckBox("option two"), "south"); 

     JFrame frame = new JFrame(); 
     frame.setContentPane(somePanel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

但我會怎麼做,如果對接不是一種選擇?

+2

刪除行約束(佈局)的差距 – kleopatra

+1

有趣的佈局管理器... – mKorbel

+0

@mKorbel MigLayout是非常好的,我發現。至少對於我來說。 ;) – brimborium

0

另一種解決方案是在細胞分裂成兩個子電池,將複選框出現,並應用組件缺口約束。

package com.zetcode; 

import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

import net.miginfocom.swing.MigLayout; 

/* 
Demonstrating component gaps in MigLayout manager. 
Author: Jan Bodnar 
Website: zetcode.com 
*/ 
public class MigLayoutGapsEx extends JFrame { 

    public MigLayoutGapsEx() { 

     initUI(); 
    } 

    private void initUI() { 

     JCheckBox cb1 = new JCheckBox("First option"); 
     JCheckBox cb2 = new JCheckBox("Second option"); 

     JButton btn = new JButton("Click me"); 

     createLayout(cb1, cb2, btn); 

     setTitle("MigLayout example"); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    private void createLayout(JComponent... arg) { 

     setLayout(new MigLayout()); 

     add(arg[0], "split 2, flowy"); 
     add(arg[1], "gapy 0"); 
     add(arg[2]); 

     pack(); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(() -> { 
      MigLayoutGapsEx ex = new MigLayoutGapsEx(); 
      ex.setVisible(true); 
     }); 
    } 
} 

下面是截圖:

Screenshot of the example

相關問題