Java | 简易计算器(一个图形化界面的小练手)

导读:本篇文章讲解 Java | 简易计算器(一个图形化界面的小练手),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

题目要求

实现加、减、乘和除运算。输入两个参数后,点击按钮加、减、乘或除,则执行相应的计算,结果显示在计算结果后的文本域中。

代码示例

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test{
   public static void main(String[] args){
	   Calculator c = new Calculator();
	   c.setTitle("简易计算器");
	   c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   c.setVisible(true);
   }
}

class Calculator extends JFrame{
	public Calculator(){
		setSize(300, 200);
		setLayout(new FlowLayout());
		JPanel jp1 = new JPanel();
		JPanel jp2 = new JPanel();
		JPanel jp3 = new JPanel();
		JPanel jp4 = new JPanel();
		
		JLabel jl1 = new JLabel("第一个参数:");
		JTextField jtf1 = new JTextField(10);
		jp1.add(jl1);
		jp1.add(jtf1);
		JLabel jl2 = new JLabel("第二个参数:");
		JTextField jtf2 = new JTextField(10);
		jp2.add(jl2);
		jp2.add(jtf2);
		JLabel jl3 = new JLabel("  计算结果:");
		JTextField jtf3 = new JTextField(10);
		jp3.add(jl3);
		jp3.add(jtf3);
		
		JButton jb1 = new JButton("加");
		jb1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				double p1 = Double.parseDouble(jtf1.getText());
				double p2 = Double.parseDouble(jtf2.getText());
				double p3 = p1+p2;
				String answer = ""+p3;
				jtf3.setText(answer);
			}
		});
		JButton jb2 = new JButton("减");
		jb2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				double p1 = Double.parseDouble(jtf1.getText());
				double p2 = Double.parseDouble(jtf2.getText());
				double p3 = p1-p2;
				String answer = ""+p3;
				jtf3.setText(answer);
			}
		});
		JButton jb3 = new JButton("乘");
		jb3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				double p1 = Double.parseDouble(jtf1.getText());
				double p2 = Double.parseDouble(jtf2.getText());
				double p3 = p1*p2;
				String answer = ""+p3;
				jtf3.setText(answer);
			}
		});
		JButton jb4 = new JButton("除");
		jb4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				double p1 = Double.parseDouble(jtf1.getText());
				double p2 = Double.parseDouble(jtf2.getText());
				if(p2!=0) {
					double p3 = p1/p2;
					String answer = ""+p3;
					jtf3.setText(answer);
				}
				else {
					JOptionPane.showMessageDialog(Calculator.this, "第二个参数为0","报错提示",JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		
		jp4.add(jb1);jp4.add(jb2);jp4.add(jb3);jp4.add(jb4);
		add(jp1);
		add(jp2);add(jp3);add(jp4);
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/103061.html

(1)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!