如何更改 JFrame 中组件的尺寸

How to change the dimension of a component in a JFrame(如何更改 JFrame 中组件的尺寸)

本文介绍了如何更改 JFrame 中组件的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 JFrame 中有一个 JPanel.当我调用更改该 JPanel 的首选大小的方法时,它不会更改.

Suppose I have a JPanel in a JFrame. When I invoke a method that changes the preferred size of that JPanel, it does not change.

代码如下所示:

public class SomePanel extends JPanel{

    public SomePanel(){
        setPreferredSize( new Dimension( 390, 40 ) );
        setBackground( Color.BLACK );
    }

    public void expand(){
        setPreferredSize( new Dimension( 390, 200 ) );
    }

    public static void main( String args[] ){
        JFrame frame = new JFrame();
        frame.setSize( 450, 500 );
        frame.setLayout( new FlowLayout() );

        SomePanel somePanel = new SomePanel();

        frame.add( somePanel );
        frame.setVisible( true );

        somePanel.expand();
    }
}

有什么我必须先做的吗?我试过在调用 expand() 时检查 JPanel 的大小.JPanel 在设置首选大小前后的高度保持在 40.

Is there something that I have to do first? I have tried so check the size of the JPanel when expand() is invoked. The height of the JPanel before and after setting the preferred size remains at 40.

我也尝试过使用 Dimension 变量,但也没有用.

I have also tried to use a Dimension variable, and that did not work either.

    Dimension dimension;

    public SomePanel(){
        dimension = new Dimension( 390, 40 );
        ...
    }

    public expand(){
        dimension.setSize( 390, 200 );
        setPreferredSize( dimension );
    } 

推荐答案

您需要invalidate容器层次结构以使其重新布局组件.

You need to invalidate the container hierarchy to make it re-layout the components.

只需在您更改的组件上调用 invalidate,然后调用 revalidate.

Simply call invalidate followed by revalidate on the component you have changed.

这是一个小例子……

public class TestComponentHierarcy {

    public static void main(String[] args) {
        new TestComponentHierarcy();
    }

    public TestComponentHierarcy() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new Test());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Test extends JPanel {

        private Dimension size = new Dimension(10, 10);

        public Test() {

            setLayout(new GridBagLayout());
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    size.width += 10;
                    size.height += 10;
                    invalidate();
                    revalidate();
                }
            });

        }

        @Override
        public Dimension getPreferredSize() {
            return size;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        }

    }

}

这篇关于如何更改 JFrame 中组件的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何更改 JFrame 中组件的尺寸

基础教程推荐