将 JTable 安装在面板中
我正在使用 JTable 并将其添加到使用 gridbaglayout 的面板,如下所示:
JTable qdbs = new JTable(rowData, columnNamesVector);
qdbs.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
panel.add(qdbs, c);
我不希望表格位于滚动窗格中,但我确实希望表格占据面板的整个宽度。我将如何实现这个目标?
根据要求提供 SSCCE:
import javax.swing.*;
import java.awt.*;
public class Main{
public static void main(String[] args) {
new TestFrame();
}
public static class TestFrame extends JFrame{
public TestFrame() {
this.setTitle("SSCCE");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,10,10);
JTable testTable = new JTable(10,2);
panel.add(testTable, c);
this.add(panel);
this.pack();
this.setVisible(true);
}
}
}
我希望该表格始终占据面板的整个宽度(插图除外)。目前,当调整框架大小时,表格的大小不会改变。
I'm using a JTable and adding it to a panel which uses a gridbaglayout like so:
JTable qdbs = new JTable(rowData, columnNamesVector);
qdbs.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
panel.add(qdbs, c);
I don't want the table to be in a scroll pane, but I do want the table to take up the entire width of the panel. How would I accomplish this?
An SSCCE as requested:
import javax.swing.*;
import java.awt.*;
public class Main{
public static void main(String[] args) {
new TestFrame();
}
public static class TestFrame extends JFrame{
public TestFrame() {
this.setTitle("SSCCE");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,10,10);
JTable testTable = new JTable(10,2);
panel.add(testTable, c);
this.add(panel);
this.pack();
this.setVisible(true);
}
}
}
I would like this table to always take up the entire width of the panel (except the insets). Currently the table does not change size when the frame is resized.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要添加约束来告诉布局如何处理更多空间。在您的 SSCCE 中添加以下项目:
You need to add constrains to tell the layout what to do with more space. In your SSCCE add these items:
如果您需要更多帮助,请发布说明问题的 SSCCE。
If you need more help post a SSCCE that demonstrates the problem.
我想
c
是一个GridBagConstraint
或者类似的东西?最简单的方法是将JPanel
的LayoutManager
设置为BorderLayout
,然后添加约束BorderLayout .CENTER
.c
is aGridBagConstraint
or something along those lines, I imagine? The very simplest thing to do would be to set theLayoutManager
of theJPanel
to aBorderLayout
, then just add with the constraintBorderLayout.CENTER
.嗯,
Alex Bliskovsky 写了
panel.add(qdbs, c);
这是错误的,不,永远不要这样做,你忘了包裹你 JTable 到 ScrollPane 然后你就可以使用一些 LayoutManagers,有关 LayoutManagers 的相关示例,请检查 GridBagConstraints for GrigBagLayout
hmmm
Alex Bliskovsky wrote
panel.add(qdbs, c);
that's wrong, not, never do that, you are forgot wrap you JTable to the ScrollPane and then you can play with some of LayoutManagers, for related examples for LayoutManagers check GridBagConstraints for GrigBagLayout
当调整框架大小时,以下框架将正确调整表格大小。
}
The following frame will correctly resize the table when the frame is resized.
}
也许与:
Perhaps with: