java - 如何监听其组件之一的JButton?

标签 java swing listener actionlistener

嗨,我希望一个类能够监听其组件之一的 JButton。 这里我有一个 Window 类,它有一个 MyPanel 组件,该组件本身有 JButtons。我希望在按下 MyPanel 的 JButton 时通知 Window。我怎样才能做到这一点?

这是一段代码,仅用于说明我的需求,尚未经过测试。

感谢您的帮助!

 class Window extends JFrame {
            private MyPanel myPane;

            public static void main(String [] args) {
                Window mainWindow = new Window();
    }

    public Window() {

        this.myPane = new MyPanel();
            getContentPane().add(myPane);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 600);
        this.setVisible(true);
    }

        public computations() {
    // Here I would like to get myPane.s, or do other things regarding myPane's attributes (with getters), only once myPane.b2 is pressed.
        }
}

class MyPanel extends JPanel {
    String s;
    JButton b1;
    JButton b2;

    public MyPanel() {
        s = new String("");
        b1 = new JButton("say hello");
        b2 = new JButton("Close");
        this.add(b1);
        this.add(b2);
        ButtonHandler phandler = new ButtonHandler();
        b1.addActionListener(phandler);
        b2.addActionListener( actionPerformed(ActionEvent e) {
            this.s = "Hello world";
        });

    }


    class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // Tells Window class something happened.
        }
    }

}

最佳答案

我给你准备了一个促销 Activity ,你可以尝试一下是否有效。由于我刚刚设置了计算机,尚未安装 java 环境或 IDE,因此很抱歉无法测试我的解决方案。

要点是您可以将 Window 实例传递给 MyPanel,如下面的代码:

 class Window extends JFrame {

        public String myStr = "";
        private MyPanel myPane;

        public static void main(String [] args) {
            Window mainWindow = new Window();
}

public Window() {
    this.myPane = new MyPanel();
        getContentPane().add(myPane);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 600);
    this.setVisible(true);
}

    public computations() {
// Here I would like to get myPane.s, or do other things regarding myPane's attributes (with getters), only once myPane.b2 is pressed.
     System.out.println(this.myStr);
    }
}

class MyPanel extends JPanel {
Window win = null;
String s;
JButton b1;
JButton b2;

public MyPanel(Window window) {
    this.win = window;
    s = new String("");
    b1 = new JButton("say hello");
    b2 = new JButton("Close");
    this.add(b1);
    this.add(b2);
    ButtonHandler phandler = new ButtonHandler();
    b1.addActionListener(phandler);
    b2.addActionListener( actionPerformed(ActionEvent e) {
        this.s = "Hello world";
    });

}


class ButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // Tells Window class something happened.
        this.win.myStr = s;
    }
}

}

关于java - 如何监听其组件之一的JButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22508993/

相关文章:

java - javax.swing 是否可以在一帧中使用两个画家?

ios - Swift FireStore Listener 在第二次加载应用程序时抛出错误

Java泛型编译错误: incompatible types: Item#1 cannot be converted to Item#2

Java重绘网格布局

java - lombok可以仅根据类型生成构建器吗?

listener - 如何将监听器添加到 SplitPane Divider 位置?

extjs - 监听 View Controller 之间的事件

Java HashMap 实现在 Entry 类中有 'next' 成员。有什么用

java - 使用空的gridLayout,但稍后会保存ImageIcon并存储信息。我可以修改尺寸吗?

java - 如何在调用 dispose() 后重置 swing 中的字段