1. 首页
  2. JAVA

计算机简单页面功能实现

package WINDOW;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Jm extends JFrame implements ActionListener {
	private JPanel panel,panel_1;
	private JTextField view;
	private String opt = "=";
	private double result=0;
	boolean isfirst = true;
	private String[] names= {"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/",};
	private JButton[] button=new JButton[17];
	public Jm() {
		panel_1=new JPanel();
		this.getContentPane().add(panel_1, BorderLayout.NORTH);
		FlowLayout fl_panel = new FlowLayout(FlowLayout.CENTER, 20, 20);
		panel_1.setLayout(fl_panel);
		Font f1 = new Font("黑体", Font.BOLD, 50);
		setSize(1100,900);
		view = new JTextField();
		view.setEditable(false);
		view.setText("0");
		//view.setPreferredSize(new Dimension(300,50));
		view.setFont(f1);
		view.setHorizontalAlignment(JTextField.RIGHT);
		panel_1.add(view);
		view.setColumns(35);
		panel=new JPanel();
		panel.setLayout(new GridLayout(5,4,50,50));
		add(panel,BorderLayout.CENTER);
		for (int i=0;i<button.length-1;i++) {
			button[i]=new JButton(names[i]);
			button[i].setFont(f1);
			if (i>=0&&i<=3) {
				button[i].setBackground(Color.DARK_GRAY);
			}
			button[i].setBorder(BorderFactory.createRaisedBevelBorder());
			button[i].addActionListener(this);
			panel.add(button[i]);
		}
		button[16]=new JButton("C");
		button[16].setFont(f1);
		panel.add(button[16]);
		button[16].addActionListener(this);
		setVisible(true);
	}
	public void actionPerformed(ActionEvent e) {
		String str = e.getActionCommand();
		if("0123456789".indexOf(str)>=0){
			HandNum(str);
		}
		else if(str.equals("C")){
			HandleC();
		}
		else {
			HandCMD(str);
		}
	}
	public void HandleC() {
		view.setText("0");
		isfirst = true;
		opt = "=";
	}
	public void HandNum(String str) {
		if(isfirst) {
			view.setText(str);
		}
		else {
			view.setText(view.getText()+str);
		}
		isfirst = false;
	}
	public void HandCMD(String cmd) {
		if(opt.equals("=")) {
			result = GetNumFromView();
			
		}
		else if(opt.equals("+")) {
			result+=GetNumFromView();
		}
		else if(opt.equals("-")) {
			result-=GetNumFromView();
		}
		else if(opt.equals("*")) {
			result*=GetNumFromView();
		}
		else if(opt.equals("/")) {
			result/=GetNumFromView();
		}
		long t1 = (long)result;
		if((t1 - result)==0) {
			view.setText(String.valueOf(t1));
		}
		else view.setText(String.valueOf(result));
		opt = cmd;
		isfirst = true;
	}
	public double GetNumFromView() {
		return Double.parseDouble(view.getText()); 		
	}
}

 

原创文章,作者:程序员凯小白,如若转载,请注明出处:http://www.kaixiaobai.com/archives/76

发表评论

登录后才能评论

联系我们

qq

在线咨询:点击这里给我发消息

邮件:1298646637@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息