JPanel。你可以
remove在
JPanel如果你
JToggleButton不选择并添加您已经创建的实例
JPanel,如果选择了按钮。看这个简单的例子:
public class Mainframe extends Jframe {private JPanel topPanel = new JPanel();private JPanel centerPanel = new JPanel();private JToggleButton toggleButton = new JToggleButton("Toggle");public Mainframe() { this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new BorderLayout()); this.topPanel.setPreferredSize(new Dimension(100, 100)); this.centerPanel.setPreferredSize(new Dimension(100, 100)); this.toggleButton.setPreferredSize(new Dimension(100, 100)); this.add(topPanel, BorderLayout.NORTH); this.add(centerPanel, BorderLayout.CENTER); this.add(toggleButton, BorderLayout.SOUTH); this.toggleButton.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { add(centerPanel, BorderLayout.CENTER); } else { remove(centerPanel); } pack(); } }); this.pack(); this.setVisible(true);}}
您可以看到
centerPanel仅实例化了一次。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)