简体   繁体   English

Java ActionListener-在actionPerformed中更改变量

[英]Java ActionListener - change variable in actionPerformed

I'm starting my adventure with programming and I've got one problem. 我从编程开始我的冒险,但是我遇到了一个问题。 I try make a simple calculator using awt. 我尝试使用awt创建一个简单的计算器。 I can't go further because I don't know, how to change one variable - textField initialized in MainFrame. 我无法继续,因为我不知道如何更改一个变量-在MainFrame中初始化的textField。 I want to change it in actionPerformed. 我想在actionPerformed中进行更改。 Here's my code and I'll be grateful if You'll give me some guidance. 这是我的代码,如果您能给我一些指导,我将不胜感激。 Thanks! 谢谢!

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();
    }
}

MainFrame 大型机

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);
    }

}

DigitActionListener DigitActionListener

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++;
        }

    }

}

Your problem is here: 您的问题在这里:

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

You're creating a new MainFrame object and changing its state, but understand that this will have no effect on the completely distinct displayed MainFrame object and will not change its state whatsoever (will not change what is displayed within its JTextField). 您正在创建一个新的MainFrame对象并更改其状态,但是要了解,这将对完全不同的显示的MainFrame对象没有任何影响,也不会更改其状态(不会更改其JTextField中显示的内容)。 There are a variety of possible solutions, but it all boils down to understanding what Java references are, and calling methods on the appropriate reference, here the appropriate MainFrame object. 有多种可能的解决方案,但这全部归结为了解什么是Java引用,以及在适当的引用(这里是适当的MainFrame对象)上调用方法。 A simple solution could be for to pass the MainFrame object into your listener via a listener constructor, and then call the methods on this reference. 一个简单的解决方案是通过侦听器构造函数将MainFrame对象传递给侦听器,然后在此引用上调用方法。

For example: 例如:

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++;
        }
    }
}

and then within MainFrame itself, do something like: 然后在MainFrame本身中执行以下操作:

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

Other issues -- learn to use and then use arrays and ArrayLists, as this can help you simplify your code, and thus make program improvement and debugging much easier. 其他问题-学习使用数组,然后使用数组和ArrayList,因为这可以帮助您简化代码,从而使程序改进和调试更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM