java - 将 JPanel 类添加到 JPanel
问题描述
当我单击第一个 JPanel 类中的按钮时,我想添加一个 JPanel 类
class LaunchForm extends JPanel {
public static JButton btnSettings;
private ButtonActionHandler buttonActionHandler;
LaunchForm() {
buttonActionHandler = new ButtonActionHandler();
setLayout(null);
setVisible(true);
btnSettings = new JButton();
createButton(btnSettings, 385, 190, 191, 55, path_btnStartGame);
}
private void createButton(JButton button, int x, int y, int width, int height, String filePath) {
button.setBounds(x, y, width, height);
button.setFocusPainted(false);
button.setIcon(new ImageIcon(LaunchForm.class.getResource(filePath)));
button.setBackground(bgColor);
button.setForeground(Color.white);
button.setVisible(true);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
Border emptyBorder = BorderFactory.createEmptyBorder();
button.setBorder(emptyBorder);
button.addActionListener(new ButtonActionHandler());
add(button);
}
public class ButtonActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
add(new SettingsPanel());
}
}
}
这是我的 SettingsPanel,当我使用此面板启动程序时,它会正确显示。
class SettingsPanel extends JPanel {
private JButton btnBack;
SettingsPanel() {
setLayout(null);
setVisible(true);
btnBack = new JButton();
createButton(btnBack, 30, 30, 40, 40, path_btnLeftArrow);
}
private void createButton(JButton button, int x, int y, int width, int height, String filePath) {
button.setBounds(x, y, width, height);
button.setFocusPainted(false);
button.setIcon(new ImageIcon(LaunchForm.class.getResource(filePath)));
button.setBackground(bgColor);
button.setForeground(Color.white);
button.setVisible(true);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
Border emptyBorder = BorderFactory.createEmptyBorder();
button.setBorder(emptyBorder);
add(button);
}
}
但是,它以某种方式不起作用。而且我不想打开一个新窗口。我只想用SettingsPanel“切换”JPanel。
所以,任何帮助将不胜感激
解决方案
推荐阅读
- excel - 选择并隐藏 2 个命名行之间的整行
- javascript - Vue.js 动态导入
- seo - 如何在 Shopware 6 中创建 SEO 网址
- c - 在 STM32F072CBTx 上使用带 DMA 的定时器中断 + ADC 不起作用
- r - unloadNamespace(package) 中的错误:命名空间“rlang”由“testthat”、“pkgload”导入,
- java - 在 Thymeleaf 数组和列表中将整数作为字符串处理
- reactjs - 使用 react-app-wired 服务反应应用程序时出错
- php - 使用下一个按钮 HTML 在 MySQL PHP 中一次滚动一条记录
- python - 在 Jupyter 中使用 @interact 装饰器时实现“重置”按钮
- python - 对元组列表进行排序实际上并没有对它们进行排序