gpt4 book ai didi

java - JButton 从 jpanel 中删除所有其他 jcomponent

转载 作者:行者123 更新时间:2023-12-01 06:52:24 24 4
gpt4 key购买 nike

我已将此 Action 监听器添加到按钮中:

//The ActionListener for the home button
ActionListener homeActionListener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
removeAll();
Home home = new Home();
add(home);
}
};

这是 jpanel 和 jbutton 的代码:

//creating the jpanel which will hold the buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setBounds(0,0,600,100);
buttonPanel.setBackground(Color.GRAY);
add(buttonPanel);

//creating the jbutton to send the user to the home page
JButton home = new JButton("Home");
home.setBounds(25,25,100,50);
buttonPanel.add(home);

我怎样才能删除 jframe 的每个组件,除了名为 buttonPanel 的 jpanel 之外?

最佳答案

一个可能的解决方案是创建一个包含所有其他组件的中央面板,并简单地在此容器上使用 removeAll

请小心在 JFrame 上调用 removeAll,它会删除根 Pane ,这并不是您真正想要的。

HovercraftFullEels 所说的

关于java - JButton 从 jpanel 中删除所有其他 jcomponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21837899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com