简体   繁体   English

如何使用相同的面板更新面板?

[英]How I can update a panel with the same panel?

I would replace the same panel several times , but do not know how to do. 我会多次更换同一面板,但不知道该怎么办。 I created a class " Grafico " which has a constructor that initializes with more parameters that I calculate in my code . 我创建了一个“ Grafico”类,该类具有一个构造函数,该构造函数使用在代码中计算的更多参数进行初始化。 For simplicity I will omit these parameters . 为简单起见,我将省略这些参数。 "box " is a JComboBox that depending on the selected item is activated and creates these panels For example: “ box”是一个JComboBox,它根据所选项目被激活并创建这些面板,例如:

JPanel middle = new JPanel(new BorderLayout());
Grafico graph1 = new Grafico(.......);
JPanel conf1 = new JPanel();

middle.add(graph1, BorderLayout.CENTER);
middle.add(conf1, BorderLayou.EAST);
frame.getContentPane().add(middle);

box.addItemListener(new ItemListener() {

                    @Override
                    public void itemStateChanged(ItemEvent e) {

.........//I do something..I create a new object of type "Grafico" with new parameters........................

Grafico graph2 = new Grafico(.......);
middle.remove(graph1);

                            middle.add(graph2, BorderLayout.CENTER);

                            frame.getContentPane().add(middle);
                            frame.getContentPane().revalidate();
                            frame.getContentPane().repaint();


}
});

And this works , but the second time I click on an object in the JComboBox I wish it would update me graph2 every time, without creating a new one every click , but it does not! 这项工作可行,但是第二次我单击JComboBox中的对象时,希望它每次都更新一次graph2,而不必每次单击都创建一个新对象,但事实并非如此!

You have to call repaint() and revalidate() in order to refresh the Panel. 您必须调用repaint()和revalidate()才能刷新面板。

Java Swing revalidate() vs repaint() Java Swing revalidate()与repaint()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM