Java JPanel不可见

Java JPanel不可见,java,swing,jpanel,actionlistener,menuitem,Java,Swing,Jpanel,Actionlistener,Menuitem,我正在尝试使用JMenuItem删除和添加我需要的面板。然而,当我使用action listener并告诉它添加一个面板时,什么都没有发生 PanelMaker newPanel = new Panel(); //I have my panel in another class and I use this to call it item.addActionListener(new ActionListener() { @Override public void actio

我正在尝试使用JMenuItem删除和添加我需要的面板。然而,当我使用action listener并告诉它添加一个面板时,什么都没有发生

PanelMaker newPanel = new Panel(); //I have my panel in another class and I use this to call it
item.addActionListener(new ActionListener() {   
    @Override
    public void actionPerformed(ActionEvent e) {
       add(newPanel.pane());//I try to add the panel here, but nothing occurs
    }
});

您需要重新验证并重新绘制添加或删除组件的容器。i、 e

@Override
public void actionPerformed(ActionEvent e) {
    add(newPanel.pane());//I try to add the panel here, but nothing occurs
    revalidate(); // tells the layout managers to re-layout components
    repaint();  // requests that the repaint manager repaint the container
}
调用
revalidate()
会告诉容器的布局管理器重新布局其持有的所有组件,同样也会导致所有包含容器的重新布局层叠

调用
repaint()
再次建议重新绘制管理器重新绘制容器及其所有子容器。这一点非常重要,尤其是在移除组件或组件移动到先前看到另一个组件的位置的顶部时,以便清理旧渲染


容器使用的布局管理器也是非常重要的。有些人不容易接受新组件——在这方面,GroupLayout会立即出现在脑海中。

您需要重新验证并重新绘制添加或删除组件的容器。i、 e

@Override
public void actionPerformed(ActionEvent e) {
    add(newPanel.pane());//I try to add the panel here, but nothing occurs
    revalidate(); // tells the layout managers to re-layout components
    repaint();  // requests that the repaint manager repaint the container
}
调用
revalidate()
会告诉容器的布局管理器重新布局其持有的所有组件,同样也会导致所有包含容器的重新布局层叠

调用
repaint()
再次建议重新绘制管理器重新绘制容器及其所有子容器。这一点非常重要,尤其是在移除组件或组件移动到先前看到另一个组件的位置的顶部时,以便清理旧渲染


容器使用的布局管理器也是非常重要的。有些人不容易接受新组件——在这方面,GroupLayout会立即出现在脑海中。

您需要重新验证并重新绘制添加或删除组件的容器。i、 e

@Override
public void actionPerformed(ActionEvent e) {
    add(newPanel.pane());//I try to add the panel here, but nothing occurs
    revalidate(); // tells the layout managers to re-layout components
    repaint();  // requests that the repaint manager repaint the container
}
调用
revalidate()
会告诉容器的布局管理器重新布局其持有的所有组件,同样也会导致所有包含容器的重新布局层叠

调用
repaint()
再次建议重新绘制管理器重新绘制容器及其所有子容器。这一点非常重要,尤其是在移除组件或组件移动到先前看到另一个组件的位置的顶部时,以便清理旧渲染


容器使用的布局管理器也是非常重要的。有些人不容易接受新组件——在这方面,GroupLayout会立即出现在脑海中。

您需要重新验证并重新绘制添加或删除组件的容器。i、 e

@Override
public void actionPerformed(ActionEvent e) {
    add(newPanel.pane());//I try to add the panel here, but nothing occurs
    revalidate(); // tells the layout managers to re-layout components
    repaint();  // requests that the repaint manager repaint the container
}
调用
revalidate()
会告诉容器的布局管理器重新布局其持有的所有组件,同样也会导致所有包含容器的重新布局层叠

调用
repaint()
再次建议重新绘制管理器重新绘制容器及其所有子容器。这一点非常重要,尤其是在移除组件或组件移动到先前看到另一个组件的位置的顶部时,以便清理旧渲染


容器使用的布局管理器也是非常重要的。有些人不容易接受新组件——这一点会立即想到GroupLayout。

不过,对于处理相同问题的任何人,都需要添加“revalidate();”,我假设这会刷新JFrame@user2059856:不完全是。请看我的答案。不要忘记
repaint()
,尤其是在移除组件时。不过,对于处理相同问题的任何人,您都需要添加“revalidate();”,我假设这会刷新JFrame@user2059856:不完全是。请看我的答案。不要忘记
repaint()
,尤其是在移除组件时。不过,对于处理相同问题的任何人,您都需要添加“revalidate();”,我假设这会刷新JFrame@user2059856:不完全是。请看我的答案。不要忘记
repaint()
,尤其是在移除组件时。不过,对于处理相同问题的任何人,您都需要添加“revalidate();”,我假设这会刷新JFrame@user2059856:不完全是。请看我的答案。不要忘记
repaint()
,尤其是在移除组件时。否则你会有坏的惊喜。