想要动态更改网格包布局中的插入 [英] want to change an Inset in a gridbag layout dynamically

查看:28
本文介绍了想要动态更改网格包布局中的插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格包布局,我在其中初始化了一个 gbc.insets = Insets(0,0,0,);稍后我想在发生某些操作时调整此插图的大小.我试过更改值然后执行 repaint() 但这不起作用?我需要做什么?非常感谢!

I have a gridbag layout where I initialize an gbc.insets = Insets(0,0,0,); Later I want to resize this inset when some action happens. I've tried changing the value and then doing a repaint() but that does not work? What do I need to do? Thank you very much!

class myGraph {

    private Insets myInsets = new Insets(0,0,0,0);

    ...
    gbc.insets = myInsets; // setting Gridbag constraints.



    Action Listener {

        ............... 
        myInsets.top =30;
        myInsets.bottom =40;
        myGraph.repaint();
    }
}

推荐答案

您需要更新 GridBagLayout:

You need to update the GridBagLayout:

GridBagLayout layout = new GridBagLayout();
JPanel panel = new JPanel(layout);
...
layout.setConstraints(myComponent, anotherConstraint);
// do this for all the components you want to update
panel.revalidate();
panel.repaint();

GridBagLayout 在您添加组件时会克隆您的约束,因此您需要告诉 LayoutManager 您想要更改这些约束.简单地修改约束值是绝对没有效果的.

GridBagLayout clones your constraints when you add components, so you need to tell the LayoutManager that you want to change those constraints. Simply modifying the consraints value will have absolutely no effect.

顺便说一句,repaint() 只是执行绘画"操作,而不是布局.改用 revalidate().

Btw, repaint() is just to perform "painting" operations, not layout. Use revalidate() instead.

这篇关于想要动态更改网格包布局中的插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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