将 JList 插入 GridLayout
insert JList into GridLayout
我有一个与 JList
有关的问题。我得到以下布局。
JTextField | Button 1 | Button 2
Button 3 | Button 4 | Button 5
这是我得到的布局:
JTextField | Button 1 | Button 2
Button 3 | Button 4 | Button 5
如您所见,我没有 JList
介于两者之间。我确实尝试在那里插入一个列表组件,但是当我这样做时,GridLayout
会将列表推到按钮 2 旁边。我只希望列表在它们之间是我自己。
package ManyThings;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
javax.swing.JTextField;
public class test {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 3));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,1));
JTextField t1;
t1=new JTextField();
p1.add(new JTextField());
p1.add(new JButton("Search"));
p1.add(new JButton("Clear"));
frame.add(p1);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,3));
p2.add(new JButton("Add"));
p2.add(new JButton("Detail"));
p2.add(new JButton("Remove"));
frame.add(p2);
frame.pack();
frame.setVisible(true);
}
}
期待以下布局。
JTextField | Button 1 | Button 2
JList
Button 3 | Button 4 | Button 5
复合布局
使用多个容器,使用不同的布局管理器(根据需要)
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
我有一个与 JList
有关的问题。我得到以下布局。
JTextField | Button 1 | Button 2
Button 3 | Button 4 | Button 5
这是我得到的布局:
JTextField | Button 1 | Button 2
Button 3 | Button 4 | Button 5
如您所见,我没有 JList
介于两者之间。我确实尝试在那里插入一个列表组件,但是当我这样做时,GridLayout
会将列表推到按钮 2 旁边。我只希望列表在它们之间是我自己。
package ManyThings;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
javax.swing.JTextField;
public class test {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 3));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,1));
JTextField t1;
t1=new JTextField();
p1.add(new JTextField());
p1.add(new JButton("Search"));
p1.add(new JButton("Clear"));
frame.add(p1);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,3));
p2.add(new JButton("Add"));
p2.add(new JButton("Detail"));
p2.add(new JButton("Remove"));
frame.add(p2);
frame.pack();
frame.setVisible(true);
}
}
期待以下布局。
JTextField | Button 1 | Button 2
JList
Button 3 | Button 4 | Button 5
复合布局
使用多个容器,使用不同的布局管理器(根据需要)
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