Java Swing(计算器界面)

win7系统的计算器,菜单栏还不会下拉列表,暂时用按钮代替。

实现了1-9和.的输入,实现了加减乘除和模除

 按键的前4行和1 2 3 -是通过循环实现的,其余的按键是一个个加进去的

package com.Wang;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
 
public class Test {
 
	static double ans;
	static String symbol;
	
	public static void main(String[] args) {
		JFrame jf = new JFrame("计算器");
		//设置布局
		jf.setLayout(null);
		
		//设置第菜单行
		String[] str0= {"查看(V)","编辑(E)","帮助(H)","",};
		int x0=0;
		int y0=0;
		for (int i = 0; i < str0.length; i++) {
			JButton jb = new JButton();
			jb.setText(str0[i]);
			jb.setFont(new Font(null, Font.PLAIN, 15));
			jb.setBounds(x0,y0,96,25);
			if (x0<300) {
				x0+=96;
			}
			jf.add(jb);
		}
		
		//设置输入框的位置
		JTextField t = new JTextField("0");
		t.setBounds(20, 40, 340, 70);
		jf.add(t);
		
		//设置按键
		int x = 20;
		int y = 120;
		String[] str = {"MC","MR","MS","M+","M-",
				        "<--","CE","C","±","√",
				        "7","8","9","/","%",
				        "4","5","6","*","1/x",};
		for(int i = 0;i<str.length;i++)
		{
			JButton btn = new JButton();
			btn.setText(str[i]);
			btn.setFont(new Font(null, Font.PLAIN, 17));
			btn.setBounds(x, y, 60, 40);
			if(x<280)
			{
				x+=70;
			}
			else
			{
				x = 20;
				y+=50;
			}
			jf.add(btn);
			if (str[i].equals("4")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="4";
							t.setText(s);
						}else {
							s=s+"4";
							t.setText(s);
						}
					}
				});
			}
			if (str[i].equals("5")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="5";
							t.setText(s);
						}else {
							s=s+"5";
							t.setText(s);
						}
							
					}
				});
			}
			if (str[i].equals("6")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="6";
							t.setText(s);
						}else {
							s=s+"6";
							t.setText(s);
						}
					}
				});
			}
			
			if (str[i].equals("7")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="7";
							t.setText(s);
						}else {
							s=s+"7";
							t.setText(s);
						}
					}
				});
			}
			if (str[i].equals("8")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="8";
							t.setText(s);
						}else {
							s=s+"8";
							t.setText(s);
						}
					}
				});
			}
			if (str[i].equals("9")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="9";
							t.setText(s);
						}else {
							s=s+"9";
							t.setText(s);
						}
					}
				});
			}
			
			if (str[i].equals("C")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
							t.setText("0");
					}
				});
			}
			
			if (str[i].equals("/")) {
				btn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						ans=Double.parseDouble(t.getText().toString().trim());
						symbol="/";
						t.setText("");
					}
				});
			}
			
			if (str[i].equals("*")) {
				btn.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						ans=Double.parseDouble(t.getText().toString().trim());
						symbol="*";
						t.setText("");
					}
				});
			}
			
			if (str[i].equals("%")) {
				btn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						ans=Double.parseDouble(t.getText().toString().trim());
						symbol="%";
						t.setText("");
					}
				});
			}
		}
		
		//1 2 3 -
		int x1 = 20;
		int y1 = 320;
		String[] str1 = {"1","2","3","-",};
		for (int i = 0; i < str1.length; i++) {
			JButton jb1 = new JButton();
			jb1.setText(str1[i]);
			jb1.setFont(new Font(null, Font.PLAIN, 17));
			jb1.setBounds(x1, y1, 60, 40);
			if(x1<280)
			{
				x1+=70;
			}
			else
			{
				x1 = 20;
				y1+=50;
			}
			jf.add(jb1);
			if (str1[i].equals("1")) {
				jb1.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="1";
							t.setText(s);
						}else {
							s=s+"1";
							t.setText(s);
						}
					}
				});
			}
			if (str1[i].equals("2")) {
				jb1.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="2";
							t.setText(s);
						}else {
							s=s+"2";
							t.setText(s);
						}
					}
				});
			}
			if (str1[i].equals("3")) {
				jb1.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="3";
							t.setText(s);
						}else {
							s=s+"3";
							t.setText(s);
						}
					}
				});
			}
			
			if (str1[i].equals("-")) {
				jb1.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						ans=Double.parseDouble(t.getText().toString().trim());
						symbol="-";
						t.setText("");
					}
				});
			}
			
		}
		//=
		
		JButton jb2 = new JButton();
		jb2.setText("=");
		jb2.setFont(new Font(null, Font.PLAIN, 17));
		jb2.setBounds(300, 320, 60, 90);
		jf.add(jb2);
		
		jb2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				double res=0;
				res=Double.parseDouble(t.getText().toString().trim());
				if (symbol.equals("+")) {
					t.setText(Double.toString(ans+res));
				}
				if (symbol.equals("-")) {
					t.setText(Double.toString(ans-res));
				}
				if (symbol.equals("*")) {
					t.setText(Double.toString(ans*res));
				}
				if (symbol.equals("/")) {
					t.setText(Double.toString(ans/res));
				}
				if (symbol.equals("%")) {
					t.setText(Double.toString(ans%res));
				}
			}
		});
		
		
		//0
		JButton jb3 = new JButton();
		jb3.setText("0");
		jb3.setFont(new Font(null, Font.PLAIN, 17));
		jb3.setBounds(20, 370, 130, 40);
		jf.add(jb3);
		jb3.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					String s=t.getText().toString().trim();
					if (!(s.equals("0"))) {
						s=s+"0";
						t.setText(s);
					}
				}
			});
		
		//.
		JButton jb4 = new JButton();
		jb4.setText(".");
		jb4.setFont(new Font(null, Font.PLAIN, 17));
		jb4.setBounds(160, 370, 60, 40);
		jf.add(jb4);
		jb4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String s=t.getText().toString().trim();
				if (!s.contains(".")) {
					s=s+".";
					t.setText(s);
				}
			}
		});
		
		//+
		JButton jb5 = new JButton();
		jb5.setText("+");
		jb5.setFont(new Font(null, Font.PLAIN, 17));
		jb5.setBounds(230, 370, 60, 40);
		jf.add(jb5);
		
		jb5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				ans=Double.parseDouble(t.getText().toString().trim());
				symbol="+";
				t.setText("");
			}
		});
		
		
		//设置窗口大小
		jf.setBounds(500,200,400,480);
		//可视化
		jf.setVisible(true);
		//响应关闭
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
}

