当前位置: 首页 > 知识库问答 >
问题:

如何最大化和最小化Swing中JPanel内的JTable?

潘振国
2023-03-14

我正在制作一个Swing GUI。我没有得到如何在JPanel中最大和最小JTable。

共有1个答案

蔺翰音
2023-03-14

从评论编辑

你评论说我想让桌子“全屏”。如果你说的全屏是指容器的大小,你可以简单地做到。

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;

public class JTableExample {
    public static void main(String[] args) {
        JPanel borderLayoutPanel = new JPanel(new BorderLayout());
        String[] headers = {"Header 1", "Header 2", "etc..."};
        String[][] data = {{"Some data", "some more data", "etc..."},
                {"Some data 1", "some more data 3", "etc..."},
                {"Some data 2", "some more data 4", "etc..."}};

        JTable table = new JTable(data, headers);
        JScrollPane scrollableTable = new JScrollPane(table);
        borderLayoutPanel.add(scrollableTable, BorderLayout.CENTER);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(borderLayoutPanel);
        frame.setSize(800, 800);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

如果你真的想要一个全屏窗口,看看这个。

有几种方法可以做到这一点。

我建议你阅读这个问题,因为它将有助于解释为什么空布局是不好的。

TLDR基本上使用布局管理器。我首选的布局管理器是miglaway,因为它使用简单,易于理解,但功能齐全。如果你不能使用MigLayout,因为它是第三方库,那么就使用GridBagLayout之类的东西

MigLayout网站和MigLayout下载页面。

我的首选布局管理器

如果你愿意使用第三方布局管理器,那么我认为MigLayout是一个不错的选择。它提供了更多的控制,我认为比大多数布局管理器更容易使用。

private JPanel createMigPanel() {
    String layoutConstraints = "fillx, filly";
    String columnAndRowConstraints = "fill, grow, center";

    JPanel migPanel = new JPanel(new MigLayout(layoutConstraints, columnAndRowConstraints, columnAndRowConstraints));

    String[] headers = {"Header 1", "Header 2", "etc..."};
    String[][] data = {{"Some data", "some more data", "etc..."},
            {"Some data 1", "some more data 3", "etc..."},
            {"Some data 2", "some more data 4", "etc..."}};

    JTable table = new JTable(data, headers);
    JScrollPane scrollableTable = new JScrollPane(table);

    migPanel.add(new JScrollPane(table), "width 400:500:800, height 400:500:800");

    return migPanel;
}

糟糕的方式

如果您必须在JPanel中使用空布局,这根本不推荐,那么您可以使用两个按钮将表格设置为其最小/最大大小。

// Not recommended
private JPanel createNullPanel() {
    JPanel nullPanel = new JPanel(null);

    String[] headers = {"Header 1", "Header 2", "etc..."};
    String[][] data = {{"Some data", "some more data", "etc..."},
            {"Some data 1", "some more data 3", "etc..."},
            {"Some data 2", "some more data 4", "etc..."}};

    JTable table = new JTable(data, headers);
    JScrollPane scrollableTable = new JScrollPane(table);

    scrollableTable.setMinimumSize(new Dimension(100, 100));
    scrollableTable.setMaximumSize(new Dimension(500, 500));

    JButton minButton = new JButton("Min");
    minButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            scrollableTable.setSize(scrollableTable.getMinimumSize());
        }
    });
    minButton.setBounds(10, 10, 50, 25);
    nullPanel.add(minButton);


    JButton maxButton = new JButton("Min");
    maxButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            scrollableTable.setSize(scrollableTable.getMaximumSize());
        }
    });
    maxButton.setBounds(70, 10, 50, 25);
    nullPanel.add(maxButton);

    nullPanel.add(scrollableTable);
    scrollableTable.setBounds(10, 45, 300, 300);

    return nullPanel;
}

内置布局管理器

但是,您可以使用布局管理器。GridBagLayout有点复杂,但提供了比大多数情况更多的控制。

private JPanel createGridBagPanel() {
    JPanel gridBagPanel = new JPanel(new GridBagLayout());

    GridBagConstraints constraints = new GridBagConstraints();
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(10, 10, 10, 10);

    String[] headers = {"Header 1", "Header 2", "etc..."};
    String[][] data = {{"Some data", "some more data", "etc..."},
            {"Some data 1", "some more data 3", "etc..."},
            {"Some data 2", "some more data 4", "etc..."}};

    JTable table = new JTable(data, headers);
    JScrollPane scrollableTable = new JScrollPane(table);
    scrollableTable.setMinimumSize(new Dimension(400, 400));
    scrollableTable.setPreferredSize(new Dimension(500, 500));
    scrollableTable.setMaximumSize(new Dimension(800, 800));

    // Nasty work around to support min and max size
    // https://stackoverflow.com/questions/15161332/setting-up-a-maximum-component-size-when-using-gridbaglayout-in-java
    JPanel wrappingPanel = new JPanel(null);
    wrappingPanel.setLayout(new BoxLayout(wrappingPanel, BoxLayout.LINE_AXIS));
    wrappingPanel.add(scrollableTable);

    gridBagPanel.add(wrappingPanel, constraints);

    return gridBagPanel;
}

完整代码示例

