在Java中使用GridBagLayout时设置最大组件大小 [英] Setting up a maximum component size when using GridBagLayout in java

查看:136
本文介绍了在Java中使用GridBagLayout时设置最大组件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:我试图用窗口调整JScrollPane的大小,将其水平调整到一定大小,在该位置应该停止尝试随着窗口的增长.

My problem is the following : I'm trying to have a JScrollPane resizing with the window, up to a certain size horizontally, where it should stop trying to grow with the window.

我可以使用GridBagLayout来做到这一点吗?如果是这样,怎么办?

Can I do that with a GridBagLayout ? If so, how ?

推荐答案

一种方法是用BoxLayout将您的滚动窗格包裹在另一个JPanel中,并在BoxLayout设置的滚动窗格上设置MaximumSize:

One way to do that is to wrap you scrollpane in another JPanel with a BoxLayout and set a MaximumSize on your scrollpane which BoxLayout will enforce:

包装:

已拉伸(最大宽度已设置为700像素):

Stretched (max width has been set to 700 px):

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.BoxLayout;
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 TestGridBagLayout2 {

    protected void initUI() throws MalformedURLException {
        final JFrame frame = new JFrame();
        frame.setTitle(TestGridBagLayout2.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel panel = new JPanel(new GridBagLayout());
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        for (int i = 0; i < 20; i++) {
            Vector<String> v = new Vector<String>();
            for (int j = 0; j < 1; j++) {
                v.add("Cell (" + (i + 1) + "," + (j + 1) + ")");
            }
            data.add(v);
        }
        DefaultTableModel model = new DefaultTableModel(data, new Vector<String>(
                Arrays.asList("Col-1"/*, "Col-2", "Col-3", "Col-4", "Col-5"*/)));
        JTable table = new JTable(model);
        JScrollPane scroll = new JScrollPane(table);
        scroll.setMaximumSize(new Dimension(700, Integer.MAX_VALUE));
        JPanel wrappingPanel = new JPanel(null);
        wrappingPanel.setLayout(new BoxLayout(wrappingPanel, BoxLayout.LINE_AXIS));
        wrappingPanel.add(scroll);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        panel.add(wrappingPanel, gbc);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new TestGridBagLayout2().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

这篇关于在Java中使用GridBagLayout时设置最大组件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