gpt4 book ai didi

java - 无法在 Swing 中水平显示 2 个 JTable

转载 作者:行者123 更新时间:2023-11-29 03:43:49 25 4
gpt4 key购买 nike

我需要沿 x 轴显示 2 个 JTable。我能够垂直显示它们(Y 轴)。这是我目前所做的:

Displaying Two tables.

但我想显示如下表格,

enter image description here

这是我的代码:

    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 View 真的很糟糕。

最佳答案

是这样的吗??

Layout

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);

关于java - 无法在 Swing 中水平显示 2 个 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11916830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com