2

我有一个JPanel. GridBagLayout该面板包含 2 行,第一行有一个 JLabel,第二行有一个JScrollPanewith JTableinside。该表未填充 100% 的滚动窗格。即使我调整了框架的大小,滚动窗格也会调整大小,但里面的表格总是有固定的宽度。

JTable table=new JTable(myModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
JScrollPane scroll=new JScrollPane(table);

JPanel panel=new JPanel(new GridBagLayout());
// component add details skipped

以下是添加到面板时应用于滚动窗格的网格包约束。

GridBagConstraints gbc=new GridBagConstraints();
gbc.gridx=0; // first column
gbc.gridy=1; // second row
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.BOTH;
gbc.anchor=GridBagConstraints.NORTHEAST;
gbc.weightx=1.0;
gbc.weighty=1.0;

什么地方出了错?问题出在滚动窗格或表格上?

4

3 回答 3

8

我认为只有(注意行永远不会通过使用任何 LayoutManager 来调整大小,只有列)

c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;

GBC 不适合 JComponents 的 LayoutManager 实现 Scrollable,对这些 JComponents 使用 BorderLayout(ev GridLayout)

例如

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class JTableAndGBC {

    private String[] columnNames = {"Source", "Hit", "Last", "Ur_Diff"};
    private JTable table;
    private Object[][] data = {{"Swing Timer", 2.99, 5, 1.01},
        {"Swing Worker", 7.10, 5, 1.010}, {"TableModelListener", 25.05, 5, 1.01}};
    private DefaultTableModel model = new DefaultTableModel(data, columnNames);

    public JTableAndGBC() {
        JPanel panel = new JPanel(new GridBagLayout());
        table = new JTable(model);
        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);
    }

    public static void main(String args[]) throws Exception {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JTableAndGBC();
            }
        });
    }
}

编辑 1

  • JTable, JScollPane,JComboBox无法合理返回PreferredSize,请在编辑中查看我的代码,然后为所有人工作LayoutManagers

  • 仔细注意table.setPreferredScrollableViewportSize(table.getPreferredSize());,我建议测试 Dimension 是否超载所需的坐标或屏幕分辨率或不超载:-),

  • 否则收缩new Dimension(int, int)而不是table.getPreferredSize()

.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class JTableAndGBC {

    private String[] columnNames = {"Source", "Hit", "Last", "Ur_Diff"};
    private JTable table;
    private Object[][] data = {{"Swing Timer", 2.99, 5, 1.01},
        {"Swing Worker", 7.10, 5, 1.010}, {"TableModelListener", 25.05, 5, 1.01}};
    private DefaultTableModel model = new DefaultTableModel(data, columnNames);

    public JTableAndGBC() {
        JPanel panel = new JPanel(new GridBagLayout());
        table = new JTable(model);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        JScrollPane pane = new JScrollPane(table);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        panel.add(pane, gbc);
        JFrame frame = new JFrame();
        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) throws Exception {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JTableAndGBC();
            }
        });
    }
}

编辑 2

我强烈建议使用 BorderLayout、GridLayout

在此处输入图像描述

.

在此处输入图像描述

而不是 GBC ( JTable, JScollPane,JComboBox不能返回合理PreferredSize的, 需要覆盖 GBC, brrrr, 不是为什么要打扰)

在此处输入图像描述.在此处输入图像描述

边框布局代码

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class JTableAndGBC {

    private String[] columnNames = {"Source", "Hit", "Last", "Ur_Diff"};
    private JTable table;
    private Object[][] data = {{"Swing Timer", 2.99, 5, 1.01},
        {"Swing Worker", 7.10, 5, 1.010}, {"TableModelListener", 25.05, 5, 1.01}};
    private DefaultTableModel model = new DefaultTableModel(data, columnNames);

    public JTableAndGBC() {
        JPanel panel = new JPanel(new BorderLayout()/*(new GridBagLayout()*/);
        table = new JTable(model);
        //GridBagConstraints gbc = new GridBagConstraints();
        //gbc.weightx = 1.0;
        //gbc.fill = GridBagConstraints.HORIZONTAL;
        JScrollPane pane = new JScrollPane(table, 
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        /*TableColumn firstColumn = table.getColumnModel().getColumn(0);
        firstColumn.setMinWidth(150);
        firstColumn.setMaxWidth(200);
        TableColumn secondColumn = table.getColumnModel().getColumn(1);
        secondColumn.setMinWidth(200);
        secondColumn.setMaxWidth(250);
        TableColumn thirdColumn = table.getColumnModel().getColumn(1);
        thirdColumn.setMinWidth(50);
        thirdColumn.setMaxWidth(100);   */     
        table.setRowHeight(30);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        panel.add(pane/*, gbc*/);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) throws Exception {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JTableAndGBC();
            }
        });
    }
}
于 2013-05-20T06:46:45.687 回答
3

感谢您提出宝贵的建议。我找到了这个问题的确切解决方案。将表格调整为其首选大小或视口大小,以较大者为准。无论您使用哪种布局管理器,它都可以正常工作!

解决方案是使用getScrollableTracksViewportWidth()方法定义表,如下所示:

JTable table=new JTable(myModel){
 public boolean getScrollableTracksViewportWidth() {
   return getPreferredSize().width < getParent().getWidth();
 }
};
JScrollPane scroll=new JScrollPane(table);
于 2013-05-31T04:25:13.940 回答
1

去除

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 

线。它导致固定的表大小。

于 2013-05-20T07:06:38.400 回答