Which layoutmanager to use?
I'd like to create a J开发者_如何学JAVAPanel
in java with a layout similar to the one below. Any ideas?
Use a vertical BoxLayout and set the alignment of items on each row. (Make each row its own JPanel
)
Another option is to use a SpringLayout
or a GridBoxLayout
and setup spacing for each indentation.
Take a look at miglayout. With this layout manager, you will never have to ask which layout manager to use for a specific situation :-)
This is the implementation using jgoodies forms FormLayout (library from google):
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);
}
}
I have created an example with the GroupLayout
manager. Indented
components are created with the LayoutStyle.ComponentPlacement.INDENT
component placement type.
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 is the default layout manager for the GUI-design tool in NetBeans (and some other IDEs, if I'm not mistaken).
精彩评论