日期:2014-05-20 浏览次数:20865 次
package com.zhyx.swing;
public class TestCalculator extends JFrame {
/**
* TestCalculator初始化方法
*/
public TextField t1;
public TestCalculator() {
setLayout(new BorderLayout());
JPanel p1=new JPanel();
t1=new TextField();
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(4,4));
String []text={"7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+","="};
Button btn[] = new Button[17];
int i;
for(i=0;i<btn.length;i++){
btn[i]=new Button(text[i]);
p2.add(btn[i]);
btn[i].addActionListener(new Listener());
}
btn[0].setSize(20,30);
p1.setSize(300,10);
p1.setBackground(Color.gray);
p2.setSize(500,30);
t1.setBackground(Color.RED);
t1.setText("0.");
add(p1,BorderLayout.NORTH);
add(t1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
TestCalculator test=new TestCalculator();
}
}
class Listener implements ActionListener{
public Listener(){
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
TextField t1=new TextField();
if(obj instanceof Button){
//如果按钮里的String内容为int型,则返回到text上
try{
int a=Integer.parseInt(((Button) obj).getLabel());
t1.setText(""+a);
t1.getText();
if(".".equals(((Button) obj).getLabel())){
t1.setText(""+a);
t1.getText();
}
}catch(Exception e1){
System.out.println("ddd");
}
}
//每一次点击完按钮后Text上的内容要清空
// t1.setText("");
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TestCalculator2 extends JFrame {
public TextField getT1() { // modified
return t1;
}
/**
* TestCalculator初始化方法
*/
private TextField t1;
public TestCalculator2() {
setLayout(new BorderLayout());
JPanel p1=new JPanel();
t1=new TextField();
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(4,4));
String []text={"7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+","="};
Button btn[] = new Button[17];
int i;
for(i=0;i<btn.length;i++){
btn[i]=new Button(text[i]);
p2.add(btn[i]);
btn[i].addActionListener(new Listener(this)); // modified
}
btn[0].setSize(20,30);
p1.setSize(300,10);
p1.setBackground(Color.gray);
p2.setSize(500,30);
t1.setBackground(Color.RED);
t1.setText("0.");
add(p1,BorderLayout.NORTH);
add(t1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
TestCalculator2 test=new TestCalculator2();
}
}
class Listener implements ActionListener{
private TextField t1=null; // modified
public Listener(TestCalculator2 cal){ // modified
t1=cal.getT1();
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
// TextField t1=new TextField();
if(obj instanceof Button){
//如果按钮里的String内容为int型,则返回到text上
try{
int a=Integer.parseInt(((Button) obj).getLabel());
t1.setText(""+a);
t1.getText();
if(".".equals(((Button) obj).getLabel())){
t1.setText(""+a);
t1.getText();
}
}catch(Exception e1){
System.out.println("ddd");
}
}
//每一次点击完按钮后Text上的内容要清空
// t1.setText("");
}
}