ActionListener获取默认值

ActionListener获取默认值

本文介绍了ActionListener获取默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java实现一个简单的应用程序.我正在为该应用程序使用MVC模块.问题是当我的 Controller 创建 View Model 的对象时,当尝试使用简单的get方法时,我会获得默认值值,而不是我在用户界面中插入的新值.这是一个代码示例:

I'm implementing a simple application in Java. I'm using the MVC module for the app. The problem is that when my Controller creates the objects of the View and the Model, when trying to use a simple get method I get the defaults values and not the new ones, that I inserted in the UI. Here is a code exmaple:

查看:

public class Client extends JFrame {
    private float ammount;
    private JButton calculateButton;
    ...
    public void startUI(ActionListener listener) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Client frame = new Client(listener);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    ...
    public Client(ActionListener listener) {
    ...
                        ammount = 10;
    ...
    calculateButton = new JButton("Calculate");
    calculateButton.addActionListener(listener);
        add(calculateButton);
    ...
    public float getAmmount() {
        return (float) this.ammount;
}

控制器:

public class Controller implements ActionListener {

    private float result;
    private Server server = new Server();
    private Client client = new Client(this);

    public Controller() {
        server.rateParser();
        client.startUI(this);
    }

    public void actionPerformed(ActionEvent e) {
        result = client.getAmmount();
    }
}

主要:

public class Program {

    // Main function
    public static void main(String[] args) {
        Controller controller = new Controller();
    }

}

但是到目前为止,当我单击按钮并触发动作事件时, getAmmount 方法返回-1,这是默认值.对于 Client 类中的所有吸气剂也是如此.有谁知道为什么会这样吗?

So far so good, however, when I click the button and the action event triggers,the getAmmount method returns -1, which is the default value. Same goes for all the getters in the Client class. Does any one knows why is this happening?

推荐答案

您正在初始化2个客户端.

You are initializing 2 clients.

第一次 main

Controller controller = new Controller();

初始化类时,它的所有成员也会被初始化.由于 Client Controller 的成员,所以:

When you initialize a class, all its members are initialized too. Since Client is a member of Controller:

public class Controller implements ActionListener {

    private Client client = new Client(this); // 1st initialization
}

通过 new Controller()调用对其进行初始化.

it is initialized with a new Controller() call.

第二次是构造函数中的调用

public Controller() {

    server.rateParser();
    client.startUI(this); // <---- here
}

将绒毛排除在 startUI 之外,它是:

Leaving the fuzz out of startUI, it is:

public void startUI(ActionListener listener) {

    Client frame = new Client(listener); // 2nd initialization
    frame.setVisible(true);
}

由于 actionPerformed 位于 Controller 内部,因此在 result = client.getAmmount();中调用的是其字段 client ..该程序在程序的整个生命周期中都保持不变,因此将返回默认值(该默认值是在其初始化过程中创建的).但是,您显示的客户端是第二个客户端 frame ,您将其称为 frame.setVisible(true); .那个被修改了,但是它的值永远不会被读取.

Since actionPerformed is inside Controller, it is its field client that is called in result = client.getAmmount();. That one is remained untouched throughout the program's lifetime and thus returns the defaults (which were created in its initialization). However, the client you display is the second one, frame, for which you call frame.setVisible(true);. That one is modified, but its values are never read.

这篇关于ActionListener获取默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 14:33