You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse WindowBuilder中Swing按钮无法打开窗口问题求助

问题:非主窗口按钮无法打开目标窗口

我使用Eclipse的WindowBuilder开发项目,为按钮添加ActionListener后,点击非主窗口内的按钮无法打开目标NotReading窗口,调整事件代码后仍未解决,现将相关代码附上,寻求解决方法:


可正常打开的窗口代码(WhatsAJmenu类)

/**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            WhatsAJmenu dialog = new WhatsAJmenu();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public WhatsAJmenu() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        
        JTextPane txtpnAJmenuIs = new JTextPane();
        txtpnAJmenuIs.setBounds(10, 11, 414, 206);
        txtpnAJmenuIs.setText("First you need a JMenu bar to add the items into the menu.\r\nSecondly you can add a JMenu, so when clicked on this element the JMenuItem performs an Action, in this case the action was opening the tab with the text you're reading.\r\n\r\n(Close this tab if you have finished reading)");
        contentPanel.add(txtpnAJmenuIs);
        
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setBounds(0, 0, 1, 1);
        contentPanel.add(layeredPane);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setBackground(new Color(216, 191, 216));
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton cancelButton = new JButton("I have finished reding this.");
                cancelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.exit (0);
                    }
                });
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                    }
                });
                
                JButton btnNewButton = new JButton("I haven't finished reading this.");
                btnNewButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        NotReading window = new NotReading();
                        window.setVisible(true);
                    }
                });
                
                buttonPane.add(btnNewButton);
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }
}

目标窗口代码(NotReading类)

package tabGame;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JLayeredPane;

public class NotReading extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NotReading frame = new NotReading();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public NotReading() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(255, 255, 51));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        textField = new JTextField();
        textField.setBackground(new Color(255, 255, 51));
        textField.setBounds(0, 0, 434, 261);
        contentPane.add(textField);
        textField.setColumns(10);
        
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setBounds(0, 0, 1, 1);
        contentPane.add(layeredPane);
    }
}

按钮事件代码

JButton btnNewButton = new JButton("I haven't finished reading this.");
btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        NotReading window = new NotReading();
        window.setVisible(true);
    }
});

buttonPane.add(btnNewButton);
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);

问题原因及解决方法

核心问题分析

  1. NotReading窗口的关闭操作设置错误:setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);会导致关闭该窗口时直接退出整个应用,而从JDialog打开JFrame时,这个设置会引发冲突。
  2. Swing组件未在事件调度线程中创建:直接在ActionListener中创建窗口可能导致线程安全问题,无法正确渲染窗口。
  3. 可能的包导入问题:如果WhatsAJmenu类和NotReading类不在同一包,未正确导入会导致编译/运行时错误。

具体修复步骤

  1. 修改NotReading的关闭操作
    将NotReading构造方法中的setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);替换为:

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    

    这样关闭NotReading只会销毁当前窗口,不会终止整个应用。

  2. 在事件调度线程中创建窗口
    修改按钮的ActionListener代码,确保窗口在Swing的事件线程中创建:

    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        NotReading window = new NotReading();
                        window.setVisible(true);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }
    });
    
  3. 检查包导入
    如果WhatsAJmenu类不在tabGame包下,需要在该类顶部添加导入语句:

    import tabGame.NotReading;
    

另外,注意代码中的拼写错误:I have finished reding this.应改为I have finished reading this.(不影响功能,但建议修正)。


内容的提问来源于stack exchange,提问作者MPV 070

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 08:05:55