附加一个计时器

 

package com.bai;

import java.awt.Font;
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.JTextField;
import javax.swing.WindowConstants;
 
public class Test {
 
	static double ans;
	static String symbol;
	static boolean flag=true;
	
	public static void main(String[] args) {
		JFrame jf = new JFrame("计算器");
		//设置布局
		jf.setLayout(null);
		
		//设置第菜单行
		String[] str0= {"查看(V)","编辑(E)","帮助(H)","",};
		int x0=0;
		int y0=0;
		for (int i = 0; i < str0.length; i++) {
			JButton jb = new JButton();
			jb.setText(str0[i]);
			jb.setFont(new Font(null, Font.PLAIN, 15));
			jb.setBounds(x0,y0,96,25);
			if (x0<300) {
				x0+=96;
			}
			jf.add(jb);
		}
		
		//设置输入框的位置
		JTextField t = new JTextField("0");
		t.setBounds(20, 40, 340, 70);
		jf.add(t);
		
		//设置按键
		int x = 20;
		int y = 120;
		String[] str = {"MC","MR","MS","M+","M-",
				        "<--","CE","C","±","√",
				        "7","8","9","/","%",
				        "4","5","6","*","1/x",};
		for(int i = 0;i<str.length;i++)
		{
			JButton btn = new JButton();
			btn.setText(str[i]);
			btn.setFont(new Font(null, Font.PLAIN, 17));
			btn.setBounds(x, y, 60, 40);
			if(x<280)
			{
				x+=70;
			}
			else
			{
				x = 20;
				y+=50;
			}
			jf.add(btn);
			if (str[i].equals("4")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="4";
							t.setText(s);
						}else {
							s=s+"4";
							t.setText(s);
						}
					}
				});
			}
			if (str[i].equals("5")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="5";
							t.setText(s);
						}else {
							s=s+"5";
							t.setText(s);
						}
							
					}
				});
			}
			if (str[i].equals("6")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="6";
							t.setText(s);
						}else {
							s=s+"6";
							t.setText(s);
						}
					}
				});
			}
			
			if (str[i].equals("7")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="7";
							t.setText(s);
						}else {
							s=s+"7";
							t.setText(s);
						}
					}
				});
			}
			if (str[i].equals("8")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="8";
							t.setText(s);
						}else {
							s=s+"8";
							t.setText(s);
						}
					}
				});
			}
			if (str[i].equals("9")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="9";
							t.setText(s);
						}else {
							s=s+"9";
							t.setText(s);
						}
					}
				});
			}
			
			if (str[i].equals("C")) {
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
							t.setText("0");
					}
				});
			}
			
			if (str[i].equals("/")) {
				btn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						ans=Double.parseDouble(t.getText().toString().trim());
						symbol="/";
						t.setText("");
					}
				});
			}
			
			if (str[i].equals("*")) {
				btn.addActionListener(new ActionListener() {
 
					@Override
					public void actionPerformed(ActionEvent e) {
						ans=Double.parseDouble(t.getText().toString().trim());
						symbol="*";
						t.setText("");
					}
				});
			}
			
			if (str[i].equals("%")) {
				btn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						ans=Double.parseDouble(t.getText().toString().trim());
						symbol="%";
						t.setText("");
					}
				});
			}
		}
		
		//1 2 3 -
		int x1 = 20;
		int y1 = 320;
		String[] str1 = {"1","2","3","-",};
		for (int i = 0; i < str1.length; i++) {
			JButton jb1 = new JButton();
			jb1.setText(str1[i]);
			jb1.setFont(new Font(null, Font.PLAIN, 17));
			jb1.setBounds(x1, y1, 60, 40);
			if(x1<280)
			{
				x1+=70;
			}
			else
			{
				x1 = 20;
				y1+=50;
			}
			jf.add(jb1);
			if (str1[i].equals("1")) {
				jb1.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="1";
							t.setText(s);
						}else {
							s=s+"1";
							t.setText(s);
						}
					}
				});
			}
			if (str1[i].equals("2")) {
				jb1.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="2";
							t.setText(s);
						}else {
							s=s+"2";
							t.setText(s);
						}
					}
				});
			}
			if (str1[i].equals("3")) {
				jb1.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						String s=t.getText().toString().trim();
						if (s.equals("0")) {
							s="3";
							t.setText(s);
						}else {
							s=s+"3";
							t.setText(s);
						}
					}
				});
			}
			
			if (str1[i].equals("-")) {
				jb1.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						ans=Double.parseDouble(t.getText().toString().trim());
						symbol="-";
						t.setText("");
					}
				});
			}
			
		}
		//=
		
		JButton jb2 = new JButton();
		jb2.setText("=");
		jb2.setFont(new Font(null, Font.PLAIN, 17));
		jb2.setBounds(300, 320, 60, 90);
		jf.add(jb2);
		
		jb2.addActionListener(new ActionListener() {
 
			@Override
			public void actionPerformed(ActionEvent e) {
				double res=0;
				res=Double.parseDouble(t.getText().toString().trim());
				if (symbol.equals("+")) {
					t.setText(Double.toString(ans+res));
				}
				if (symbol.equals("-")) {
					t.setText(Double.toString(ans-res));
				}
				if (symbol.equals("*")) {
					t.setText(Double.toString(ans*res));
				}
				if (symbol.equals("/")) {
					t.setText(Double.toString(ans/res));
				}
				if (symbol.equals("%")) {
					t.setText(Double.toString(ans%res));
				}
			}
		});
		
		
		//0
		JButton jb3 = new JButton();
		jb3.setText("0");
		jb3.setFont(new Font(null, Font.PLAIN, 17));
		jb3.setBounds(20, 370, 130, 40);
		jf.add(jb3);
		jb3.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					String s=t.getText().toString().trim();
					if (!(s.equals("0"))) {
						s=s+"0";
						t.setText(s);
					}
				}
			});
		
		//.
		JButton jb4 = new JButton();
		jb4.setText(".");
		jb4.setFont(new Font(null, Font.PLAIN, 17));
		jb4.setBounds(160, 370, 60, 40);
		jf.add(jb4);
		jb4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String s=t.getText().toString().trim();
				if (!s.contains(".")) {
					s=s+".";
					t.setText(s);
				}
			}
		});
		
		//+
		JButton jb5 = new JButton();
		jb5.setText("+");
		jb5.setFont(new Font(null, Font.PLAIN, 17));
		jb5.setBounds(230, 370, 60, 40);
		jf.add(jb5);
		
		jb5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				ans=Double.parseDouble(t.getText().toString().trim());
				symbol="+";
				t.setText("");
			}
		});
		
		//秒
		JLabel jl1 = new JLabel();
		jl1.setBounds(600, 100, 100, 100);
		jl1.setFont(new Font(null, Font.PLAIN, 17));
		jl1.setText("秒");
		jf.add(jl1);
		
		//分
		JLabel jl2 = new JLabel();
		jl2.setBounds(510, 100, 100, 100);
		jl2.setFont(new Font(null, Font.PLAIN, 17));
		jl2.setText("分");
		jf.add(jl2);

		//时
		JLabel jl3 = new JLabel();
		jl3.setBounds(420, 100, 100, 100);
		jl3.setFont(new Font(null, Font.PLAIN, 17));
		jl3.setText("时");
		jf.add(jl3);
		
		
		
		//计时器,文本框
		//时
		JTextField jsq = new JTextField("0");
		jsq.setBounds(580, 170, 70, 50);
		jf.add(jsq);
		
		
		//分
		JTextField jsq1 = new JTextField("0");
		jsq1.setBounds(490, 170, 70, 50);
		jf.add(jsq1);

		
		//秒
		JTextField jsq2 = new JTextField("0");
		jsq2.setBounds(400, 170, 70, 50);
		jf.add(jsq2);
		
		
		
		//秒  线程
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				int second=0;
				while(flag){
					jsq.setText(""+second++);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (second==60) {
						second=0;
					}
					
				}
			}
		});
		
		
		//分钟  线程
		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				int count=0;
				while(flag){
					jsq1.setText(""+count++);
					try {
						Thread.sleep(60000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (count==60) {
						count=0;
					}
				}
			}
		});
		
		
		//小时  线程
		Thread thread2 = new Thread(new Runnable() {

			@Override
			public void run() {
				int count=0;
				while(flag){
					jsq2.setText(""+count++);
					try {
						Thread.sleep(3600000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (count==60) {
						count=0;
					}
				}
			}
		});
		
		
		//开始按钮
		JButton jbs = new JButton("开始");
		jbs.setBounds(440, 240, 70, 50);
		jbs.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				thread.start();
				thread1.start();
				thread2.start();
			}
		});
		jf.add(jbs);
		
		
		//结束按钮
		JButton jbs1 = new JButton("结束");
		jbs1.setBounds(540, 240, 70, 50);
		jbs1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				flag=false;
			}
		});
		jf.add(jbs1);
		
		
		//设置窗口大小
		jf.setBounds(500,200,700,480);
		//可视化
		jf.setVisible(true);
		//响应关闭
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

1while(true){learn}

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

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

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

打赏作者

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

抵扣说明:

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

余额充值