「java小程序计算器」微信小程序 计算器
今天给各位分享java小程序计算器的知识,其中也会对微信小程序 计算器进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java编写计算器!
- 2、用java.EE编写计算器程序代码
- 3、用JAVA制作一个小程序,计算两个数的加减乘除,用Applet实现
- 4、求java编写的租金计算器小程序
- 5、java:编写一个计算器小程序,要求可以做加减乘除运算
- 6、求各位大神看看小弟这个java小程序为什么一输入小数就报错..求解
java编写计算器!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame {
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField;//计算结果显示区
private String lastCommand;//保存+,-,*,/,=命令
private double result;//保存计算结果
private boolean start;//判断是否为数字的开始
public Calculator() {
super("计算器");
container=getContentPane();
layout=new GridBagLayout();
container.setLayout(layout);
constraints=new GridBagConstraints();
start=true;
result=0;
lastCommand = "=";
displayField=new JTextField(20);
displayField.setText("0.0");
displayField.setCaretColor(Color.red);
displayField.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=100;
constraints.weighty=100;
layout.setConstraints(displayField,constraints);
container.add(displayField);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
addButton("7",0,2,1,1,insert);
addButton("8",1,2,1,1,insert);
addButton("9",2,2,1,1,insert);
addButton("/",3,5,1,1,command);
addButton("4",0,3,1,1,insert);
addButton("5",1,3,1,1,insert);
addButton("6",2,3,1,1,insert);
addButton("*",3,4,1,1,command);
addButton("1",0,4,1,1,insert);
addButton("2",1,4,1,1,insert);
addButton("3",2,4,1,1,insert);
addButton("-",3,3,1,1,command);
addButton("0",1,5,1,1,insert);
addButton("=",2,5,1,1,command);
addButton(".",0,5,1,1,insert);
addButton("+",3,2,1,1,command);
setSize(180,200);
setVisible(true);
}
private void addButton(String label,int row,int column,int with,int height,ActionListener listener) {
JButton button=new JButton(label);
constraints.gridx=row;
constraints.gridy=column;
constraints.gridwidth=with;
constraints.gridheight=height;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button,constraints);
container.add(button);
}
private class InsertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input=event.getActionCommand();
if (start) {
displayField.setText("");
start=false;
displayField.setText(displayField.getText()+input);
}
}
}
private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String command=evt.getActionCommand();
if(start) {
lastCommand=command;
}else {
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command;
start=true;
}
}
}
public void calculate(double x) {
if (lastCommand.equals("+")) result+= x;
else if (lastCommand.equals("-")) result-=x;
else if (lastCommand.equals("*")) result*=x;
else if (lastCommand.equals("/")) result/=x;
else if (lastCommand.equals("=")) result=x;
displayField.setText(""+ result);
}
public static void main(String []args) {
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
用java.EE编写计算器程序代码
java EE是java企业级开发平台的意思,实在是看不出跟计算器这种小程序有什么关联。不知道楼主要找的是不是这个。
package ex1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;
public class Calcutor extends JFrame {
private JTextField tf;
private JPanel panel1, panel2, panel3, panel4;
private JMenuBar myBar;
private JMenu menu1, menu2, menu3;
private JMenuItem editItem1, editItem2, help1, help2, help3;
private JRadioButtonMenuItem seeItem1, seeItem2;//单选框
private JCheckBoxMenuItem seeItem3;//复选框
private ButtonGroup bgb;
private String back;
private boolean IfResult = true, flag = false;
private String oper = "=";
private double result = 0;
private Num numActionListener;
private DecimalFormat df;
public Calcutor(){
super("科学计算器");//设置标题栏
df = new DecimalFormat("#.####");//保留四位小数
this.setLayout(new BorderLayout(10, 5));
panel1 = new JPanel(new GridLayout(1, 3, 10, 10));
panel2 = new JPanel(new GridLayout(5, 6, 5, 5));//5行6列
panel3 = new JPanel(new GridLayout(5, 1, 5, 5));
panel4 = new JPanel(new BorderLayout(5, 5));
/**
* 菜单栏
*/
myBar = new JMenuBar();
menu1 = new JMenu("编辑(E)");
menu2 = new JMenu("查看(V)");
menu3 = new JMenu("帮助(H)");
menu1.setFont(new Font("宋体", Font.PLAIN, 12));
menu2.setFont(new Font("宋体", Font.PLAIN, 12));
menu3.setFont(new Font("宋体", Font.PLAIN, 12));
/**
* 编辑栏
*/
editItem1 = new JMenuItem("复制(C) Ctrl+C");
editItem2 = new JMenuItem("粘贴(P) Ctrl+V");
editItem1.setFont(new Font("宋体",Font.PLAIN,12));
editItem2.setFont(new Font("宋体",Font.PLAIN,12));
/**
* 查看栏
*/
seeItem1 = new JRadioButtonMenuItem("科学型(T)");
seeItem2 = new JRadioButtonMenuItem("标准型(S)");
seeItem3 = new JCheckBoxMenuItem("数字分组(I)");
seeItem1.setFont(new Font("宋体",Font.PLAIN,12));
seeItem2.setFont(new Font("宋体",Font.PLAIN,12));
seeItem3.setFont(new Font("宋体",Font.PLAIN,12));
/**
* 帮助栏
*/
help1 = new JMenuItem("帮助主题(H)");
help2 = new JMenuItem("关于计算器(A)");
help1.setFont(new Font("宋体",Font.PLAIN,12));
help2.setFont(new Font("宋体",Font.PLAIN,12));
bgb = new ButtonGroup();//选项组
menu1.add(editItem1);
menu1.add(editItem2);
menu2.add(seeItem1);
menu2.add(seeItem2);
menu2.addSeparator();//添加一条分割线
menu2.add(seeItem3);
menu3.add(help1);
menu3.addSeparator();//添加一条分割线
menu3.add(help2);
myBar.add(menu1);
myBar.add(menu2);
myBar.add(menu3);
this.setJMenuBar(myBar);
numActionListener = new Num();//实现数字监听
/**
* 文本域,即为计算器的屏幕显示区域
*/
tf = new JTextField();
tf.setEditable(false);//文本区域不可编辑
tf.setBackground(Color.white);//文本区域的背景色
tf.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐
tf.setText("0");
tf.setBorder(BorderFactory.createLoweredBevelBorder());
init();//对计算器进行初始化
}
/**
* 初始化操作
* 添加按钮
*/
private void init(){
addButton(panel1, "Backspace", new Clear(), Color.red);
addButton(panel1, "CE", new Clear(), Color.red);
addButton(panel1, "C", new Clear(), Color.red);
addButton(panel2, "1/x", new Signs(), Color.magenta);
addButton(panel2, "log", new Signs(), Color.magenta);
addButton(panel2, "7", numActionListener, Color.blue);
addButton(panel2, "8", numActionListener, Color.blue);
addButton(panel2, "9", numActionListener, Color.blue);
addButton(panel2, "÷", new Signs(), Color.red);
addButton(panel2, "n!", new Signs(), Color.magenta);
addButton(panel2, "sqrt", new Signs(), Color.magenta);
addButton(panel2, "4", numActionListener, Color.blue);
addButton(panel2, "5", numActionListener, Color.blue);
addButton(panel2, "6", numActionListener, Color.blue);
addButton(panel2, "×", new Signs(), Color.red);
addButton(panel2, "sin", new Signs(), Color.magenta);
addButton(panel2, "x^2", new Signs(), Color.magenta);
addButton(panel2, "1", numActionListener, Color.blue);
addButton(panel2, "2", numActionListener, Color.blue);
addButton(panel2, "3", numActionListener, Color.blue);
addButton(panel2, "-", new Signs(), Color.red);
addButton(panel2, "cos", new Signs(), Color.magenta);
addButton(panel2, "x^3", new Signs(), Color.magenta);
addButton(panel2, "0", numActionListener, Color.blue);
addButton(panel2, "-/+", new Clear(), Color.blue);
addButton(panel2, ".", new Dot(), Color.blue);
addButton(panel2, "+", new Signs(), Color.red);
addButton(panel2, "tan", new Signs(), Color.magenta);
addButton(panel2, "%", new Signs(), Color.magenta);
addButton(panel2, "π", numActionListener, Color.orange);
addButton(panel2, "e", numActionListener, Color.orange);
addButton(panel2, "′″", new Signs(), Color.orange);
addButton(panel2, "=", new Signs(), Color.red);
JButton btns = new JButton("计算器");
btns.setBorder(BorderFactory.createLoweredBevelBorder());
btns.setEnabled(false);//按钮不可操作
btns.setPreferredSize(new Dimension(20, 20));
panel3.add(btns);//加入按钮
addButton(panel3, "MC", null, Color.red);
addButton(panel3, "MR", null, Color.red);
addButton(panel3, "MS", null, Color.red);
addButton(panel3, "M+", null, Color.red);
panel4.add(panel1, BorderLayout.NORTH);
panel4.add(panel2, BorderLayout.CENTER);
this.add(tf, BorderLayout.NORTH);
this.add(panel3, BorderLayout.WEST);
this.add(panel4);
pack();
this.setResizable(false);//窗口不可改变大小
this.setLocation(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 统一设置按钮的的使用方式
* @param panel
* @param name
* @param action
* @param color
*/
private void addButton(JPanel panel, String name, ActionListener action, Color color){
JButton bt = new JButton(name);
panel.add(bt);//在面板上增加按钮
bt.setForeground(color);//设置前景(字体)颜色
bt.addActionListener(action);//增加监听事件
}
/**
* 计算器的基础操作(+ - × ÷)
* @param x
*/
private void getResult (double x){
if(oper == "+"){result += x;}
else if(oper == "-"){result -= x;}
else if(oper == "×"){result *= x;}
else if(oper == "÷"){result /= x;}
else if(oper == "="){result = x;}
tf.setText(df.format(result));
}
/**
* 运算符号的事件监听
*/
class Signs implements ActionListener{
public void actionPerformed(ActionEvent e) {
/*
* 用ActionEvent对象的getActionCommand()方法
* 取得与引发事件对象相关的字符串
*/
String str = e.getActionCommand();
/* sqrt求平方根 */
if(str.equals("sqrt")){
double i = Double.parseDouble(tf.getText());
if(i=0){
/*
* String.valueOf() 转换为字符串
* df.format() 按要求保留四位小数
* Math.sqrt() 求算数平方根
*/
tf.setText(String.valueOf(df.format(Math.sqrt(i))));
}
else{
tf.setText("负数不能开平方根");
}
}
/* log求常用对数 */
else if(str.equals("log")){
double i = Double.parseDouble(tf.getText());
if(i0){
tf.setText(String.valueOf(df.format(Math.log(i))));
}else{
tf.setText("负数不能求对数");
}
}
/* %求百分比 */
else if(str.equals("%")){
tf.setText(df.format(Double.parseDouble(tf.getText()) / 100));
}
/* 1/x求倒数 */
else if(str.equals("1/x")){
if(Double.parseDouble(tf.getText()) == 0){
tf.setText("除数不能为零");
}else{
tf.setText(df.format(1 / Double.parseDouble(tf.getText())));
}
}
/* sin求正弦函数 */
else if(str.equals("sin")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.sin(i))));
}
/* cos求余弦函数 */
else if(str.equals("cos")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.cos(i))));
}
/* tan求正切函数 */
else if(str.equals("tan")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.tan(i))));
}
/* n!求阶乘 */
else if(str.equals("n!")){
double i = Double.parseDouble(tf.getText());
if((i%2==0)||(i%2==1))//判断为整数放进行阶乘操作
{
int j = (int)i;//强制类型转换
int result=1;
for(int k=1;k=j;k++)
result *= k;
tf.setText(String.valueOf(result));
}
else
{
tf.setText("无法进行阶乘");
}
}
/* x^2求平方 */
else if(str.equals("x^2")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i)));
}
/* x^3求立方 */
else if(str.equals("x^3")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i*i)));
}
/* ′″角度转换 */
/**
* 将角度值转换成弧度值,方便三角函数的计算
*/
else if(str.equals("′″")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(i/180*Math.PI));
}
else{
if(flag){
IfResult = false;
}
if(IfResult){
oper = str;
}else{
getResult(Double.parseDouble(tf.getText()));
oper = str;
IfResult = true;
}
}
}
}
/**
* 清除按钮的事件监听
*/
class Clear implements ActionListener{
public void actionPerformed(ActionEvent e) {
/*
* 用ActionEvent对象的getActionCommand()方法
* 取得与引发事件对象相关的字符串
*/
String str = e.getActionCommand();
if(str == "C"){
tf.setText("0");
IfResult = true;
result = 0;
}else if(str == "-/+"){
double i = 0 - Double.parseDouble(tf.getText().trim());
tf.setText(df.format(i));
}else if(str == "Backspace"){
if(Double.parseDouble(tf.getText()) 0){
if(tf.getText().length() 1){
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
//使用退格删除最后一位字符
}else{
tf.setText("0");
IfResult = true;
}
}else{
if(tf.getText().length() 2){
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
}else{
tf.setText("0");
IfResult = true;
}
}
}else if(str == "CE"){
tf.setText("0");
IfResult = true;
}
}
}
/**
* 数字输入的事件监听
*/
class Num implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if(IfResult){
tf.setText("");
IfResult = false;
}
if(str=="π")
{
tf.setText(String.valueOf(Math.PI));
}
else if(str=="e")
{
tf.setText(String.valueOf(Math.E));
}
else{
tf.setText(tf.getText().trim() + str);
if(tf.getText().equals("0")){
tf.setText("0");
IfResult = true;
flag = true;
}
}
}
}
/**
* 小数点的事件监听
*/
class Dot implements ActionListener{
public void actionPerformed(ActionEvent e) {
IfResult = false;
if(tf.getText().trim().indexOf(".") == -1){
tf.setText(tf.getText() + ".");
}
}
}
/**
* main方法
*/
public static void main(String[] args) {
new Calcutor().setVisible(true);
}
}
用JAVA制作一个小程序,计算两个数的加减乘除,用Applet实现
下面两个可以么,是我做实验答辩时用到的!
import java.awt.*;//AWT核心包
import java.awt.event.*;//提供事件类和监听器
public class Counter extends Frame implements ActionListener
{
TextField t=new TextField("");//文本框
Panel p1=new Panel();//new一个panel,用于存放数字键和符号键。
Panel p2=new Panel();//new一个panel,用于存放开方、平方、和清除键。
Button[] b=new Button[10];//实例化Button对象
Button bAdd=new Button("加");
Button bSub=new Button("减");
Button bMul=new Button("乘以");
Button bPoint=new Button(".");
Button bDiv=new Button("除以");
Button bEqual=new Button("等於");
Button bSqrt=new Button("开方");
Button bPow=new Button("平方");
Button bNull=new Button("清除");
String str1=""; //str1和str2存放两个输入的数
String str2="";
String operator=null; //存放加减乘除以及开平方的符号
boolean first=true; //检验输入的是否为第一个数
int countOper=0; //累计输入符号的个数,连加连减等操作中会用到
double result=0.0; //暂存结果
double num1=0.0,num2=0.0; //两个输入的数做运算时转化为double存放
boolean error=false; //检验除数是否为0
//构造方法
public Counter()
{
Frame s=new Frame("计算器");//创建Frame
for(int i=0;i10;i++)//利用for循环将数字键添加进p1中
{
b[i]=new Button(String.valueOf(i));
p1.add(b[i]);
b[i].setActionCommand("number");
b[i].setForeground(new Color(150,20,20));
b[i].addActionListener(this);//调用addActionListener()方法注册事件监听器
}
p1.add(bPoint);
bPoint.setActionCommand("number");
p1.add(bAdd); //数字键,符号键放置在panel的p1中
p1.add(bSub);
p1.add(bMul);
p1.add(bDiv);
p1.add(bEqual);
p2.add(bSqrt);//开方键,平方键,清除键放置在panel的p2中
p2.add(bPow);
p2.add(bNull);
bAdd.setActionCommand("oper");
bSub.setActionCommand("oper");
bMul.setActionCommand("oper");
bDiv.setActionCommand("oper");
bAdd.setForeground(Color.red);//为组键设计颜色
bSub.setForeground(Color.red);
bMul.setForeground(Color.red);
bDiv.setForeground(Color.red);
bPoint.setForeground(Color.black);
bEqual.setForeground(Color.red);
bSqrt.setForeground(Color.blue);
bPow.setForeground(Color.blue);
bNull.setForeground(Color.blue);
bAdd.addActionListener(this);//调用addActionListener()方法注册事件监听器
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bPoint.addActionListener(this);
bEqual.addActionListener(this);
bSqrt.addActionListener(this);
bPow.addActionListener(this);
bNull.addActionListener(this);
p1.setLayout(new GridLayout(4,4,5,5));//网格布局管理器,把容器根据行数和列数分成同样大小的单元,
//每个单元可容纳一个组件,并且此组件会填满网格单元,不能控
//制其占据网格的大小。4、4为网格的行、列数。5、5为组建之间的
//间距
p2.setLayout(new FlowLayout());//用FlowLayout布局管理器将组建默认剧中排放,默认间隙为5个像素
add(t,"North"); //frame的north放置输入框,panel放置在center和south
add(p1,"Center");//将p1添加到Center中
add(p2,"South");//将p2添加到South中
setLocation(400,200);//设计按钮尺寸
setSize(200,200);//设计窗口尺寸
setBackground(new Color(20,200,10));//设置Frame的背景,默认为白色
setVisible(true);//设置Frame设置为可见
addWindowListener(new WindowAdapter(){ //关闭窗口功能
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//实现接口ActionListener
public void actionPerformed(ActionEvent e)
{
Button temp=(Button)e.getSource();
if(e.getActionCommand().equals("number"))
{
if(first)
{
str1=str1+temp.getLabel();
t.setText(str1);//将输入的str1显示在文本框中
}
else
{
str2=str2+temp.getLabel();
t.setText(str2);//将输入的str2显示在文本框中
}
}
else if(e.getActionCommand().equals("oper"))
{
if(str1=="") //如果还没有输入数就点击运算符执行if
{
countOper=0;//若此,则将计数清零
first=true;
}
else
{
countOper++;//计算输入符号的个数
if(countOper1)//若输入的符号个数多余一个,则可以进行计算
{
getResult();
}
operator=temp.getLabel();//存放加减乘除以及开方、平方的符号
first=false;
}
}
else if(e.getActionCommand().equals("开方"))
{
double d=Math.sqrt(Double.parseDouble(str1));
str1=String.valueOf(d);//将计算出来的结果再次传给str1,为连计算准备
t.setText(String.valueOf(d));//将计算出来的结果传至文本框中
first=false;//置为false,即已输入第一个数
}
else if(e.getActionCommand().equals("平方"))
{
double f=Math.pow(Double.parseDouble(str1),2);
str1=String.valueOf(f);
t.setText(String.valueOf(f));
first=false;
}
else if(e.getActionCommand().equals("清除"))
{
str1="";//清空
str2="";
t.setText("");//将文本框清空
countOper=0;//将按键计数器清零
first=true;
error=false;
}
else if(e.getActionCommand().equals("等於"))
{
if((str1=="")||(str2=="")) //两个数没有输全就点击等号,执行if
{
countOper=0;//将按键计数器清零
first=true;
}
else
{
getResult();
countOper=0;
}
}
}
//运算结果的方法
public void getResult()
{
num1=Double.parseDouble(str1);
num2=Double.parseDouble(str2);
if(operator.equals("加"))
{
result=num1+num2;
}
else if(operator.equals("减"))
{
result=num1-num2;
}
else if(operator.equals("乘以"))
{
result=num1*num2;
}
else if(operator.equals("除以"))
{
if(num2==0.0) //除数为0的处理方法
{
error=true;
}
else
{
result=num1/num2;
}
}
if(error)
{
t.setText("error");
}
else
{
t.setText(String.valueOf(result));
str1=String.valueOf(result); //运算后把结果放入str1中,str2清空,为连加连减等操作做准备
str2="";
}
}
//主方法
public static void main(String[] args)
{
new Counter();//创建一个对象"计算器"
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CalculatorPanel extends JPanel
implements ActionListener
{ public CalculatorPanel()
{ setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
add(display, "North");
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
String buttons = "789/456*123-0.=+";
for (int i = 0; i buttons.length(); i++)
addButton(p, buttons.substring(i, i + 1));
add(p, "Center");
}
private void addButton(Container c, String s)
{ JButton b = new JButton(s);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{ String s = evt.getActionCommand();
if ('0' = s.charAt(0) s.charAt(0) = '9'
|| s.equals("."))
{ if (start) display.setText(s);
else display.setText(display.getText() + s);
start = false;
}
else
{ if (start)
{ if (s.equals("-"))
else op = s;
}
else
{ calculate(Double.parseDouble(display.getText()));
op = s;
start = true;
}
}
}
public void calculate(double n)
{ if (op.equals("+")) arg += n;
else if (op.equals("-")) arg -= n;
else if (op.equals("*")) arg *= n;
else if (op.equals("/")) arg /= n;
else if (op.equals("=")) arg = n;
display.setText("" + arg);
}
private JTextField display;
private double arg = 0;
private String op = "=";
private boolean start = true;
}
public class CalculatorApplet extends JApplet
{ public void init()
{ Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
}
}
求java编写的租金计算器小程序
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ZuJin extends JFrame {
/**
* (结束日期-开始日期)÷30×月租金+业务费用+其他费用=总费用
〔(结束日期-开始日期)÷30×月租金+业务费用+其他费用〕÷合租人员=平均费用
需要弹出一个租金计算器对话框 分为租金计算信息与租金计算结果两部分
*/
public ZuJin(){
Container c=getContentPane();
c.setLayout(new GridLayout(5, 4));
JLabel j1=new JLabel("开始日期");
c.add(j1);
JTextField jt1=new JTextField(10);
c.add(jt1);
JLabel j2=new JLabel("结束日期");
c.add(j2);
JTextField jt2=new JTextField("");
c.add(jt2);
JLabel j3=new JLabel("月租金(元)");
c.add(j3);
JTextField jt3=new JTextField(5);
c.add(jt3);
JLabel j4=new JLabel("业务费(元)");
c.add(j4);
JTextField jt4=new JTextField(5);
c.add(jt4);
JLabel j5=new JLabel("其他费用(元)");
c.add(j5);
JTextField jt5=new JTextField(5);
c.add(jt5);
JLabel j6=new JLabel("合租人员数量");
c.add(j6);
JTextField jt6=new JTextField(3);
c.add(jt6);
JLabel j7=new JLabel("总费用(元)");
c.add(j7);
JTextField jt7=new JTextField(5);
jt7.setEditable(false);
c.add(jt7);
JLabel j8=new JLabel("平均费用(元)");
c.add(j8);
JTextField jt8=new JTextField(5);
jt8.setEditable(false);
c.add(jt8);
JButton jb1=new JButton("计算");
c.add(jb1);
jt1.addFocusListener(new FocusAdapter()
{
@Override
public void focusGained(FocusEvent e)
{
if (jt1.getText().equals("格式为:0000-00-00")) {
jt1.setText("");
}
}
@Override
public void focusLost(FocusEvent e)
{
if (jt1.getText().equals("")) {
jt1.setText("格式为:0000-00-00");
}
}
});
jt2.addFocusListener(new FocusAdapter()
{
@Override
public void focusGained(FocusEvent e)
{
if (jt2.getText().equals("格式为:0000-00-00")) {
jt2.setText("");
}
}
@Override
public void focusLost(FocusEvent e)
{
if (jt2.getText().equals("")) {
jt2.setText("格式为:0000-00-00");
}
}
});
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Date d1=Date.valueOf(jt1.getText());//开始日期
Date d2=Date.valueOf(jt2.getText());//结束日期
Calendar c1=Calendar.getInstance();
c1.setTime(d1);
Calendar c2=Calendar.getInstance();
c2.setTime(d2);
int day1=c1.get(Calendar.DAY_OF_YEAR);
int day2=c2.get(Calendar.DAY_OF_YEAR);
int days=day2-day1;
double money1=Double.valueOf(jt3.getText());//月租金
double money2=Double.valueOf(jt4.getText());//业务费
double money3=Double.valueOf(jt5.getText());//其他费用
int man=Integer.valueOf(jt6.getText());//人数
double money4=days/30*money1+money2+money3;
double money5=0.0;
if (man!=0) {
money5=money4/man;
}
else {
money5=money4;
}
jt7.setText(String.valueOf(money4));
jt8.setText(String.valueOf(money5));
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(400,400,500,300);
setVisible(true);
setTitle("租金计算器");
}
public static void main(String[] args) {
ZuJin zj=new ZuJin();
}
}
丑是丑了点 用还是可以用的。
java:编写一个计算器小程序,要求可以做加减乘除运算
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //当前运算的运算符
private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private double resultNum = 0.0; // 计算的中间结果
private boolean operateValidFlag = true; //判断操作是否合法
public Calculator()
{
super("Calculator");
this.setBounds(300, 300, 300, 300);
this.setResizable(false);
this.setBackground(Color.orange);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());//设置布局
text_double=new JTextField("0",20);//设置文本区
text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐
this.getContentPane().add(text_double,BorderLayout.NORTH);//将文本区添加到Content北部
JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局
this.getContentPane().add(panel);//添加panel面板
for(int i=0;ibutton.length;i++)//在面板上添加按钮
panel.add(button[i]);
for(int i=0;ibutton.length;i++)
button[i].addActionListener(this);//为按钮注册
text_double.setEditable(false);//文本框不可编辑
text_double.addActionListener(this);//
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= e.getActionCommand();//返回与此动作相关的命令字符串。
System.out.println("##########command is "+c);
if(c.equals("C")){
handleC(); //用户按了“C”键
}
else if (c.equals("CE")) // 用户按了"CE"键
{
text_double.setText("0");
}
else if ("0123456789.".indexOf(c) = 0) // 用户按了数字键或者小数点键
{
handleNumber(c); // handlezero(zero);
} else //用户按了运算符键
{
handleOperator(c);
}
}
private void handleC() // 初始化计算器的各种值
{
text_double.setText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//输入的第一个数字
{
text_double.setText(button);
} else if ((button.equals(".")) (text_double.getText().indexOf(".") 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1
{
text_double.setText(text_double.getText() + ".");
} else if (!button.equals("."))// 如果输入的不是小数点,则将数字附在结果文本框的后面
{
text_double.setText(text_double.getText() + button);
}
// 以后输入的肯定不是第一个数字了
firstDigit = false;
}
private void handleOperator(String button) {
if (operator.equals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0
if (getNumberFromText() == 0.0){
// 操作不合法
operateValidFlag = false;
text_double.setText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("+")){
// 加法运算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
// 减法运算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
// 乘法运算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
// 平方根运算
if(getNumberFromText()0){
operateValidFlag = false;
text_double.setText("被开方数不能为负数");}
else
resultNum = Math.sqrt(resultNum);
}
else if (operator.equals("+/-")){
// 正数负数运算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
// 赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
text_double.setText(String.valueOf(t1));
} else {
text_double.setText(String.valueOf(resultNum));
}
}
operator = button; //运算符等于用户按的按钮
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() //从结果的文本框获取数字
{
double result = 0;
try {
result = Double.valueOf(text_double.getText()).doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 实例
} catch (NumberFormatException e){
}
return result;
}
public static void main(final String[] args) {
new Calculator();
}
}
求各位大神看看小弟这个java小程序为什么一输入小数就报错..求解
你从文本框中获取后转换成了 整数。整数你输入小数能不报错吗你在事件函数里面讲获取文本的类型转换成float的类型,就可以了,
java小程序计算器的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于微信小程序 计算器、java小程序计算器的信息别忘了在本站进行查找喔。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。