2011-04-22 62 views
2

我想在java中創建一個JPanel,其佈局類似於下面的佈局。有任何想法嗎?哪個layoutmanager使用?

enter image description here

+1

我的回答標題的所有問題將是['MiGLayout'(http://www.miglayout.com/);-) – 2011-04-22 12:04:40

+1

或JGoodies數據的FormLayout。 http://www.jgoodies.com/freeware/forms/ – MeBigFatGuy 2011-04-22 12:09:16

+2

或訪問[本地商店](http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html)。 – trashgod 2011-04-22 12:24:17

回答

8

使用垂直BoxLayout並設置在各行項目的定位。 (使每一行都是自己的JPanel

另一種選擇是使用SpringLayoutGridBoxLayout以及每個縮進的設置間距。

2

看看miglayout。有了這個佈局管理器,你將永遠不會有問的具體情況使用的佈局管理器:-)

1

這是使用jgoodies forms的FormLayout(庫從谷歌)的實現:

import com.jgoodies.forms.layout.CellConstraints; 
import com.jgoodies.forms.layout.FormLayout; 

import javax.swing.*; 
import java.awt.*; 

public class FormSample { 

    private static JPanel generatePanel() { 

     FormLayout layout = new FormLayout(
       "3dlu, 15dlu, max(50dlu;p), 2dlu, p, 3dlu", // column definition 
       "5dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 5dlu" // row definition 
     ); 

     JPanel panel = new JPanel(layout); 

     CellConstraints cc = new CellConstraints(); 
     int row = 0; 
     panel.add(new JLabel("Amplitude"), cc.xyw(2, row * 4 + 2, 4)); 
     panel.add(new JTextField(), cc.xy(3, row * 4 + 4)); 
     panel.add(new JLabel("V"), cc.xy(5, row * 4 + 4)); 
     row++; 
     panel.add(new JLabel("Off-set"), cc.xyw(2, row * 4 + 2, 4)); 
     panel.add(new JTextField(), cc.xy(3, row * 4 + 4)); 
     panel.add(new JLabel("V"), cc.xy(5, row * 4 + 4)); 
     row++; 
     panel.add(new JLabel("Frequentie"), cc.xyw(2, row * 4 + 2, 4)); 
     panel.add(new JTextField(), cc.xy(3, row * 4 + 4)); 
     panel.add(new JLabel("Hz"), cc.xy(5, row * 4 + 4)); 
     row++; 
     panel.add(new JLabel("Fase"), cc.xyw(2, row * 4 + 2, 4)); 
     panel.add(new JTextField(), cc.xy(3, row * 4 + 4)); 
     panel.add(new JLabel("rad"), cc.xy(5, row * 4 + 4)); 

     return panel; 
    } 


    public static void main(String[] args) { 
     // create frame 
     JFrame frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // add content panel 
     frame.setContentPane(generatePanel()); 

     // shring/expand to optimal size 
     frame.pack(); 

     // center frame 
     Toolkit toolkit = Toolkit.getDefaultToolkit(); 
     Dimension resolution = toolkit.getScreenSize(); 
     Dimension frameSize = frame.getSize(); 
     int xLocation = resolution.width/2 - frameSize.width/2; 
     int yLocation = resolution.height/2 - frameSize.height/2; 
     frame.setLocation(xLocation, yLocation); 

     // show frame 
     frame.setVisible(true); 
    } 
} 
-1

的GroupLayout是NetBeans中的GUI設計工具的默認佈局管理器(以及其他一些IDE,如果我沒有弄錯的話)。

0

我用GroupLayout管理器創建了一個示例。縮進 組件使用LayoutStyle.ComponentPlacement.INDENT 組件放置類型創建。

package com.zetcode; 

import java.awt.Container; 
import java.awt.EventQueue; 
import javax.swing.GroupLayout; 
import static javax.swing.GroupLayout.Alignment.BASELINE; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.LayoutStyle; 


public class GroupLayoutIndent extends JFrame { 

    public GroupLayoutIndent() { 

     initUI(); 

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

    private void initUI() { 

     Container pane = getContentPane(); 
     GroupLayout gl = new GroupLayout(pane); 
     pane.setLayout(gl); 

     JLabel ampLbl = new JLabel("Amplitude"); 
     JLabel offLbl = new JLabel("Off-set"); 
     JLabel freqLbl = new JLabel("Frequency"); 
     JLabel phsLbl = new JLabel("Phase"); 
     JLabel unit1 = new JLabel("V"); 
     JLabel unit2 = new JLabel("V"); 
     JLabel unit3 = new JLabel("Hz"); 
     JLabel unit4 = new JLabel("rad"); 

     JTextField field1 = new JTextField(12); 
     JTextField field2 = new JTextField(12); 
     JTextField field3 = new JTextField(12); 
     JTextField field4 = new JTextField(12); 

     gl.setAutoCreateGaps(true); 
     gl.setAutoCreateContainerGaps(true); 

     gl.setHorizontalGroup(gl.createParallelGroup() 
       .addComponent(ampLbl) 
       .addGroup(gl.createSequentialGroup() 
         .addPreferredGap(ampLbl, field1, 
           LayoutStyle.ComponentPlacement.INDENT) 
         .addComponent(field1) 
         .addComponent(unit1)) 
       .addComponent(offLbl) 
       .addGroup(gl.createSequentialGroup() 
         .addPreferredGap(offLbl, field2, 
           LayoutStyle.ComponentPlacement.INDENT) 
         .addComponent(field2) 
         .addComponent(unit2)) 
       .addComponent(freqLbl) 
       .addGroup(gl.createSequentialGroup() 
         .addPreferredGap(freqLbl, field3, 
           LayoutStyle.ComponentPlacement.INDENT) 
         .addComponent(field3) 
         .addComponent(unit3))  
       .addComponent(phsLbl) 
       .addGroup(gl.createSequentialGroup() 
         .addPreferredGap(phsLbl, field4, 
           LayoutStyle.ComponentPlacement.INDENT) 
         .addComponent(field4) 
         .addComponent(unit4))     
     ); 

     gl.setVerticalGroup(gl.createSequentialGroup() 
       .addComponent(ampLbl) 
       .addGroup(gl.createParallelGroup(BASELINE) 
         .addComponent(field1) 
         .addComponent(unit1)) 
       .addComponent(offLbl) 
       .addGroup(gl.createParallelGroup(BASELINE) 
         .addComponent(field2) 
         .addComponent(unit2)) 
       .addComponent(freqLbl) 
       .addGroup(gl.createParallelGroup(BASELINE) 
         .addComponent(field3) 
         .addComponent(unit3)) 
       .addComponent(phsLbl) 
       .addGroup(gl.createParallelGroup(BASELINE) 
         .addComponent(field4) 
         .addComponent(unit4)) 
     ); 

     gl.linkSize(field1, field2, field3, field4); 

     pack(); 
    } 

    public static void main(String[] args) { 

     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       GroupLayoutIndent ex = new GroupLayoutIndent(); 
       ex.setVisible(true); 
      } 
     }); 
    } 
} 

GroupLayout indent