gpt4 book ai didi

java - 类 abstractButton 中的方法 addActionListener 不能应用于给定类型

转载 作者:行者123 更新时间:2023-11-29 07:50:18 25 4
gpt4 key购买 nike

我正在尝试使用 swing 制作一个计算器,但我在编译它时遇到了一个问题,这是一个错误,即类 AbstractButton 中的 addActionListener 方法无法应用于给定类型。

我的代码是

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {
private String str = "0";
JTextField l_tf = new JTextField(100);
JButton button[] = new JButton[31];
Northwindow panelNorth = new Northwindow();
Centerwindow panelCenter = new Centerwindow();

Calculator(String l_s) {
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North", panelNorth);
getContentPane().add("Center", panelCenter);

setSize(300, 400);
setVisible(true);
}

//Northwindow

class Northwindow extends JPanel {
public Northwindow() {
Dimension textf = new Dimension(50, 50);
setLayout(new GridLayout(0, 1));
setBackground(new Color(230, 230, 255));
l_tf.setPreferredSize(textf);
add(l_tf);
}
}


class Centerwindow extends JPanel {
public Centerwindow() {
setLayout(new GridLayout(0, 4));
String key[] = {"ON", "Del", "C", "+/-", "1/x", "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+", "Sqrt", "Sqr", "Sin", "Cos", "^", "log", "ln", "tan", "(", ")", "pie"};

Dimension but = new Dimension(80, 30);
for (int i = 0; i < button.length; i++) {

button[i] = new JButton(key[i]);
button[i].setPreferredSize(but);
add(button[i]);
button[i].addActionListener(this);

//before the on button is not clicked

for (i = 0; i < button.length; i++)
button[i].setEnable(false);
l_tf.setEditable(false);

}
}
}

public void actionPerformed(ActionEvent e) {
if (e.getSource == button[0]) // ON button
{
l_tf.setBackground(color.white);
button[0].setLabel("OFF");
for (i = 0; i < button.length; i++)
button[i].setEnable(true);
l_tf.setEditable(true);
l_tf.setText(str);
}
if (e.getSource == button[1]) {
}
}

public static void main(String... s) {
Calculator c = new Calculator("Calculator");
}
}

最佳答案

button[i].addActionListener(this);

这出现在 Centerwindow 中,它没有实现 ActionListener。因为 Centerwindow 是一个内部类,所以您可以访问封闭的 Calculator 实例并添加它(如果您正在尝试这样做):

button[i].addActionListener(Calculator.this);

关于java - 类 abstractButton 中的方法 addActionListener 不能应用于给定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21711628/

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