java GUI 计算器代码
package cn.itcast.chapter11;
import java.awt.*;
//import java.awt.event.*;
import javax.swing.*;
public class Text{
public Text() {
StringBuffer str;//要显示的字符串
JFrame jf=new JFrame("计算器");//创建一个窗口
jf.setSize(600,500);
jf.setLocation(600,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new BorderLayout());
JPanel jpanel1=new JPanel();//为按钮创建panel组件
JPanel jpanel2=new JPanel();//为文本框和CE键创建panel组件
JTextField jtf=new JTextField(45);//创建文本框
jtf.setEditable(false);//设置文本不可编辑
jpanel1.setLayout(new GridLayout(4,4));
JButton but1=new JButton("1");//创建按钮
JButton but2=new JButton("2");
JButton but3=new JButton("3");
JButton but10=new JButton("+");
JButton but4=new JButton("4");
JButton but5=new JButton("5");
JButton but6=new JButton("6");
JButton but11=new JButton("-");
JButton but7=new JButton("7");
JButton but8=new JButton("8");
JButton but9=new JButton("9");
JButton but12=new JButton("*");
JButton but16=new JButton(".");
JButton but0=new JButton("0");
JButton but14=new JButton("=");
JButton but13=new JButton("/");
JButton but15=new JButton("CE");
jpanel1.add(but1);//添加按钮
jpanel1.add(but2);
jpanel1.add(but3);
jpanel1.add(but10);
jpanel1.add(but4);
jpanel1.add(but5);
jpanel1.add(but6);
jpanel1.add(but11);
jpanel1.add(but7);
jpanel1.add(but8);
jpanel1.add(but9);
jpanel1.add(but12);
jpanel1.add(but16);
jpanel1.add(but0);
jpanel1.add(but14);
jpanel1.add(but13);
jpanel2.add(jtf);
jpanel2.add(but15);
jf.add(jpanel2,BorderLayout.NORTH);
jf.add(jpanel1,BorderLayout.CENTER);
jf.setVisible(true);
MyActionListener ac1=new MyActionListener();
but1.addActionListener(ac1);//添加动作监听器
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
jtf.setText(e.getActionCommand());
}
}
public static void main(String[] args) {
new Text();
}
}
后面动作监听器那里错了,为什么,成员内部类不能继承java自己定义的类吗?
可以了,你之前的jtf变量,你自定义的监听器肯定访问不了,可以运行了,另外,下回发,记得格式化一下代码.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.awt.event.*;
import javax.swing.*;
public class Main {
private JTextField jtf = null;
public Main() {
StringBuffer str;//要显示的字符串
JFrame jf = new JFrame("计算器");//创建一个窗口
jf.setSize(600, 500);
jf.setLocation(600, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new BorderLayout());
JPanel jpanel1 = new JPanel();//为按钮创建panel组件
JPanel jpanel2 = new JPanel();//为文本框和CE键创建panel组件
jtf = new JTextField(45);//创建文本框
jtf.setEditable(false);//设置文本不可编辑
jpanel1.setLayout(new GridLayout(4, 4));
JButton but1 = new JButton("1");//创建按钮
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but10 = new JButton("+");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but11 = new JButton("-");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but12 = new JButton("*");
JButton but16 = new JButton(".");
JButton but0 = new JButton("0");
JButton but14 = new JButton("=");
JButton but13 = new JButton("/");
JButton but15 = new JButton("CE");
jpanel1.add(but1);//添加按钮
jpanel1.add(but2);
jpanel1.add(but3);
jpanel1.add(but10);
jpanel1.add(but4);
jpanel1.add(but5);
jpanel1.add(but6);
jpanel1.add(but11);
jpanel1.add(but7);
jpanel1.add(but8);
jpanel1.add(but9);
jpanel1.add(but12);
jpanel1.add(but16);
jpanel1.add(but0);
jpanel1.add(but14);
jpanel1.add(but13);
jpanel2.add(jtf);
jpanel2.add(but15);
jf.add(jpanel2, BorderLayout.NORTH);
jf.add(jpanel1, BorderLayout.CENTER);
jf.setVisible(true);
MyActionListener ac1 = new MyActionListener();
but1.addActionListener(ac1);//添加动作监听器
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
jtf.setText(e.getActionCommand());
}
}
public static void main(String[] args) {
new Main();
}
}
监听器那里还是有错误啊,为什么?
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Text.MyActionListener)
ActionListener cannot be resolved to a type
ActionEvent cannot be resolved to a type