本文介绍了如何在Java Swing中动态控制自动调整大小的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用Java(1.8)Swing创建一个新的GUI,我正在寻找一种方法来覆盖我所有组件的调整大小行为.

Creating a new GUI in Java (1.8) Swing, I am searching for a way to override resize behavior of all my components.

让我向您解释一些编辑过的照片:

Let me explain to you with some edited photos:

这是我的全屏GUI,具有3个面板和一个 JToolBar .绿色的一个需要具有固定大小,其他的则需要可调整大小.

This is my full screen GUI, with 3 panels and a JToolBar. The green one needs to have a fixed size, the others would be resizable.

目前,我只有2个小的 GridLayout 来组织它们(绿色和青色面板一个垂直,一个水平).

Currently, I only have 2 small GridLayout to organize them (one vertical, and one horizontal for green and cyan panels).

例如,如果我从右侧减小边框尺寸,我希望根据新的边框尺寸调整尺寸来调整我的蓝色和青色面板.但是绿色面板必须为固定.(这不是我认为最困难的部分.)

If I, for example, reduce the frame size from the right side, I want my blue and cyan panel to be resized according to the new frame size. But the green panel must be fixed. (Not the most difficult part I think.)

这对我来说是最困难的部分.一旦青色(或蓝色)面板达到最小尺寸,我希望他将绿色面板从左侧"",即使它从框架中消失了.

This is the most difficult part for me. As soon as the cyan (or blue) panel reach is minimum size, I want him to "push" the green panel to the left, even if it disappears off the frame.

当然,向右拉边框将使绿色面板再次出现.

Of course, pulling the frame to the right will make the green panel appear again.

我该怎么办?

我考虑过使用 JSplitPane 或特定的侦听器来手动确定调整大小的行为,但是我需要一些建议.

I thought of using JSplitPane or a specific listener to manually decide the resize behavior but I need some advice.

很抱歉,如果现有帖子可以回答这个问题,我没有找到任何能为我解释问题的答案.

提前谢谢!

如果您想要更多示例,请查看以相同方式运行的"Evernote"软件

推荐答案

设置 Green 面板的最大/最小/首选大小可以使该面板在第一种情况下保持相同的大小.要检查尺寸,可以在其他 JPanel 之一上使用 ComponentListener -如果尺寸小于特定宽度,则更改最大/最小/首选尺寸 Green 面板上的.

Setting the max/min/preferred size of the Green panel can keep that panel the same size under the first condition. To check for resizes, you can use a ComponentListener on one of the other JPanel's - if the size gets below a particular width then change the max/min/preferred size of the Green panel.

下面是一个被黑的例子-当 Blue 为<时,它将调整 Green 面板的大小.600,并且调整大小是加权调整大小(占总宽度的30%).为了获得真正的L& F,您可能需要尝试布局/尺寸.

Below is a hacked together example of doing this - it resizes the Green panel when the Blue is < 600, and the resize is a weighted resize (30% of total width). To get the true L&F and that you desire you may have to play with the layout/sizes.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;


public class GridTest extends JPanel{

    private boolean changeAllowed = false;
    //keep reference to cyan for the height dimension
    final JPanel cyan = new JPanel();

    public GridTest(){
        cyan.setPreferredSize(new Dimension(600, 300));//provide sizing hint
    }

    private Dimension getCustomDimensions(){
        if ( changeAllowed ){
            return new Dimension((int)(super.getParent().getBounds().width * 0.3), cyan.getBounds().height);
        }else{
            return new Dimension(200, cyan.getBounds().height);
        }
    }
    @Override
    public Dimension getMaximumSize(){
        return getCustomDimensions();
    }
    @Override
    public Dimension getMinimumSize(){
        return getCustomDimensions();
    }
    @Override
    public Dimension getPreferredSize(){
        return getCustomDimensions();
    }

    public static void main(String[] args) throws Exception{
        SwingUtilities.invokeAndWait(new Runnable(){

            @Override
            public void run() {
                final int MINIMUM = 600;
                JFrame frame = new JFrame();
                frame.add(new JToolBar(), BorderLayout.NORTH);
                final JPanel blue = new JPanel();

                final GridTest green = new GridTest();
                green.setBackground(Color.green);
                green.setOpaque(true);

                green.cyan.setBackground(Color.cyan);
                green.cyan.setOpaque(true);
                blue.setOpaque(true);
                blue.setBackground(Color.blue);
                blue.setPreferredSize(new Dimension(900, 300));//hint at size
                blue.setMinimumSize(new Dimension(100, 200));//hint at size
                //Nest Box Layouts
                Box top = Box.createHorizontalBox();
                top.add(blue);
                Box bottom = Box.createHorizontalBox();
                bottom.add(green);
                bottom.add(green.cyan);
                Box vert = Box.createVerticalBox();
                vert.add(top);
                vert.add(bottom);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(vert);
                //listen for resizes
                blue.addComponentListener(new ComponentAdapter(){

                    @Override
                    public void componentResized(ComponentEvent e) {
                        if ( blue.getBounds().width < MINIMUM ){//set flag
                            green.changeAllowed = true;
                        }else{
                            green.changeAllowed = false;
                        }
                    }
                });

                frame.pack();
                frame.setSize(800, 600);
                frame.setVisible(true);

            }

        });
    }
}

这篇关于如何在Java Swing中动态控制自动调整大小的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:59