这是实现类
package games;
import java.awt.Color;
import javax.swing.JFrame;
public class game2 {
public static void main(String[] args) {
game2_2 win=new game2_2();
win.setTitle("计算器");
win.setBackground(Color.cyan);
win.setBounds(20,20,350,400);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
这是我做的计算器的界面,可是事件源和监视器不知道怎么写。我一直搞不懂,我看了别人做的,照猫画虎,结构一样却是怎么都出错,,望大神随意提点一下,随便一下都可以。看书也看不懂。在这里谢过了。
package games;
import java.awt.*;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.DefaultPersistenceDelegate;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.xml.transform.Result;
import Textw.emmm;
public class game2_2 extends JFrame implements ActionListener{
String key[]={"7", "8", "9", "/" , "4", "5", "6",
"*", "1", "2", "3", "-", "0", ".", "+", "=" };
JButton k[]=new JButton[key.length];
private double resultNum =0.0;
List<String>lists=new ArrayList<String>();
JTextField txt;
boolean firstnumber = true;
public game2_2(){
init();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
void init(){
JPanel h=new JPanel();
h.setLayout(new GridLayout(4,4));
for(int i=0;i<key.length;i++){
k[i]=new JButton(key[i]);
h.add(k[i]);
k[i].setBackground(Color.white);
k[i].addActionListener(this);
}
JPanel h2=new JPanel();
txt=new JTextField(13);
h2.add(txt);
JButton c=new JButton("c");
c.addActionListener(this);
h2.add(c);
add(h2,BorderLayout.NORTH);
add(h,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
String text=txt.getText();
String label=e.getActionCommand();
if(label.equals("c")){
txt.setText("");
}
else if(label.equals("0123456789."));
txt.setText(txt.getText()+label);
}
}