Java Swing点击btnManga按钮打开指定窗口时弹出空窗口问题
问题分析与解决方案
你的代码出现空白窗口的核心原因是:manga类继承了JFrame,但所有可视化组件都被添加到了类内部额外创建的frmAcheterDesLivres对象上,而你点击按钮时调用的是mangaWindow.setVisible(true),显示的是manga自身继承的空JFrame,而非包含所有内容的frmAcheterDesLivres。
下面提供两种解决方案:
方案一:修改manga类(推荐,符合Swing规范)
直接使用manga类自身作为主窗口,不再创建额外的JFrame对象,所有组件添加到当前窗口的内容面板中。
修改后的manga.java代码:
package biblio; import java.awt.*; import javax.swing.*; public class manga extends JFrame { private JTable mangatable; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { manga mangaWindow = new manga(); mangaWindow.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public manga() { initialize(); } private void initialize() { // 直接配置当前manga实例,替代原有的frmAcheterDesLivres setTitle("Acheter des Livres"); setResizable(false); getContentPane().setFont(new Font("Yu Gothic Medium", Font.BOLD, 15)); getContentPane().setBackground(new Color(66, 66, 66)); setBounds(100, 100, 758, 601); // 改为DISPOSE_ON_CLOSE,关闭子窗口不会退出整个程序 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); JButton btnNewButton_1_1 = new JButton(""); btnNewButton_1_1.setIcon(new ImageIcon(manga.class.getResource("/resources/mangas/blackClover.jpg"))); btnNewButton_1_1.setBounds(31, 46, 125, 125); getContentPane().add(btnNewButton_1_1); JButton btnNewButton_1 = new JButton(""); btnNewButton_1.setIcon(new ImageIcon(manga.class.getResource("/resources/mangas/chainsawMan.png"))); btnNewButton_1.setBounds(225, 46, 125, 125); getContentPane().add(btnNewButton_1); JButton btnNewButton_1_2 = new JButton(""); btnNewButton_1_2.setIcon(new ImageIcon(manga.class.getResource("/resources/mangas/BokuNoHeroAcademia.jpg"))); btnNewButton_1_2.setBounds(414, 46, 125, 125); getContentPane().add(btnNewButton_1_2); JButton btnNewButton_1_3 = new JButton(""); btnNewButton_1_3.setIcon(new ImageIcon(manga.class.getResource("/resources/mangas/jujutsuKaisen.jpg"))); btnNewButton_1_3.setBounds(590, 46, 125, 125); getContentPane().add(btnNewButton_1_3); JLabel lblNewLabel = new JLabel("Black Clover"); lblNewLabel.setLabelFor(btnNewButton_1_1); lblNewLabel.setForeground(new Color(40, 168, 253)); lblNewLabel.setVerticalAlignment(SwingConstants.TOP); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblNewLabel.setBounds(31, 185, 125, 34); getContentPane().add(lblNewLabel); JLabel lblMga = new JLabel("40 000 MGA"); lblMga.setLabelFor(btnNewButton_1_1); lblMga.setForeground(new Color(40, 168, 253)); lblMga.setVerticalAlignment(SwingConstants.TOP); lblMga.setHorizontalAlignment(SwingConstants.CENTER); lblMga.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblMga.setBounds(31, 210, 125, 34); getContentPane().add(lblMga); JLabel lblChainsawMan = new JLabel("Chainsaw Man"); lblChainsawMan.setLabelFor(btnNewButton_1); lblChainsawMan.setForeground(new Color(40, 168, 253)); lblChainsawMan.setVerticalAlignment(SwingConstants.TOP); lblChainsawMan.setHorizontalAlignment(SwingConstants.CENTER); lblChainsawMan.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblChainsawMan.setBounds(235, 181, 125, 34); getContentPane().add(lblChainsawMan); JLabel lblMga_1 = new JLabel("60 000 MGA"); lblMga_1.setLabelFor(btnNewButton_1); lblMga_1.setForeground(new Color(40, 168, 253)); lblMga_1.setVerticalAlignment(SwingConstants.TOP); lblMga_1.setHorizontalAlignment(SwingConstants.CENTER); lblMga_1.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblMga_1.setBounds(225, 210, 125, 34); getContentPane().add(lblMga_1); JLabel lblBokuNoHero = new JLabel("Boku no Hero Academia"); lblBokuNoHero.setLabelFor(btnNewButton_1_2); lblBokuNoHero.setVerticalAlignment(SwingConstants.TOP); lblBokuNoHero.setHorizontalAlignment(SwingConstants.CENTER); lblBokuNoHero.setForeground(new Color(40, 168, 253)); lblBokuNoHero.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblBokuNoHero.setBounds(414, 181, 125, 34); getContentPane().add(lblBokuNoHero); JLabel lblMga_1_1 = new JLabel("35 000 MGA"); lblMga_1_1.setLabelFor(btnNewButton_1_2); lblMga_1_1.setVerticalAlignment(SwingConstants.TOP); lblMga_1_1.setHorizontalAlignment(SwingConstants.CENTER); lblMga_1_1.setForeground(new Color(40, 168, 253)); lblMga_1_1.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblMga_1_1.setBounds(414, 213, 125, 34); getContentPane().add(lblMga_1_1); JLabel lblJujutsuKaisen = new JLabel("Jujutsu Kaisen"); lblJujutsuKaisen.setLabelFor(btnNewButton_1_3); lblJujutsuKaisen.setVerticalAlignment(SwingConstants.TOP); lblJujutsuKaisen.setHorizontalAlignment(SwingConstants.CENTER); lblJujutsuKaisen.setForeground(new Color(40, 168, 253)); lblJujutsuKaisen.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblJujutsuKaisen.setBounds(590, 181, 125, 34); getContentPane().add(lblJujutsuKaisen); JLabel lblMga_1_1_1 = new JLabel("23 000 MGA"); lblMga_1_1_1.setLabelFor(btnNewButton_1_3); lblMga_1_1_1.setVerticalAlignment(SwingConstants.TOP); lblMga_1_1_1.setHorizontalAlignment(SwingConstants.CENTER); lblMga_1_1_1.setForeground(new Color(40, 168, 253)); lblMga_1_1_1.setFont(new Font("Yu Gothic Medium", Font.ITALIC, 14)); lblMga_1_1_1.setBounds(590, 213, 125, 34); getContentPane().add(lblMga_1_1_1); JLabel lblNewLabel_1 = new JLabel("Votre Panier de Mangas:"); lblNewLabel_1.setFont(new Font("Yu Gothic UI Semibold", Font.PLAIN, 20)); lblNewLabel_1.setForeground(new Color(38, 217, 255)); lblNewLabel_1.setBounds(257, 291, 282, 34); getContentPane().add(lblNewLabel_1); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(61, 335, 641, 158); getContentPane().add(scrollPane); mangatable = new JTable(); scrollPane.setViewportView(mangatable); mangatable.setBackground(new Color(49, 49, 49)); mangatable.setForeground(new Color(38, 217, 255)); } }
关键修改点:
- 删除了冗余的
private JFrame frmAcheterDesLivres;字段 - 将所有
frmAcheterDesLivres.xxx()调用替换为当前类的xxx()方法 - 把关闭操作改为
DISPOSE_ON_CLOSE,避免关闭子窗口时退出整个程序
方案二:修改按钮事件监听(不推荐,破坏封装)
如果不想改动manga类结构,可将frmAcheterDesLivres改为public字段,然后在按钮点击时显示该窗口:
- 修改
manga.java的字段定义:
public JFrame frmAcheterDesLivres;
- 修改
principal.java中的按钮监听代码:
btnManga.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { manga mangaWindow = new manga(); mangaWindow.frmAcheterDesLivres.setVisible(true); } });
额外建议
- Java类名需遵守大驼峰规范,
principal和manga应改为Principal和Manga - 尽量避免使用
null布局,推荐使用BorderLayout、GridBagLayout等布局管理器,提升界面兼容性
内容的提问来源于stack exchange,提问作者Stract
相关产品推荐
相关产品推荐

