我遇到与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
答案 0 :(得分:1)
根据需要使用不同的布局管理器来使用多个容器
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