簡體   English   中英

JPanel不可見

[英]JPanel is not visible

我正在嘗試使用JMenuItem刪除並添加所需的面板。 但是,當我使用動作偵聽器並告訴它添加面板時,什么也沒發生。

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

您需要重新驗證並重新粉刷添加或刪除組件的容器。

@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立即浮現在腦海。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM