0

gbc.insets = Insets(0,0,0、);を初期化するグリッドバッグレイアウトがあります。後で、何らかのアクションが発生したときに、このインセットのサイズを変更したいと思います。値を変更してからrepaint()を実行しようとしましたが、機能しませんか?私は何をする必要がありますか?どうもありがとうございます!

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();
    }
}
4

1 に答える 1

3

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に通知する必要があります。consraints値を変更するだけでは、まったく効果がありません。

ところで、repaint()は、レイアウトではなく、単に「ペイント」操作を実行するためのものです。代わりにrevalidate()を使用してください。

于 2012-05-14T18:53:25.270 に答える