Java ActionListener - 在actionPerformed中更改变量

时间:2016-01-08 23:36:18

标签: java swing actionlistener calculator

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

$collection= $company->car;
$company->boat->each(function ($item) use ($collection) {
    $collection->add($item);
});

大型机

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

DigitActionListener

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

}

1 个答案:

答案 0 :(得分:4)

你的问题在这里:

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

其他问题 - 学会使用然后使用数组和ArrayLists,因为这可以帮助您简化代码,从而使程序改进和调试更多更容易。