[java]代码库
111111111111111111111111111111111111111111111111111111111111111111111
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.rmi.server.Operation;
import java.text.NumberFormat;
import java.util.LinkedList;
import javax.naming.LinkLoopException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
public class ComputerPad extends Frame implements ActionListener{
NumberButton numberButton[]; //数字按钮数组
OperationButton operationButton[]; //运算符号按钮数组
JTextField resultShow;
//小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮
JButton btn_point,btn_symbol,btn_quit,btn_lastCount,btn_equal,btn_clear; //按钮
LinkedList linkedList =new LinkedList();//负责存储数据的对象
String operation[]={"+","-","*","/"};//运算发号数组
boolean flag=false; //是否按下等号
public void f(){
JFrame frame=new JFrame("计算器");
frame.setBounds(200,100,320,250);
JPanel panel=new JPanel();
JPanel panel1=new JPanel();
numberButton=new NumberButton[10];//共有十个数字按钮
for (int i = 0; i < 10 ;i++) {
numberButton[i]=new NumberButton(i);
numberButton[i].addActionListener(this);
}
operationButton =new OperationButton[4];
for (int i = 0; i <4; i++) {
operationButton[i]=new OperationButton(operation[i]);
operationButton[i].addActionListener( this);
}
btn_point=new JButton(".");//小数点
btn_symbol=new JButton("+/-");//正负号
btn_equal=new JButton("=");//等号
btn_quit=new JButton("退格");//退格
btn_lastCount=new JButton("1/x");//倒数
btn_clear=new JButton("c");//清空
btn_clear.setForeground(Color.red);
btn_quit.setForeground(Color.red);
btn_equal.setForeground(Color.red);
btn_lastCount.setForeground(Color.blue);
btn_symbol.setForeground(Color.blue);
btn_point.setForeground(Color.blue);
btn_clear.addActionListener(this);
btn_equal.addActionListener(this);
btn_lastCount.addActionListener(this);
btn_point.addActionListener(this);
btn_quit.addActionListener(this);
btn_symbol.addActionListener(this);
resultShow=new JTextField(20);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setForeground(Color.blue);
resultShow.setFont(new Font("TimesRoman",Font.PLAIN,14));
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
resultShow.setBackground(Color.white);
resultShow.setEditable(false);
panel1.add(resultShow);
panel.setLayout(new GridLayout(4,5));//面板中的第一行
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(operationButton[0]);
panel.add(btn_clear);
//面板第二行
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operationButton[1]);
panel.add(btn_quit);
//面板中的第三行
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operationButton[2]);
panel.add(btn_lastCount);
//面板中的第四行
panel.add(numberButton[0]);
panel.add(btn_symbol);
panel.add(btn_point);
panel.add(operationButton[3]);
panel.add(btn_equal);
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel,BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
setResizable(false);
validate();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() instanceof NumberButton)//如果数遍单击数字按钮
{
NumberButton b=(NumberButton) e.getSource();
if (linkedList.size()==0) {//如果链表中没有节点
int number=b.getNumber();//获取按钮上的数字
linkedList.add(""+number);//将数据添加到链表
resultShow.setText(""+number);//将两个数字收尾相接
flag=false;
}
else if(linkedList.size()==1&&flag==false){
int number=b.getNumber();
String num=(String) linkedList.getFirst();
String s=num.concat(""+number);
linkedList.set(0, s);
resultShow.setText(s);
}
else if(linkedList.size()==1&&flag==true){
int number=b.getNumber();
linkedList.removeFirst();
linkedList.add(""+number); //将数据添加到链表
flag=false;
resultShow.setText(""+number);
}
else if(linkedList.size()==2){//链表中已有2个节点
int number=b.getNumber();//获取按钮上数字
linkedList.add(""+number);//链表添加第三个节点,注意:第二个节点是运算符号。
resultShow.setText(""+number);
}
else if(linkedList.size()==3){//链表中已有3个节点
int number=b.getNumber();
String num=(String) linkedList.getLast();
String s=num.concat(""+number);//与链表第3个节点中的数字收尾相接
linkedList.set(2, s);
resultShow.setText(s);
}
}
else if(e.getSource() instanceof OperationButton){//如果单击运算符号按钮
OperationButton b=(OperationButton) e.getSource();
if(linkedList.size()==1){
String fuhao=b.getOperator();
linkedList.add(fuhao);
}
else if(linkedList.size()==2){
String fuhao=b.getOperator();
linkedList.set(1, fuhao);
}
else if(linkedList.size()==3){
String fuhao=b.getOperator();
String number1=(String) linkedList.getFirst();
String number2=(String) linkedList.getLast();
String yunsuanfuhao=(String) linkedList.get(1);
try {
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
double n=0;
if(yunsuanfuhao.equals("+")){
n=n1+n2;
}
else if(yunsuanfuhao.equals("-")){
n=n1-n2;
}
else if(yunsuanfuhao.equals("*")){
n=n1*n2;
}
else if(yunsuanfuhao.equals("/")){
n=n1/n2;
}
linkedList.clear();//根据链表内已有的数进行运算,其结果作为新的运算数值
linkedList.add(""+n);
linkedList.add(fuhao);
resultShow.setText(""+n);
} catch (Exception e2) {
// TODO: handle exception
}
}
}
//(1)
else if(e.getSource()==btn_equal){
flag=true;
if(linkedList.size()==1||linkedList.size()==2){
String num=(String) linkedList.getFirst();
resultShow.setText(""+num);
}
else if(linkedList.size()==3){
String number1=(String) linkedList.getFirst();
String number2=(String) linkedList.getLast();
String yunsuanfuhao=(String) linkedList.get(1);
try {
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
double n=0;
if(yunsuanfuhao.equals("+")){
n=n1+n2;
}
else if(yunsuanfuhao.equals("-")){
n=n1-n2;
}
else if(yunsuanfuhao.equals("*")){
n=n1*n2;
}
else if(yunsuanfuhao.equals("/")){
n=n1/n2;
}
resultShow.setText(""+n);
linkedList.set(0,""+n);
linkedList.removeLast();//将链表中的第二个运算数删除
linkedList.removeLast();//再将运算符号删除
} catch (Exception e2) {
// TODO: handle exception
}
}
}
//(2)
else if(e.getSource()==btn_point){
if(linkedList.size()==0){
flag=false;
}
else if(linkedList.size()==1) {
String dot=btn_point.getLabel();//获取按钮上的小数点
String num=(String) linkedList.getFirst();
String s=null;
if(num.indexOf(dot)==-1){//嫁娶数字中不含小数点
s=num.concat(dot);
linkedList.set(0, s);
}
else{
s=num;
}
linkedList.set(0, s);
resultShow.setText(s);
}
else if(linkedList.size()==3){
String dot=btn_point.getLabel();//获取按钮上的小数点
String num=(String) linkedList.getLast();
String s=null;
if(num.indexOf(dot)==-1){
s=num.concat(dot);
linkedList.set(2, s);
}
else{
s=num;
}
resultShow.setText(s);
}
}
//(3)
else if(e.getSource()==btn_quit){
if(linkedList.size()==1){
String num=(String) linkedList.getFirst();
if(num.length()>=1){
num=num.substring(0,num.length()-1);
linkedList.set(0, num);
resultShow.setText(num);
}else{
linkedList.removeLast();
resultShow.setText("0");
}
}
else if(linkedList.size()==3){
String num=(String) linkedList.getLast();
if(num.length()>=1){
num=num.substring(0, num.length()-1);
linkedList.set(2, num);
}
else{
linkedList.removeLast();
resultShow.setText("0");
}
}
}
//(4)
else if(e.getSource()==btn_symbol){
if(linkedList.size()==1){
String number1=(String) linkedList.getFirst();
try {
double d=Double.parseDouble(number1);
d=-1*d;
String str=String.valueOf(d);
linkedList.set(0, str);
resultShow.setText(str);
} catch (Exception e2) {
// TODO: handle exception
}
}
else if(linkedList.size()==3){
String number2=(String) linkedList.getLast();
try {
double d=(Double) linkedList.getLast();
d=-1*d;
String str=String.valueOf(d);
linkedList.set(2, str);
resultShow.setText(str);
} catch (Exception e2) {
// TODO: handle exception
}
}
}
//(5)
else if(e.getSource()==btn_lastCount){
if(linkedList.size()==1||linkedList.size()==2){
String number1=(String) linkedList.getFirst();
try {
double d=Double.parseDouble(number1);
d=1.0/d;
String str=String.valueOf(d);
linkedList.set(0, str);
resultShow.setText(str);
} catch (Exception e2) {
// TODO: handle exception
}
}
else if(linkedList.size()==3){
String number2=(String) linkedList.getLast();
try {
double d=Double.parseDouble(number2);
d=1.0/d;
String str=String.valueOf(d);
linkedList.set(0, str);
} catch (Exception e2) {
// TODO: handle exception
}
}
}
//(6)
else if(e.getSource()==btn_clear){
flag=false;
resultShow.setText("0");
linkedList.clear();
}
}
public static void main(String[] args) {
ComputerPad c=new ComputerPad();
c.f();
}
}
2222222222222222222222222222222222222222222222222222222
import java.awt.Button;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
public class NumberButton extends Button{
int number;
public NumberButton(int number) {
super(" "+number);// public Button(String label) throws HeadlessException { GraphicsEnvironment.checkHeadless();this.label = label; } 构造一个带指定标签的按钮。
this.number=number;
setForeground(Color.blue);
}
public int getNumber() {
return number;
}
}
3333333333333333333333333333333333333333333333333333333333333
import java.awt.Button;
import java.awt.Color;
public class OperationButton extends Button{
String operator;
public OperationButton(String s){
super(s);
operator=s;
setForeground(Color.red);
}
public String getOperator(){
return operator;
}
}
by: 发表于:2017-06-21 17:22:43 顶(0) | 踩(0) 回复
??
回复评论