我想在java中创建一个JPanel
,其布局类似于下面的布局。有什么想法吗?
答案 0 :(得分:8)
使用垂直BoxLayout并设置每行上的项目对齐方式。 (使每一行都有自己的JPanel
)
另一种选择是为每个缩进使用SpringLayout
或GridBoxLayout
和设置间距。
答案 1 :(得分:2)
看看miglayout。使用此布局管理器,您将永远不必询问在特定情况下使用哪个布局管理器: - )
答案 2 :(得分: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);
}
}
答案 3 :(得分: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);
}
});
}
}
答案 4 :(得分:-1)
GroupLayout是NetBeans中GUI设计工具的默认布局管理器(以及其他一些IDE,如果我没记错的话)。