import net.miginfocom.swing.MigLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTableExample {

    private void run() {
        setUpWindow();
    }

    private void setUpWindow() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // frame.getContentPane().add(createNullPanel());
        // frame.getContentPane().add(createGridBagPanel());
        frame.getContentPane().add(createMigPanel());

        // If using null layout manager
        // frame.setSize(800, 800);

        // If using a none null layout manager
        frame.pack();

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    // Not recommended
    private JPanel createNullPanel() {
        JPanel nullPanel = new JPanel(null);

        String[] headers = {"Header 1", "Header 2", "etc..."};
        String[][] data = {{"Some data", "some more data", "etc..."},
                {"Some data 1", "some more data 3", "etc..."},
                {"Some data 2", "some more data 4", "etc..."}};

        JTable table = new JTable(data, headers);
        JScrollPane scrollableTable = new JScrollPane(table);

        scrollableTable.setMinimumSize(new Dimension(100, 100));
        scrollableTable.setMaximumSize(new Dimension(500, 500));

        JButton minButton = new JButton("Min");
        minButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                scrollableTable.setSize(scrollableTable.getMinimumSize());
            }
        });
        minButton.setBounds(10, 10, 50, 25);
        nullPanel.add(minButton);


        JButton maxButton = new JButton("Min");
        maxButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                scrollableTable.setSize(scrollableTable.getMaximumSize());
            }
        });
        maxButton.setBounds(70, 10, 50, 25);
        nullPanel.add(maxButton);

        nullPanel.add(scrollableTable);
        scrollableTable.setBounds(10, 45, 300, 300);

        return nullPanel;
    }

    private JPanel createGridBagPanel() {
        JPanel gridBagPanel = new JPanel(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.weightx = 1;
        constraints.weighty = 1;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.insets = new Insets(10, 10, 10, 10);

        String[] headers = {"Header 1", "Header 2", "etc..."};
        String[][] data = {{"Some data", "some more data", "etc..."},
                {"Some data 1", "some more data 3", "etc..."},
                {"Some data 2", "some more data 4", "etc..."}};

        JTable table = new JTable(data, headers);
        JScrollPane scrollableTable = new JScrollPane(table);
        scrollableTable.setMinimumSize(new Dimension(400, 400));
        scrollableTable.setPreferredSize(new Dimension(500, 500));
        scrollableTable.setMaximumSize(new Dimension(800, 800));

        // Nasty work around to support min and max size
        // https://stackoverflow.com/questions/15161332/setting-up-a-maximum-component-size-when-using-gridbaglayout-in-java
        JPanel wrappingPanel = new JPanel(null);
        wrappingPanel.setLayout(new BoxLayout(wrappingPanel, BoxLayout.LINE_AXIS));
        wrappingPanel.add(scrollableTable);

        gridBagPanel.add(wrappingPanel, constraints);

        return gridBagPanel;
    }

    private JPanel createMigPanel() {
        String layoutConstraints = "fillx, filly";
        String columnAndRowConstraints = "fill, grow, center";

        JPanel migPanel = new JPanel(new MigLayout(layoutConstraints, columnAndRowConstraints, columnAndRowConstraints));

        String[] headers = {"Header 1", "Header 2", "etc..."};
        String[][] data = {{"Some data", "some more data", "etc..."},
                {"Some data 1", "some more data 3", "etc..."},
                {"Some data 2", "some more data 4", "etc..."}};

        JTable table = new JTable(data, headers);
        JScrollPane scrollableTable = new JScrollPane(table);

        migPanel.add(new JScrollPane(table), "width 400:500:800, height 400:500:800");

        return migPanel;
    }

    public static void main(String[] args) {
        JTableExample example = new JTableExample();
        example.run();
    }
}
 类似资料:
  • 问题内容: 是否可以在JDialog中添加最大化/最小化按钮?如果不是,那么我们可以将这些按钮添加到JPanel吗? 我有一个JPanel,并且在该面板内部有一个JDialog。我想添加一个最小化/最大化按钮,以便当单击该按钮时,JDialog会根据JPanel调整JDialog下的组件。就像当我单击最大化时,jpanel应该被放大,并且该面板(JDialog)中的组件也被放大,反之亦然,以最小化

  • 问题内容: 我需要从中删除最大化和最小化按钮。请提出建议。 问题答案:

  • 问题内容: 我正在使用和作为模块捆绑程序编写一个Web应用程序。到目前为止,我的代码还很轻巧,整个文件夹的大小为25 kb。 我创建的虽然是2.2 mb。使用标志运行优化后,它将捆绑包减少到700kb,这仍然非常大。 我调查了文件,文件大小为130kb。 Webpack是否可能产生如此大的文件,或者我做错了什么? webpack.config.js 编辑 package.json: 问题答案: 根

  • 问题内容: 是否可以通过事件监听器访问JFrame对象,以检测用户何时单击窗口的最大化或最小化按钮? 我正在使用JFrame对象,如下所示: 问题答案: 您可以使用WindowStateListener。如何编写窗口侦听器教程演示了如何创建与窗口相关的事件处理程序。

  • 我正在测试一些使用JWindow和隐藏jframe的代码,所以我必须创建最小化、最大化和关闭按钮,以方便用户使用。单击创建的按钮时,如何将状态设置为帧。

  • 问题内容: 我有一个python程序,它将打开一个新窗口以显示一些“关于”信息。该窗口有其自己的关闭按钮,而我已使其不可调整大小。但是,最大化和最小化按钮仍然存在,我希望它们消失。 我正在使用Tkinter,包装所有信息以显示在Tk类中。 到目前为止的代码如下。我知道它不是很漂亮,我打算将信息扩展到一个类中,但是我想在继续之前解决这个问题。 任何人都知道如何管理Windows管理器显示哪些默认按钮