gpt4 book ai didi

Java ActionListener - 更改 actionPerformed 中的变量

转载 作者:行者123 更新时间:2023-12-01 18:08:20 26 4
gpt4 key购买 nike

我正在开始我的编程冒险,但我遇到了一个问题。我尝试使用 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);
}

}

数字 Action 监听器

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

}

}

最佳答案

您的问题在这里:

    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,因为这可以帮助您简化代码,从而使程序改进和调试变得更加容易。

关于Java ActionListener - 更改 actionPerformed 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34687880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com