【问题标题】:Java ActionListener - change variable in actionPerformedJava ActionListener - 更改 actionPerformed 中的变量
【发布时间】:2016-01-08 23:36:17
【问题描述】:

我正在开始我的编程冒险,但我遇到了一个问题。我尝试使用 awt 制作一个简单的计算器。我无法更进一步,因为我不知道如何更改一个变量 - 在 MainFrame 中初始化的 textField。我想在 actionPerformed 中更改它。这是我的代码,如果您能给我一些指导,我将不胜感激。谢谢!

package starter;
import java.awt.EventQueue;

public class Starter {
    public Starter () {
        EventQueue.invokeLater(new Runnable (){
            @Override
            public void run () {
                new MainFrame();
                System.out.println();
            }
        });
    }

    public static void main(String[] args) {
        new Starter();
    }
}

主框架

package starter;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainFrame extends JFrame {
    private JTextField textField = new JTextField();
    DigitActionListener digitPressed = new DigitActionListener();

    public MainFrame() {
        super("Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(true);
        setSize(350, 400);
        setLayout (new GridLayout (6, 4, 3, 3));

        JButton buttonClear = new JButton("Clear"); buttonClear.addActionListener(digitPressed);
        JButton button0 = new JButton ("0");        button0.addActionListener(digitPressed);
        JButton button1 = new JButton ("1");        button1.addActionListener(digitPressed);
        JButton button2 = new JButton ("2");        button2.addActionListener(digitPressed);
        JButton button3 = new JButton ("3");        button3.addActionListener(digitPressed);
        JButton button4 = new JButton ("4");        button4.addActionListener(digitPressed);
        JButton button5 = new JButton ("5");        button5.addActionListener(digitPressed);
        JButton button6 = new JButton ("6");        button6.addActionListener(digitPressed);
        JButton button7 = new JButton ("7");        button7.addActionListener(digitPressed);
        JButton button8 = new JButton ("8");        button8.addActionListener(digitPressed);
        JButton button9 = new JButton ("9");        button9.addActionListener(digitPressed);
        JButton multiplicationButton = new JButton ("*");   multiplicationButton.addActionListener(digitPressed);
        JButton divisionButton = new JButton ("/");         divisionButton.addActionListener(digitPressed);
        JButton additionButton = new JButton ("+");         additionButton.addActionListener(digitPressed);
        JButton substructionButton = new JButton ("-");     substructionButton.addActionListener(digitPressed);
        JButton equalsButton = new JButton ("=");           equalsButton.addActionListener(digitPressed);
        JButton commaButton = new JButton (".");            commaButton.addActionListener(digitPressed);

        add (buttonClear);
        add (new JLabel (""));
        add (new JLabel (""));
        JPanel textPanel = new JPanel();
        textPanel.setLayout(new BorderLayout());
        textPanel.add(textField, BorderLayout.CENTER);
        this.add(textPanel);
        add(button7);
        add(button8);
        add(button9);
        add(divisionButton);
        add(button4);
        add(button5);
        add(button6);
        add(multiplicationButton);
        add(button1);
        add(button2);
        add(button3);
        add(additionButton);
        add(commaButton);
        add(button0);
        add(equalsButton);
        add(substructionButton);
    }

    public JTextField getTextField() {
        return textField;
    }

    public void setTextField(String text) {
        textField.setText(text);
    }

}

数字动作监听器

package starter;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JTextField;

class DigitActionListener implements ActionListener {
    int size = 0;
    char[] tab = new char[size];

    public void pain (Graphics g, String s){
        g.drawString(s, 20, 10);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //Object source = e.getSource();
        String command = e.getActionCommand();

        if ("button0".equals(command)) {
            tab[size] = 0;
            String eq = String.valueOf(tab[size]);
            MainFrame mainFrame = new MainFrame();
            mainFrame.setTextField(eq);
            size++;
        }

    }

}

【问题讨论】:

  • “如果您能给我一些指导,我将不胜感激。” 问一个问题。
  • 另见calculator example。它使用ScriptEngine 来评估文本字段中的表达式。
  • 您需要将 MainFrame 的引用传递给 DigitiActionListener 的实例,以便它可以访问其功能
  • 谢谢大家。我认为一切都很好,但仍然看不到 textField 中的结果。单击 button0 时没有任何变化。我认为在 textField 中我会看到我的结果(在本例中是数字 0)。我对吗?你知道什么是错的吗?抱歉我的语言错误,我仍在努力。

标签: java swing actionlistener calculator


【解决方案1】:

你的问题在这里:

    if ("button0".equals(command)) {
        tab[size] = 0;
        String eq = String.valueOf(tab[size]);
        MainFrame mainFrame = new MainFrame();
        mainFrame.setTextField(eq);
        size++;
    }

您正在创建一个新的 MainFrame 对象并更改其状态,但要了解这对完全不同的显示 MainFrame 对象没有影响,并且不会更改其状态(不会更改其 JTextField 中显示的内容)。有多种可能的解决方案,但归根结底是了解 Java 引用是什么,并在适当的引用上调用方法,这里是适当的 MainFrame 对象。一个简单的解决方案是通过侦听器构造函数将 MainFrame 对象传递给您的侦听器,然后调用此引用上的方法。

例如:

class DigitActionListener implements ActionListener {
    private MainFrame mainFrame;
    int size = 0;
    char[] tab = new char[size];

    public DigitActionListener(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
    }

    public void pain (Graphics g, String s){
        g.drawString(s, 20, 10);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //Object source = e.getSource();
        String command = e.getActionCommand();

        if ("button0".equals(command)) {
            tab[size] = 0;
            String eq = String.valueOf(tab[size]);
            // MainFrame mainFrame = new MainFrame(); // **** no, don't do this
            mainFrame.setTextField(eq);
            size++;
        }
    }
}

然后在 MainFrame 本身内,执行以下操作:

// pass *this* or the current MainFrame instance, into the DigitalActionListener
DigitActionListener digitPressed = new DigitActionListener(this);

其他问题 - 学习使用数组和 ArrayList,因为这可以帮助您简化代码,从而使程序改进和调试更轻松

【讨论】:

  • 非常感谢!我会检查您的解决方案并阅读有关此问题的更多信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多