import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
static int count=0;
private JLabel promptLabel;
private JTextField inputField;
private JTextArea outputArea;
private JButton j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,x1,x2,y1,y2,s1,s2;
private int all=0,jiahao=0,j11,j22;
public test(){
super("ceshi StringTokenizer");
Container container=getContentPane();
container.setLayout(new FlowLayout());
promptLabel=new JLabel("计算器");
container.add(promptLabel);
inputField=new JTextField(20);
inputField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
StringTokenizer tokens=new StringTokenizer(event.getActionCommand());
outputArea.setText("Number of elements"+tokens.countTokens()+"\nThe tokens are:\n");
while(tokens.hasMoreTokens())
outputArea.append(tokens.nextToken()+"\n");
}
});
container.add(inputField);
j1=new JButton("1");
j2=new JButton("2");
j3=new JButton("3");
x1=new JButton("*");
x2=new JButton("/");
j4=new JButton("4");
j5=new JButton("5");
j6=new JButton("6");
y1=new JButton("+");
y2=new JButton("-");
j7=new JButton("7");
j8=new JButton("8");
j9=new JButton("9");
s1=new JButton("=");
j0=new JButton("0");
j1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
j11=1;
inputField.setText("\t\t 1\n");
count=1;
}
});
j2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
j22=2;
if(count==2)
all+=j22;
}
});
y1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(count==1)
{ all=j11;
count=2;}
else
inputField.setText("\t\t 1\n");
}
});
s1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
String All="";
All+=all;
inputField.setText(All);
}
});
container.add(j9);
container.add(j8);
container.add(j7);
container.add(x1);
container.add(x2);
container.add(j6);
container.add(j5);
container.add(j4);
container.add(y1);
container.add(y2);
container.add(j3);
container.add(j2);
container.add(j1);
container.add(j0);
container.add(s1);
outputArea=new JTextArea(10,20);
outputArea.setEditable(false);
container.add(new JScrollPane(outputArea));
setSize(275,500);
setVisible(true);
}
public static void main(String args[]){
test application=new test();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//------------------------------------------------------------------------------------------
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
static int all=0,but;
static String fuhao;
private JLabel promptLabel;
private JTextField inputField;
private JTextArea outputArea;
private JButton j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,x1,x2,y1,y2,s1,s2;
private int jiahao=0;
public test(){
super("ceshi StringTokenizer");
Container container=getContentPane();
container.setLayout(new FlowLayout());
promptLabel=new JLabel("计算器");
container.add(promptLabel);
inputField=new JTextField(20);
inputField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
StringTokenizer tokens=new StringTokenizer(event.getActionCommand());
outputArea.setText("Number of elements"+tokens.countTokens()+"\nThe tokens are:\n");
while(tokens.hasMoreTokens())
outputArea.append(tokens.nextToken()+"\n");
}
});
container.add(inputField);
j1=new JButton("1");
j2=new JButton("2");
j3=new JButton("3");
x1=new JButton("*");
x2=new JButton("/");
j4=new JButton("4");
j5=new JButton("5");
j6=new JButton("6");
y1=new JButton("+");
y2=new JButton("-");
j7=new JButton("7");
j8=new JButton("8");
j9=new JButton("9");
s1=new JButton("=");
j0=new JButton("0");
j1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
but=1;
inputField.setText("\t\t 1\n");
}
});
j2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
but=2;
inputField.setText("\t\t 2\n");
}
}
);
y1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(but==1)
{ all+=1;
fuhao="+";}
}
});
s1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if((fuhao=="+")&&(but==2))
{ all+=but;
String All="\t\t ";
All+=all;
inputField.setText(All);
}
}
});
container.add(j9);
container.add(j8);
container.add(j7);
container.add(x1);
container.add(x2);
container.add(j6);
container.add(j5);
container.add(j4);
container.add(y1);
container.add(y2);
container.add(j3);
container.add(j2);
container.add(j1);
container.add(j0);
container.add(s1);
outputArea=new JTextArea(10,18);
outputArea.setEditable(false);
container.add(new JScrollPane(outputArea));
setSize(275,500);
setVisible(true);
}
public static void main(String args[]){
test application=new test();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
评论