在JScrollPane和JPanel中设置JTable的大小,使用JFrame的大小

时间:2012-11-03 21:24:52

标签: java swing jtable jscrollpane preferredsize

我希望表格的宽度与帧的宽度相同,而且当我调整帧的大小时,表格也需要调整大小。我认为setSize()的{​​{1}}无效。你能帮助我吗?

JTable

4 个答案:

答案 0 :(得分:11)

看看

table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);

您在此处发布的代码

答案 1 :(得分:7)

摆脱所有setSize来电。然后将tablePaneltableSP直接删除到JFrame的内容窗格。

JFrame的内容窗格默认为BorderLayout,并向BorderLayout#CENTER添加了一个组件(当您只使用add而没有约束时,这是默认设置)将确保组件采用父容器的大小。

快速调整代码

public class Main extends JFrame {

    public Main() {
        String[] columnNames = {"A", "B", "C"};
        Object[][] data = {
            {"Moni", "adsad", 2},
            {"Jhon", "ewrewr", 4},
            {"Max", "zxczxc", 6}
        };

        JTable table = new JTable(data, columnNames);
        JScrollPane tableSP = new JScrollPane(table);
        tableSP.setPreferredSize( 400, 600 );

        add(tableSP);
        setTitle("Marks");

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Main ex = new Main();
                ex.setVisible(true);
            }
        });
    }
}

小方注:没有理由延长JFrame。只需使用一个

答案 2 :(得分:1)

作为替代方案,请对GridLayouttablePanel封闭pack()使用Window

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    public Main() {
        String[] columnNames = {"A", "B", "C"};
        Object[][] data = {
            {"Moni", "adsad", 2},
            {"Jhon", "ewrewr", 4},
            {"Max", "zxczxc", 6}
        };

        JTable table = new JTable(data, columnNames);
        JScrollPane tableSP = new JScrollPane(table);

        int A = this.getWidth();
        int B = this.getHeight();

        table.setSize(A, B);
        JPanel tablePanel = new JPanel(new GridLayout());
        tablePanel.add(tableSP);
        tablePanel.setBackground(Color.red);

        add(tablePanel);
        setTitle("Marks");

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Main ex = new Main();
                ex.setVisible(true);
            }
        });
    }
}

答案 3 :(得分:1)

以下是答案:

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class Main extends JFrame {
    JTable table = new JTable();
    DefaultTableModel model = new DefaultTableModel(new Object[][]{},new String[]{"A","B","C"});
    public Main() {
        super("Mark");
        table.setModel(model);
        add(new JScrollPane(table));

   }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Main ex = new Main();
                ex.setDefaultCloseOperation(EXIT_ON_CLOSE);
                ex.setLocationByPlatform(true);
                ex.pack();
                ex.setVisible(true);
            }
        });
    }
}

相当简单,但相同。 :)