java GUI实现简易计算器

在期末实训写的一个计算器,希望可以帮到大家,如果有错误的地方,希望大家多多指点1c7f1735d1154fae8273587964f9bf9a.jpg

package 计算器;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {
private JPanel centerPanel=new JPanel();//中间面板
private String x="";//记录输入的数
private String y="";//记录输入的数
private String fh="";//记录输入的符号
private double answer;//记录答案
private JTextField tfDouble;// 显示表达式
private JTextField tfAnswer;// 显示答案

public Calculator(){
this.setBackground(Color. lightGray);// 设置背景颜色

tfDouble= new JTextField();// 初始化数字
tfDouble.setHorizontalAlignment(JTextField.LEFT);// 左对齐
tfDouble. setText ( "0.") ; //初始化答案
tfDouble.setBounds(10,20,370,50);// 设置边界
add(tfDouble, BorderLayout.NORTH);//添加文本框

tfAnswer = new JTextField();// 初始化答案
tfAnswer=new JTextField();//显示答案
tfAnswer. setHorizontalAlignment(JTextField. RIGHT);//右对齐
tfAnswer.setBounds(10,70,370,50);// 设置边界
add(tfAnswer,BorderLayout. CENTER);//添加答案

centerPanel. setLayout(new GridLayout(5,5));//创建中间面板
addButton("7", Color. blue);
addButton("8", Color. blue);
addButton("9", Color. blue);
addButton("/", Color. red);
addButton("CE", Color. green);
addButton("4", Color. blue);
addButton("5", Color. blue);
addButton("6", Color. blue);
addButton( "x", Color. red);
addButton("^", Color. blue);
addButton("1", Color. blue);
addButton("2", Color. blue);
addButton("3", Color. blue);
addButton("-", Color. red);
addButton("sqrt", Color. blue);
addButton("0", Color. blue);
addButton("+/-", Color. blue);
addButton( ".", Color. blue);
addButton("+", Color. red);
addButton("=", Color. red);
addButton("sin", Color. red);
addButton("cos", Color. red);
addButton("tan", Color.red);
addButton("ln", Color. red);
addButton("⬅", Color.red);
centerPanel.setBounds(10,120,370,250);//设置中间面板的位置
add(centerPanel, BorderLayout.SOUTH);// 添加中间面板
setLayout(new BorderLayout());// 添加布局
}
public void addButton(String name, Color color) {
JButton bt = new JButton(name);//
bt.setBackground(Color.pink);//设置按钮的背景色
bt.setForeground(color);//设置按钮的前景色
bt.addActionListener(this);//添加事件监听
centerPanel.add(bt);// 添加按钮
}
//计算功能实现
public void dengyu(String z){
if ( z. equals("+"))
answer=Double. parseDouble(x)+Double. parseDouble(y);
if ( z. equals("-"))
answer=Double. parseDouble(x)-Double. parseDouble(y);
if ( z. equals("x"))
answer=Double. parseDouble(x)*Double. parseDouble(y);
if ( z. equals("/"))
answer=Double. parseDouble(x)/Double. parseDouble(y);
if ( z. equals("^"))
answer=Math. pow(Double. parseDouble(x), Double. parseDouble(y));
if (z. equals("sin"))
answer=Math. sin(Double. parseDouble(x));
if ( z. equals("cos"))
answer=Math. cos(Double. parseDouble(x));
if (z. equals("tan"))
answer=Math. tan(Double. parseDouble(x));
if ( z. equals("ln"))
answer=Math. log(Double. parseDouble(x));
x=Double. toString(answer);//记录答案
tfAnswer. setText(x);//显示答案
y="" ;//清空
fh="";//清空
}

public void actionPerformed(ActionEvent e) throws IndexOutOfBoundsException {
//按钮的事件处理
if (e. getActionCommand(). equals("0")
|| e. getActionCommand( ) . equals ("1")
|| e. getActionCommand( ) . equals ("2")
|| e. getActionCommand( ) . equals ("3")
|| e. getActionCommand( ) . equals ("4")
|| e. getActionCommand( ) . equals ("5")
|| e. getActionCommand( ) . equals ("6")
|| e. getActionCommand( ) . equals ("7")
|| e. getActionCommand( ) . equals ("8")
|| e. getActionCommand( ) . equals ("9")){
if (fh. equals("")) /*清空*/{
x=x+e. getActionCommand();
if ( x . startsWith ( "00") )/*去掉第一个0*/ {
x=x. substring(1);
}
tfDouble. setText(x);
}else {
y=y+e. getActionCommand();
if ( y. startsWith ( " 00" ) ) {
y=y. substring(1);
}
tfDouble. setText(x+fh+y);
}
}
if (e. getActionCommand( ) . equals( ".")) {//小数
if (fh. equals("")) {
int i=0,j=0;
for ( i=0;i<x. length( );i++)
if ( x. charAt ( i ) == '.' )//判断是否为小数
j++;
if ( j==0)
x=x+ " ." ;
tfDouble. setText(x);
}else {
int i=0,j=0;
for ( i=0;i<y. length ( );i++)
if ( y. charAt ( i ) == '.' )
j++ ;
if ( j==0)
y=y+ " ." ;
tfDouble. setText(x+fh+y);
}
}
if (e.getActionCommand().equals("⬅")) {
if(fh.equals("")){
x = x.substring(0, x.length() - 1);// 截取前一个数
tfDouble.setText(x);
}
if(y.equals("")){
fh=fh.substring(0, fh.length() -1);// 截取前一个数
tfDouble.setText(x);
}
else {
y=y.substring(0, y.length() -1);// 截取前一个数
tfDouble.setText(x+fh+y);
}
}
if (e. getActionCommand(). equals("CE")){//清空
x="";
y="" ;
fh="";
tfDouble.setText("0.");
tfAnswer. setText("");//
}
if (e. getActionCommand(). equals("+/-")){//正负
if ( fh. equals( "")) {
if (x. substring(0,1). equals("-"))
x=x. substring(1);
else
x="- "+ x;
tfDouble. setText(x);
}else {
if (y. substring(0,1). equals("-"))
y=y. substring(1);
else
y="-"+y;
tfDouble. setText(x+fh+y);
}
}
if (e. getActionCommand(). equals("sqrt")){//平方根
if ( fh !="")
dengyu(fh);
answer=Math. sqrt(Double. parseDouble(x));
x=Double. toString(answer);
tfAnswer. setText(x);
}
if ( e. getActionCommand( ) . equals( "+")) {//加
if ( fh !="")
dengyu(fh);
fh="+";
tfDouble.setText(x+fh);
}
if (e. getActionCommand( ) . equals( "-")) {//减
if ( fh !="")
dengyu(fh);
fh="-";
tfDouble.setText(x+fh);
}
if ( e. getActionCommand( ) . equals ( "x") ) {//乘
if ( fh !="")
dengyu(fh);
fh="x";
tfDouble.setText(x+fh);
}
if (e. getActionCommand() . equals( "/")) {//除
if ( fh !="")
dengyu(fh);
fh="/";
tfDouble.setText(x+fh);
}
if ( e. getActionCommand( ) . equals( "^")) {//幂
if ( fh !="")
dengyu(fh);
fh="^";
}
if (e. getActionCommand(). equals("sin")){//正弦
if ( fh !="")
dengyu(fh);
answer=Math. sin(Double. parseDouble(x));
x=Double. toString (answer);
tfAnswer. setText(x);
}
if (e. getActionCommand(). equals("cos")){//余弦
if ( fh !="")
dengyu(fh);
answer=Math. cos(Double. parseDouble(x));
x=Double. toString(answer);
tfAnswer. setText(x);
}
if (e. getActionCommand(). equals("tan")){//正切
if ( fh !="")
dengyu(fh);
answer=Math. tan(Double. parseDouble(x));
x=Double.toString(answer);
tfAnswer. setText(x);
}
if (e. getActionCommand(). equals("ln")){//对数
if ( fh !="")
dengyu(fh);
answer=Math. log(Double. parseDouble(x));
x=Double. toString(answer);
tfAnswer. setText(x);
}
if ( e. getActionCommand( ). equals( "=")){// 等于
if(fh !="")
dengyu(fh);
tfAnswer.setText(x);
}
}
public static void main(String args[]){
Calculator c=new Calculator();//实力化对象
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
c. setSize( 400,400) ;//设置窗口大小
c. setVisible(true);
}
}

