美文网首页
JAVA-15 用Java 在intelliJ制作一个加减计算器

JAVA-15 用Java 在intelliJ制作一个加减计算器

作者: RashidinAbdu | 来源:发表于2021-08-24 15:20 被阅读0次
  • 总的步骤:

1)intelliJ 的安装,JDK的设置:下载community版本并安装(直接用下一步来完成安装;)

https://www.jetbrains.com/idea/download/#section=windows

  • JDK 的下载与安装,path的设置参考:
https://www.javatpoint.com/how-to-set-path-in-java#:~:text=The%20path%20is%20required%20to%20be%20set%20for,is%20necessary%20to%20set%20the%20path%20of%20JDK.


2) 代码的完成如下所示:

  • 总体代码,在intelliJ先运行没问题后,再进行打包:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class R1 implements ActionListener{

    JFrame f;
    JButton b,badd,bsub,bclear;
    Container cp;
    JLabel jl;


    JTextField jtf1,jtf2,jtf3;
    R1(){
        f = new JFrame("Rashidin's Calculator In Java-Produced By Rashidin Abdugheni ");
        f.setSize(600,200);//设置大小

        cp = f.getContentPane();//加载面板
        cp.setLayout(new FlowLayout());//改成流式布局


        jtf1 = new JTextField(10);
        jtf2 = new JTextField(10);
        jtf3 = new JTextField(10);


        badd = new JButton("+  Plus");
        bsub = new JButton("-  Minus");
        bclear = new JButton("Clean");

        cp.add(jtf1);//添加文本框
        cp.add(jtf2);
        cp.add(jtf3);

        cp.add(badd);
        badd.addActionListener(this);//给+添加监听机制
        cp.add(bsub);
        bsub.addActionListener(this);
        cp.add(bclear);
        bclear.addActionListener(this);


        f.setVisible(true);//显示窗体
        f.setLocationRelativeTo(null);//窗体居中
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭

    }
    public static void main(String[] args){
        new R1();
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        int num1 = Integer.parseInt(jtf1.getText());//获取文本框1输入的值将其转换成整型
        int num2 = Integer.parseInt(jtf2.getText());//获取文本框2输入的值将其转换成整型

        if(e.getSource()==badd){
            jtf3.setText(String.valueOf(num1+num2));//将文本框1和2的值相加并转成字符串型显示到文本框3中
        }
        else if(e.getSource()==bsub){
            jtf3.setText(String.valueOf(num1-num2));
        }
        else{
            jtf1.setText("");//让文本框为空
            jtf2.setText("");
            jtf3.setText("");
        }
    }
}

输出结果如下:

image.png
3)打包成Jar文件:

https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html#write-code


  • 总体代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class R1 implements ActionListener{

    JFrame f;
    JButton b,badd,bsub,bclear;
    Container cp;
    JLabel jl;


    JTextField jtf1,jtf2,jtf3;
    R1(){
        f = new JFrame("Rashidin's Calculator In Java-Produced By Rashidin Abdugheni ");
        f.setSize(600,200);//设置大小

        cp = f.getContentPane();//加载面板
        cp.setLayout(new FlowLayout());//改成流式布局


        jtf1 = new JTextField(10);
        jtf2 = new JTextField(10);
        jtf3 = new JTextField(10);


        badd = new JButton("+  Plus");
        bsub = new JButton("-  Minus");
        bclear = new JButton("Clean");

        cp.add(jtf1);//添加文本框
        cp.add(jtf2);
        cp.add(jtf3);

        cp.add(badd);
        badd.addActionListener(this);//给+添加监听机制
        cp.add(bsub);
        bsub.addActionListener(this);
        cp.add(bclear);
        bclear.addActionListener(this);


        f.setVisible(true);//显示窗体
        f.setLocationRelativeTo(null);//窗体居中
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭

    }
    public static void main(String[] args){
        new R1();
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        int num1 = Integer.parseInt(jtf1.getText());//获取文本框1输入的值将其转换成整型
        int num2 = Integer.parseInt(jtf2.getText());//获取文本框2输入的值将其转换成整型

        if(e.getSource()==badd){
            jtf3.setText(String.valueOf(num1+num2));//将文本框1和2的值相加并转成字符串型显示到文本框3中
        }
        else if(e.getSource()==bsub){
            jtf3.setText(String.valueOf(num1-num2));
        }
        else{
            jtf1.setText("");//让文本框为空
            jtf2.setText("");
            jtf3.setText("");
        }
    }
}
  • 打包后生产的jar文件为:
image.png
  • 双击即可运行:
image.png

相关文章

网友评论

      本文标题:JAVA-15 用Java 在intelliJ制作一个加减计算器

      本文链接:https://www.haomeiwen.com/subject/cxmdiltx.html