一个小程序

上一篇 / 下一篇  2011-04-28 08:04:46 / 个人分类:JAVA开发

源自网络,仅供学习者参考

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;//引入类及子类
public class jisuanqi implements ActionListener
{
 String s="",s1;
 double d1,d2;
 JFrame. jf = new JFrame("小小计算器") ;//标题界面
 JTextField tf = new JTextField();
public void init()//计算器界面
 {
 Container c=jf.getContentPane();
 tf.setHorizontalAlignment(JTextField.LEFT);//文本框
 c.add(tf,"North");//文本框在界面中的位置
 JPanel pn3 = new JPanel(new BorderLayout());
 c.add(pn3,"Center");
 JPanel pn2 = new JPanel();//功能键界面
 pn2.setLayout(new BorderLayout());
 JPanel pn1 = new JPanel();//运算界面
 pn1.setLayout(new GridLayout(4,4));//在所有容器父类方法中引用GridLayout布局方法,分为四行四列
 pn3.add(pn2,"South");//功能键在界面中的位置
 pn3.add(pn1);//将容器1放到容器3中
 JButton b = new JButton("清屏");//创建按钮
 b.setForeground(Color.ORANGE);//前景字体色
 b.setBackground(Color.GRAY);//背景色
 b.addActionListener(this);//监听鼠标活动
 pn2.add(b,"Center");//将按钮加入到容器2中并设置位置
 b = new JButton("退出计算器");
 b.setForeground(Color.ORANGE);
 b.setBackground(Color.GRAY);
 b.addActionListener(this);
 pn2.add(b,"East");
 b = new JButton("1");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("2");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("3");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("加");
 b.setForeground(Color.RED);
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("4");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("5");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("6");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("减");
 b.setForeground(Color.RED);
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("7");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("8");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("9");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("乘");
 b.setForeground(Color.RED);
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("0");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton(".");
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("等于");
 b.setForeground(Color.RED);
 b.addActionListener(this);
 pn1.add(b);
 b = new JButton("除");
 b.setForeground(Color.RED);
 b.addActionListener(this);
 pn1.add(b);
 jf.setSize(350,200);//设置大小
 jf.setVisible(true);//设置为可视
}
//处理按钮按下时的动作
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
tf.setText(tf.getText()+command);
 if(command.equals("清屏")) //清零键 按下时返回初始状态
 {
  s1=null;
  s="";
  tf.setText("");//记录输入值的变量清空
 }
 else if(command.equals("退出计算器")) System.exit(0);//退出计算器键 关闭应用程序
 else if(!command.equals("乘")&&!command.equals("除")
   &&!command.equals("加")&&!command.equals("减")
   &&!command.equals("等于"))//判断输入是否为数字
  {
   if(s1==null)//判断输入是否为第一个
   s1 = command;
   else s1+=command;
   d1 = new Double(s1).doubleValue();//字符串型转换为双精度型,还原输入数字
 try
 {
 if(s.equals("加")) d1 = d1+d2;//加法运算
 else if(s.equals("减")) d1 = d2-d1;//减法运算
 else if(s.equals("乘")) d1 = d1*d2;//乘法运算
 else if(s.equals("除"))d1 = d2/d1;//除法运算
 }
 catch(Exception ex)
 {
 tf.setText("Error");//错误显示"Error"
 System.out.println(ex.getMessage());
 }
 }
 else if(!command.equals("等于")) //判断输入是否为加减乘除
 {
 s = command;
 s1 = null;
 d2 = d1;
 }
 else//输入等于时,显示运算结果
 {
 tf.setText(tf.getText()+d1);
 }
 }
public static void main(String [] args)
{
new jisuanqi().init();//初始化程序
}
}
 

TAG:

 

评分:0

我来说两句

Open Toolbar