无法动态更改面板 [英] Unable to Dynamically Change panel
问题描述
我有2个面板.第一个面板有一个组合框.根据所选组合框中项目的值,它下面的面板必须更改.在组合框的动作侦听器中,当我尝试更改面板时,它不会更改.为什么会这样?
I have 2 panels. The first panel has a combo box. depending on the value of the item in the combobox selected, a panel below it must change. in the action listener of the combo box, when I try to change the panel, it does not change. Why is this?
cb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String s = (String) cb1.getSelectedItem();
if (s.equals("Invoice")) {
panel3Customizera();
g.gridy = 2;
remove(panel3);
add(panel3, g);
} else {
panel3Customizerb();
g.gridy = 2;
add(panel3, g);
}
}
});
panel3customizer的add元素添加到panel3中.panel3被添加到jframe中.可以找到整个代码的链接在这里
panel3customizer's add elements into panel3.panel 3 is added to a jframe. The link to the entire code can be found here
推荐答案
像底部JPanel
这样的声音应该使用如何使用CardLayout 的教程.
Sounds like the bottom JPanel
ought to be using CardLayout
. Here's a tutorial on How to Use CardLayout.
@Hovercraft Full Of Eels 是他的推荐调用 revalidate()
和 repaint()
在容器对象上,因为这将导致它再次布局其组件并删除所有可视的工件".
And @Hovercraft Full Of Eels is right in his recommendation to invoke revalidate()
and repaint()
on the container object, since this will cause it to layout its components again and remove any visual "artifacts".
这篇关于无法动态更改面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!