可以从第二页或儿童页面处置

发布于 2025-01-21 10:13:28 字数 1107 浏览 3 评论 0原文

我无法从第二页或子页面处置。编译器报告:找不到符号

如何处理它?

菜单

public class Menu extends javax.swing.JFrame {

    /**
     * Creates new form Menu
     */
    public Menu() {
        initComponents();
    }
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:
    JFrame app = new JFrame();
    Task t = new Task();
    t.setVisible(true);
    app.add(t);
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    app.setSize(690, 340);
    app.setVisible(true);
    dispose();
}    
}

<代码>任务

public class Task extends javax.swing.JPanel {
/**
 * Creates new form NewJPanel
 */
public Task() {
    initComponents();
}
    private void BackButtonMouseClicked(java.awt.event.MouseEvent evt) {                                        
    Menu t = new Menu();
    t.setVisible(true);
    setVisible(false);
    dispose(); <-- here
    
}    
}

我从菜单 任务链接。它已经完成,但是当我回到菜单时,它无法处理。

I can't dispose from second page or child page. The compiler reports: can not find symbol

How can I dispose of it?

Menu

public class Menu extends javax.swing.JFrame {

    /**
     * Creates new form Menu
     */
    public Menu() {
        initComponents();
    }
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:
    JFrame app = new JFrame();
    Task t = new Task();
    t.setVisible(true);
    app.add(t);
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    app.setSize(690, 340);
    app.setVisible(true);
    dispose();
}    
}

Task

public class Task extends javax.swing.JPanel {
/**
 * Creates new form NewJPanel
 */
public Task() {
    initComponents();
}
    private void BackButtonMouseClicked(java.awt.event.MouseEvent evt) {                                        
    Menu t = new Menu();
    t.setVisible(true);
    setVisible(false);
    dispose(); <-- here
    
}    
}

I linked from Menu to Task. It's done but when I go back to Menu it can not be disposed of.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2025-01-28 10:13:28

首先,不要使用Mouselistener来处理单击按钮。相反,您应该使用旨在用于此目的的ActionListener。

然后,在您的ActionListener代码中,您需要在处理框架之前获取包含按钮的帧的引用。您可以从ActionEvent中获取按钮。

基本逻辑是:

JButton button = (JButton)evt.getSource();
Window window = SwingUtilities.windowForComponent( button );
window.dispose();

First of all, don't use a MouseListener to handle a click on a button. Instead you should be using an ActionListener which was designed to be used for this purpose.

Then in your ActionListener code you need to get a reference to the frame containing the button before you can dispose of the frame. You can get the button from the ActionEvent.

The basic logic would be:

JButton button = (JButton)evt.getSource();
Window window = SwingUtilities.windowForComponent( button );
window.dispose();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文