吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1994|回复: 7
收起左侧

[Java 转载] 简单计算器用swing界面设计

  [复制链接]
shenyuan 发表于 2020-7-25 10:37
[Java] 纯文本查看 复制代码
package swing;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Event;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.peer.ButtonPeer;
import java.util.ArrayList;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

/**
* @title
* [url=home.php?mod=space&uid=686208]@AuThor[/url] 
* [url=home.php?mod=space&uid=686237]@date[/url] 2020年6月26日上午10:45:39
*/
@SuppressWarnings({ "serial", "unused" })
public class Calculator extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Calculator frame = new Calculator();
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		
		
	}

	/**
	 * Create the frame.
	 */
	public Calculator() {
		setTitle("计算器");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 220, 240);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		contentPane.setLayout(new BorderLayout(0,0));
		setContentPane(contentPane);
		
		JPanel displayPanel = new JPanel();
		contentPane.add(displayPanel,BorderLayout.NORTH);
		
		JTextField display = new JTextField();//创建文本框对象   上面输出框
		display.setToolTipText("0");
		display.setHorizontalAlignment(SwingConstants.RIGHT);//设置文本框中的文本内容的对齐有式为右对齐
		display.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String a = display.getText();
				int a1 = Integer.valueOf(display.getText());
				int a2 = Integer.valueOf(display.getText());
				int sum = a1 + a2;
				
				String summ = sum+"";
//				display.setText(summ);
//				sum.setText(Integer.parseInt(display.getText())+Integer.parseInt(display.getText())+"");
			}
		});
		
		display.setEditable(false);//设置文本框不可编辑
		display.setFont(new Font("微软雅黑",Font.PLAIN,15));//设置文本框中的字体样式
		displayPanel.add(display);//将文本框添加到displayPanel面板中
		display.setColumns(13);//设置文本框的长度
		
		JPanel buttonPanel = new JPanel();
		contentPane.add(buttonPanel,BorderLayout.CENTER);
		buttonPanel.setLayout(new GridLayout(4,4,5,5));
		
		JButton number7Button = new JButton("7");//数字7
		number7Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number7Button);
		number7Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String seven = number7Button.getText();//获取按钮的text,然后赋值给textfield
				display.setText(seven);//输出框显示按钮数字
				int seven1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});		
		
		JButton number8Button = new JButton("8");//数字8
		number8Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number8Button);
		number8Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String eight = number8Button.getText();//获取按钮的text,然后赋值给textfield
				display.setText(eight);//输出框显示按钮数字
				int eight1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});	
		
		JButton number9Button = new JButton("9");//数字9
		number9Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number9Button);
		number9Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String nine = number9Button.getText();//获取按钮的text,然后赋值给textfield
				display.setText(nine);//输出框显示按钮数字
				int nine1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});		
		
		JButton divideButton = new JButton("/");//除法
		divideButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(divideButton);
		
		JButton number4Button = new JButton("4");//数字4
		number4Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number4Button);
		number4Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String four = number4Button.getText();//获取按钮的text,然后赋值给textfield
				display.setText(four);//输出框显示按钮数字
				int four1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});			
		JButton number5Button = new JButton("5");//数字5
		number5Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number5Button);
		number5Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String fiv = number5Button.getText();//获取按钮的text,然后赋值给textfield
				display.setText(fiv);//输出框显示按钮数字
				int fiv1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});			
		JButton number6Button = new JButton("6");//数字6
		number6Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number6Button);
		number6Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String six = number6Button.getText();//获取按钮的text,然后赋值给textfield
				display.setText(six);//输出框显示按钮数字
				int six1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});			
		
		JButton multiplyButton = new JButton("*");//乘法
		multiplyButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(multiplyButton);
		
		JButton number3Button = new JButton("1");//数字1
		number3Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number3Button);
		number3Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String one = number3Button.getText();
				display.setText(one);//输出框显示按钮数字
				int one1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});
		
		JButton number2Button = new JButton("2");//数字2
		number2Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number2Button);
		number2Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String two = number2Button.getText();//获取按钮的text,然后赋值给textfield
				display.setText(two);//输出框显示按钮数字
				int two1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});
		
		JButton number1Button = new JButton("3");//数字3
		number1Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(number1Button);
		number1Button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String third = number1Button.getText();//获取按钮的text,然后赋值给textfield
				display.setText(third);//输出框显示按钮数字
				int third1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});		
		
		JButton subtractButton = new JButton("-");//减法
		subtractButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(subtractButton);
				
		JButton numberButton = new JButton("0");//数字0
		numberButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(numberButton);
		numberButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String zero = numberButton.getText();//获取按钮的text,然后赋值给textfield
				display.setText(zero);//输出框显示按钮数字
				int zero1 = Integer.valueOf(display.getText());//转换成int类型进行运算
			}
		});	
		
		JButton dotButton = new JButton(".");//小数点
		dotButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(dotButton);
		
		JButton equalButton = new JButton("=");//等于  (结算)
		equalButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(equalButton);
		equalButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String a = display.getText();
				int a1 = Integer.valueOf(display.getText());
				int a2 = Integer.valueOf(display.getText());
				if (display.getText() == "0") {
					
				}
//				int a2 = Integer.valueOf(display.getText());
				int sum = a1 + a2;
				
				String summ = sum+"";
				display.setText(summ);
			}
		});
		
		JButton addButton = new JButton("+");//加法
		addButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		buttonPanel.add(addButton);
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (addButton.equals(e.getSource())) {
					int zero = 0;
					display.setText(zero+"");
					
				}
			}
		});
	}
	
}

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

leading2rome 发表于 2020-7-26 17:03
swing应该过时了吧,远古猛兽
liujieboss 发表于 2020-7-25 22:17
djr1017 发表于 2020-7-26 13:27
 楼主| shenyuan 发表于 2020-7-31 09:54
leading2rome 发表于 2020-7-26 17:03
swing应该过时了吧,远古猛兽

应该是的吧,我还刚刚入门
褚褚先生 发表于 2020-9-28 10:35
woc,我最近也在学习swing,不过都说过时了,我转js了,还是写网页吧。
c03xp 发表于 2020-9-28 11:03
慎用微软雅黑
 楼主| shenyuan 发表于 2020-9-28 22:58
褚褚先生 发表于 2020-9-28 10:35
woc,我最近也在学习swing,不过都说过时了,我转js了,还是写网页吧。

js学完了我准备jsp了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 23:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表