我需要沿x轴显示2 JTable
。我可以垂直显示它们(Y轴。)这是我到目前为止所做的:
但我想显示如下表格,
这是我的代码:
tableA = new JTable(data, colNames);
tableB = new JTable(data, colNames);
JLabel labelA = new JLabel("Table-A");
JLabel labelB = new JLabel("Table-B");
JButton bt_copy = new JButton("Copy");
Container c = frame.getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
c.add(labelA);
c.add(tableA.getTableHeader());
c.add(tableA);
c.add(labelB);
c.add(tableB.getTableHeader());
c.add(tableB);
c.add(bt_copy);
当我将 c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
中的Y轴更改为 X轴时。我的GUI视图确实很糟糕。
答案 0 :(得分:2)
我建议使用GridBagLayout
代替BoxLayout
。而不是将所有内容添加到JFrame
本身,请尝试向框架添加JPanel
并将表格添加到框架中。 (这意味着您可以将面板的布局设置为GridBagLayout
)。
如果您不熟悉布局管理器,请尝试阅读Visual Guide To Layout manager。这是非常有用的信息。
答案 1 :(得分:2)
这样的东西?
JTable leftTable = new JTable();
JTable rightTable = new JTable();
addButton = new JButton("Add >>");
removeButton = new JButton("<< Remove");
setLayout(new GridBagLayout());
// Prepare the buttons panel...
JPanel pnlActions = new JPanel(new GridBagLayout());
pnlActions.setBorder(new LineBorder(Color.RED));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
pnlActions.add(addButton, gbc);
gbc.weighty = 0;
gbc.gridy++;
pnlActions.add(removeButton, gbc);
// Prepare the main layout
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.33;
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
add(new JScrollPane(leftTable), gbc);
gbc.gridx = 2;
add(new JScrollPane(rightTable), gbc);
gbc.gridx = 1;
gbc.gridy++;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 0;
add(pnlActions, gbc);
答案 2 :(得分:0)
尝试使用box作为contentPane。您需要使用组合框来获得所需的所需布局。这是一个可以让你接近的例子:
tableA = new JTable(data, colNames);
tableB = new JTable(data, colNames);
JLabel labelA = new JLabel("Table-A");
JLabel labelB = new JLabel("Table-B");
JButton bt_copy = new JButton("Copy");
Box v = Box.createVerticalBox();
frame.setContentPane(v);
Box c = Box.createHorizontalBox();
v.add(c);
JScrollPane jsp = new JScrollPane(tableA);
c.add(jsp);
jsp = new JScrollPane(tableB);
c.add(jsp);
Box c2 = Box.createHorizontalBox();
c2.add(Box.createHorizontalGlue());
c2.add(bt_copy);
c2.createHorizontalGlue();
v.add(c2);