java 在面板内安装 JTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6050708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fitting a JTable Inside a Panel
提问by Alex Bliskovsky
I'm using a JTable and adding it to a panel which uses a gridbaglayout like so:
我正在使用 JTable 并将其添加到使用 gridbaglayout 的面板中,如下所示:
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:
应要求的 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 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.
我希望这张桌子始终占据面板的整个宽度(插图除外)。当前,当调整框架大小时,表格不会改变大小。
回答by jzd
You need to add constrains to tell the layout what to do with more space. In your SSCCE add these items:
您需要添加约束来告诉布局如何使用更多空间。在您的 SSCCE 中添加以下项目:
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
回答by camickr
import java.awt.*;
import javax.swing.*;
public class Test
{
public static void main(String args[]) throws Exception
{
JPanel panel = new JPanel( new GridBagLayout() );
JTable table = new JTable(5, 5);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(table, gbc);
JFrame frame = new JFrame();
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
If you need more help post a SSCCEthat demonstrates the problem.
如果您需要更多帮助,请发布说明问题的SSCCE。
回答by mKorbel
hmmm
嗯嗯
Alex Bliskovsky wrote panel.add(qdbs, c);
亚历克斯·布利斯科夫斯基写道 panel.add(qdbs, c);
that's wrong, not, never do that, you are forgot wrap you JTableto the ScrollPaneand then you can play with some of LayoutManagers, for related examples for LayoutManagers check GridBagConstraintsfor GrigBagLayout
那是错误的,不,永远不要那样做,您忘记将JTable包装到ScrollPane然后您可以使用一些LayoutManagers,有关LayoutManagers的相关示例,请检查GridBagConstraintsfor GrigBagLayout
回答by Ernest Friedman-Hill
c
is a GridBagConstraint
or something along those lines, I imagine? The very simplest thing to do would be to set the LayoutManager
of the JPanel
to a BorderLayout
, then just add with the constraint BorderLayout.CENTER
.
c
是一个GridBagConstraint
或类似的东西,我想?做的很简单的事情将是设定LayoutManager
的JPanel
到BorderLayout
,那么就用约束添加BorderLayout.CENTER
。
回答by JohnnyO
The following frame will correctly resize the table when the frame is resized.
调整框架大小时,以下框架将正确调整表格的大小。
public class Sandbox {
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);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
JTable testTable = new JTable(10, 2);
panel.add(testTable, c);
this.add(panel);
this.pack();
this.setVisible(true);
}
}
}
}
回答by OscarRyz
Perhaps with:
也许与:
GridBagConstraints.fill = GridBagContraints.BOTH;