2011-05-26 11 views
5

Kullanarak bir panelde ayırıcı nasıl çizilir Bu bir MigLayout-newbie sorusudur. Bir etiketin sonundan bir panelin genişliği boyunca bir ayırıcının nasıl çizileceğini bilmek isterim.MigLayout

İşte benim örnek kod:

package com.ndh.swingjunk; 

import java.awt.*; 
import javax.swing.*; 
import net.miginfocom.swing.MigLayout; 

class MyJFrame extends JFrame { 
    public MyJFrame() { 
     super(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JLabel label = new JLabel("foo"); 
     label.setFont(new Font("Tahoma", Font.BOLD, 11)); 
     JSeparator separator = new JSeparator(); 
     JLabel anotherLabel = new JLabel("some other label"); 
     anotherLabel.setFont(new Font("Tahoma", Font.PLAIN, 11)); 
     JButton button1 = new JButton("button 1"); 
     JButton button2 = new JButton("button 2"); 
     JPanel panel = new JPanel(new MigLayout()); 
     panel.add(label); 
     panel.add(separator, "growx, wrap"); 
     panel.add(anotherLabel); 
     panel.add(button1); 
     panel.add(button2); 
     getContentPane().add(panel); 
     pack(); 
    } 
} 

public class SeparatorLayoutQuestion { 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override public void run() {new MyJFrame().setVisible(true);}});}} 

İşte sonuç: korkunç topal

image of messed-up layout

. Ben panelin sonuna "Foo" sonundan germek için ayırıcı, düğme öbür istiyorum 2.

cevap

6

için

"span 3" 

ekleyerek İşte benim için çalışılan, etiket ve ayırıcının aynı hücreyi paylaşabilmesi için "bölme" kullanmak zorundaydım:

JPanel panel = new JPanel(new MigLayout()); 
panel.add(label, "split 2, span"); 
panel.add(separator, "growx, wrap"); 
panel.add(anotherLabel); 
panel.add(button1); 
panel.add(button2);