复合布局
根据需要使用不同的布局管理器来使用多个容器
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Test {
public static void main(String args[]) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(3,1));
JPanel topRow = new JPanel(new GridLayout(1,3));
topRow.add(new JTextField(10));
topRow.add(new JButton("Search"));
topRow.add(new JButton("Clear"));
add(topRow);
add(new JScrollPane(new JList()));
JPanel bottomRow = new JPanel(new GridLayout(1,3));
bottomRow.add(new JButton("Add"));
bottomRow.add(new JButton("Detail"));
bottomRow.add(new JButton("Remove"));
add(bottomRow);
}
}
}
不同的布局管理器
使用不同的布局管理器来更好地满足各个组件的要求
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Test {
public static void main(String args[]) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
JPanel topRow = new JPanel(new GridLayout(1,3));
topRow.add(new JTextField(10));
topRow.add(new JButton("Search"));
topRow.add(new JButton("Clear"));
JPanel bottomRow = new JPanel(new GridLayout(1,3));
bottomRow.add(new JButton("Add"));
bottomRow.add(new JButton("Detail"));
bottomRow.add(new JButton("Remove"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(topRow,gbc);
gbc.gridy = 2;
add(bottomRow,gbc);
gbc.gridy = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(new JList()),gbc);
}
}
}
有关更多详细信息,请参见Laying Out Components Within a Container
本文链接:https://www.f2er.com/3148777.html