[英]Swing: Place jTables and jButtons
I have a jFrame with 2 jTables (inserted in 2 jScrollPanes). 我有一个带有2个jTable的jFrame(插入2个jScrollPanes中)。 Then, I have 3 jButtons for each jTable. 然后,每个jTable有3个jButton。 How can I place them to have the following result: 如何放置它们以获得以下结果:
I don't know very well what Layout to use to manage it. 我不太清楚要使用哪种布局来管理它。 Thanks! 谢谢!
I have this, but I can't see the buttons: 我有这个,但是看不到按钮:
JButton addButton1 = new JButton();
JButton deleteButton1 = new JButton();
JButton playButton1 = new JButton();
JButton addButton2 = new JButton();
JButton deleteButton2 = new JButton();
JButton playButton2 = new JButton();
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List 1"));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List 2"));
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(addButton1);
panel3.add(deleteButton1);
panel3.add(playButton1);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(addButton2);
panel4.add(deleteButton2);
panel4.add(playButton2);
JScrollPane tableContainer1 = new JScrollPane(table1);
panel1.add(tableContainer1, BorderLayout.CENTER);
JScrollPane tableContainer2 = new JScrollPane(table2);
panel2.add(tableContainer2, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Edit: Gah! 编辑:加! Someone beat me to it...Here's a slightly different approach regardless! 有人击败了我...这里的方法稍有不同!
There's many ways to do this, but I would try the following: 有很多方法可以做到这一点,但是我会尝试以下方法:
JFrame
> BoxLayout
using X_AXIS
使用X_AXIS
JFrame
> BoxLayout
JPanel
#1 > BorderLayout
JPanel
#1> BorderLayout
BorderLayout.NORTH
] JPanel
for buttons > FlowLayout
using FlowLayout.LEFT
[ BorderLayout.NORTH
] JPanel
for FlowLayout
>使用FlowLayout.LEFT
BorderLayout.CENTER
] JScrollPane
with Table #1 [ BorderLayout.CENTER
]具有表#1的JScrollPane
JPanel
#2 > BorderLayout
JPanel
#2> BorderLayout
BorderLayout.NORTH
] JPanel
for buttons > FlowLayout
using FlowLayout.LEFT
[ BorderLayout.NORTH
] JPanel
for FlowLayout
>使用FlowLayout.LEFT
BorderLayout.CENTER
] JScrollPane
with Table #2 [ BorderLayout.CENTER
] JScrollPane
与表#2 Using BoxLayout
and BorderLayout.CENTER
will ensure that the tables resize with the frame and fill up as much space as they can. 使用BoxLayout
和BorderLayout.CENTER
将确保表格根据框架调整大小并尽可能多地填充空间。 Here's a simple example: 这是一个简单的例子:
public class TwoTableJFrameTest extends JFrame
{
public TwoTableJFrameTest()
{
setTitle("Two Table Layout");
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
JPanel table1Panel = new JPanel(new BorderLayout(5, 5));
JPanel table1ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
table1ButtonPanel.add(new JButton("Button 1"));
table1ButtonPanel.add(new JButton("Button 2"));
table1ButtonPanel.add(new JButton("Button 3"));
JTable table1 = new JTable(new DefaultTableModel(new Object[]{"Column 1", "Column 2"}, 10));
table1Panel.add(table1ButtonPanel, BorderLayout.NORTH);
table1Panel.add(new JScrollPane(table1));
JPanel table2Panel = new JPanel(new BorderLayout(5, 5));
JPanel table2ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
table2ButtonPanel.add(new JButton("Button 1"));
table2ButtonPanel.add(new JButton("Button 2"));
table2ButtonPanel.add(new JButton("Button 3"));
JTable table2 = new JTable(new DefaultTableModel(new Object[]{"Column 1", "Column 2"}, 10));
table2Panel.add(table2ButtonPanel, BorderLayout.NORTH);
table2Panel.add(new JScrollPane(table2));
add(table1Panel);
add(table2Panel);
pack();
}
}
Steps: 脚步:
This is why you couldn't see the buttons, you forgot to do this: 这就是为什么您看不到按钮,却忘记这样做的原因:
panel1.add(panel3);
panel2.add(panel4);
Anyway, here's the working code: 无论如何,这是工作代码:
JButton addButton1 = new JButton();
JButton deleteButton1 = new JButton();
JButton playButton1 = new JButton();
JButton addButton2 = new JButton();
JButton deleteButton2 = new JButton();
JButton playButton2 = new JButton();
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List 1"));
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List 2"));
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(addButton1);
panel3.add(deleteButton1);
panel3.add(playButton1);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(addButton2);
panel4.add(deleteButton2);
panel4.add(playButton2);
panel1.add(panel3);
panel2.add(panel4);
JScrollPane tableContainer1 = new JScrollPane(table1);
panel1.add(tableContainer1, BorderLayout.CENTER);
JScrollPane tableContainer2 = new JScrollPane(table2);
panel2.add(tableContainer2, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
PS 聚苯乙烯
You'll notice I changed the layout of panel1
& panel2
from FlowLayout
to a Y-Axis BoxLayout
. 您会注意到我将panel1
和panel2
的布局从FlowLayout
更改为Y-Axis BoxLayout
。 This is because the buttons appeared beside the tables, not above. 这是因为按钮显示在表格旁边而不是上方。 Changing the layout to a Y-Axis BoxLayout
fixed that. 将布局更改为Y-Axis BoxLayout
解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.