效果图如下:e86e3ce3f2fd4756b4af9ed0348df869.png

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Java中进行数据库操作可以使用JDBC(Java Database Connectivity)技术,而JUI(Java User Interface)是Java用户界面的缩写,两者并没有直接关系。如果您想了解如何使用Java进行数据库操作,可以参考以下步骤: 1. 导入JDBC驱动程序:在Java程序中使用JDBC连接数据库,需要先导入相应的JDBC驱动程序。不同的数据库有不同的驱动程序,需要根据使用的数据库类型选择相应的驱动程序。例如,使用MySQL数据库可以使用以下代码导入MySQL的JDBC驱动程序: ```java Class.forName("com.mysql.jdbc.Driver"); ``` 2. 建立数据库连接:使用JDBC连接数据库需要提供数据库的连接信息,包括数据库的URL、用户名和密码等。例如,使用MySQL数据库可以使用以下代码建立数据库连接: ```java String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, user, password); ``` 3. 创建Statement对象:在Java程序中使用JDBC执行SQL语句需要创建Statement对象。例如,使用MySQL数据库可以使用以下代码创建Statement对象: ```java Statement stmt = conn.createStatement(); ``` 4. 执行SQL语句:使用Statement对象执行SQL语句。例如,查询表中的数据可以使用以下代码: ```java String sql = "SELECT * FROM user"; ResultSet rs = stmt.executeQuery(sql); ``` 5. 处理查询结果:使用ResultSet对象处理查询结果。例如,遍历查询结果可以使用以下代码: ```java while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println("id: " + id + ", name: " + name + ", age: " + age); } ``` 6. 关闭数据库连接:在Java程序中使用JDBC连接数据库后,需要关闭数据库连接。例如,使用以下代码关闭MySQL数据库连接: ```java rs.close(); stmt.close(); conn.close(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会有风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值