⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shiyangui.java

📁 简单的计算机GUI程序
💻 JAVA
字号:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
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.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
class shiyanGUI extends JPanel{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -687632332975246256L;
	//生成对象
	String str1 = null,str2=null,madd=null;
	double no1=0,no2=0;
	int i=0;
	String[] AccType1= {"编辑","复制","粘贴"};
	String[] AccType2= {"查看","计算型","科学型"};
	String[] AccType3= {"帮助","帮助主题","关于计算器"};
       Dimension dim = new Dimension(57,28);
    //Dimension dim2 = new Dimension(57,28);
    StringBuffer buf = new StringBuffer();
    StringBuffer buf2= new StringBuffer();

	JFrame frame = new JFrame("计算器");
	JPanel com = new JPanel();
	JComboBox comboAccType1 =new JComboBox(AccType1);
	JComboBox comboAccType2 =new JComboBox(AccType2);
	JComboBox comboAccType3 =new JComboBox(AccType3);
	
     JTextField text = new JTextField("0.",33);
    JTextField text2 = new JTextField("M",4);
      //JLabel label = new JLabel("M");
	JButton BB   = new JButton("Backspace");
	JButton BCE  = new JButton("CE");
	JButton BC   = new JButton("C");
	
	JButton  MC  = new JButton("MC");
	JButton  MR  = new JButton("MR");
	JButton  MS  = new JButton("MS");
	JButton Madd = new JButton("M+");

	 JPanel pane = new JPanel();
	JPanel pane2 = new JPanel();
	 JPanel  jp2 = new JPanel();
	 JPanel  jp3 = new JPanel();
	 JPanel  jp4 = new JPanel();
	 JPanel  jp5 = new JPanel();
	 JPanel  jp6 = new JPanel();
	
	JButton Bchu = new JButton("/");
	JButton  Bx  = new JButton("*");
    JButton Bjian= new JButton("-");
    JButton Bjia = new JButton("+");
    JButton Bsqrt= new JButton("sqrt");
	JButton  bai = new JButton("%");
	JButton  ds  = new JButton("1/x");
	JButton  zfu = new JButton("+/-");
	JButton dian = new JButton(".");
	JButton   dy = new JButton("=");
	
	JButton  B0  = new JButton("0");
	JButton  B1  = new JButton("1");
	JButton  B2  = new JButton("2");
	JButton  B3  = new JButton("3");
	JButton  B4  = new JButton("4");
	JButton  B5  = new JButton("5");
	JButton  B6  = new JButton("6");
	JButton  B7  = new JButton("7");
	JButton  B8  = new JButton("8");
	JButton  B9  = new JButton("9");
	//构造方法
	public  shiyanGUI(){
	text2.setVisible(false);
		//为组建字体着色
	text2.setForeground(Color.red);
	   BB.setForeground(Color.red);
	  BCE.setForeground(Color.red);
	   BC.setForeground(Color.red);
	   MC.setForeground(Color.red);
	   MR.setForeground(Color.red);
	   MS.setForeground(Color.red);
	 Madd.setForeground(Color.red);
	 Bchu.setForeground(Color.red);
	   Bx.setForeground(Color.red);
	Bjian.setForeground(Color.red);
	 Bjia.setForeground(Color.red);
	Bsqrt.setForeground(Color.red);
	  bai.setForeground(Color.red);
	   ds.setForeground(Color.red);
	  zfu.setForeground(Color.red);
	   dy.setForeground(Color.red);
	 dian.setForeground(Color.blue);
	   B0.setForeground(Color.blue);	
	   B1.setForeground(Color.blue);
	   B2.setForeground(Color.blue);
	   B3.setForeground(Color.blue);
	   B4.setForeground(Color.blue);
	   B5.setForeground(Color.blue);
	   B6.setForeground(Color.blue);
	   B7.setForeground(Color.blue);
	   B8.setForeground(Color.blue);
	   B9.setForeground(Color.blue);
	   
	  // BB.setPreferredSize(dim);
	 //BCE.setPreferredSize(dim);
	  // BC.setPreferredSize(dim);
	  // MC.setPreferredSize(dim);
	 //  MR.setPreferredSize(dim);
    // text.setPreferredSize(dim);
	
	//设置按钮大小
	 Madd.setPreferredSize(dim);
	 Bchu.setPreferredSize(dim);
	   Bx.setPreferredSize(dim);
	Bjian.setPreferredSize(dim);
	 Bjia.setPreferredSize(dim);
	Bsqrt.setPreferredSize(dim);
	  bai.setPreferredSize(dim);
	   ds.setPreferredSize(dim);
	  zfu.setPreferredSize(dim);
	 dian.setPreferredSize(dim);
	   dy.setPreferredSize(dim);
	   B0.setPreferredSize(dim);
	   B1.setPreferredSize(dim);
	   B2.setPreferredSize(dim);
	   B3.setPreferredSize(dim);
	   B4.setPreferredSize(dim);
	   B5.setPreferredSize(dim);
	   B6.setPreferredSize(dim);
	   B7.setPreferredSize(dim);
	   B8.setPreferredSize(dim);
	   B9.setPreferredSize(dim);
	  BCE.setPreferredSize(new Dimension(100,28));
	   BC.setPreferredSize(new Dimension(100,28));
	   System.out.println(B1.getPreferredSize()+" "+Bsqrt.getPreferredSize());
	//引入事件监听服务程序
	//退格
	BB.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
	     if(buf.length()==0){}
		 else{
		 buf.delete(buf.length()-1,buf.length()); 
		 text.setText(buf.toString());
		 }
	}
	});
	//全清除键
	BC.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
			str1 = null;
			str2 = null;
			 no1 = 0;
			 no2 = 0;
			   i = 0;
			   buf.delete(0,buf.length());
		text.setText("0.");
		System.out.println(str1+"  "+str2+"  "+buf.toString());
		}
	});
	//撤销上一个数
	BCE.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
	        if(str1!=null&&str2!=null){
				str2 = null;
				no2=0;
				buf.delete(0,buf.length());
				text.setText("0.");
			}else if(str1!=null&&str2==null){
				str1 = null;
				no1=0;
				buf.delete(0,buf.length()-1);
				text.setText("0.");
			}
			System.out.println(str1+"  "+str2+"  "+buf.toString());
	}
	});
	/*以下定义事件服务程序
	 *0-9的按钮事件*/
	class NoButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			buf.append(e.getActionCommand());
			text.setText(buf.toString());
			if(i==0){
			no1=Double.valueOf(text.getText()).doubleValue();}
			System.out.println(e.getActionCommand()+"  "+no1);
		}
	}
	//点事件
	class dianButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(buf.indexOf(".")==buf.lastIndexOf(".")&&buf.lastIndexOf(".")==-1){
			buf.append(".");
			text.setText(buf.toString());
			System.out.println(e.getActionCommand());
		}}
	}
	//运算符事件
	class YunSuanFuButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//if(i==0){
			//buf.delete(0,buf.length());
			//}
			String cha = e.getActionCommand();
			if(cha=="/"){
				no1 = Double.valueOf(text.getText()).doubleValue();
				i=1; 
				buf.delete(0,buf.length());
				text.setText(".");
			}else if(cha=="*"){
				no1 = Double.valueOf(text.getText()).doubleValue();
				i=2;
				buf.delete(0,buf.length());
				text.setText(".");
			}else if(cha=="-"){
				no1 = Double.valueOf(text.getText()).doubleValue();
				i=3;
				buf.delete(0,buf.length());
				text.setText(".");
			}else if(cha=="+"){
				no1 = Double.valueOf(text.getText()).doubleValue();
				i=4;
				buf.delete(0,buf.length());
				text.setText(".");
			}
			System.out.println(no1+"  "+no2);
			}					
		}
	//附注运算符事件
	class FuZhuButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String cha = e.getActionCommand();
		
		if(cha.equals("sqrt")){
			double sq  = Double.valueOf(text.getText()).doubleValue();
			text.setText(new Double(Math.sqrt(sq)).toString());
		}else if(cha=="bai"){
			double bai = Double.valueOf(text.getText()).doubleValue();
			text.setText((new Double(bai/100)).toString());
		}else if(cha=="ds"){
			double ds  = Double.valueOf(text.getText()).doubleValue();
			text.setText((new Double(1/ds)).toString());
		}else if(cha=="zfu"){
			double zf  = Double.valueOf(text.getText()).doubleValue();
			buf.delete(0,buf.length());
			buf.append("-");
			buf.append((new Double(zf)).toString());
			text.setText(buf.toString());
			System.out.println("kiss");
			}
		}	
	  }
	//等号事件
	class DyButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
		String cha = e.getActionCommand();
		if(text.getText().length()!=0&&text.getText()!=null&&text.getText()!="")
		{
		 no2 = Double.valueOf(text.getText()).doubleValue();
		 if(i==1){text.setText(new Double(no1/no2).toString());}
		 if(i==2){text.setText(new Double(no1*no2).toString());}
		 if(i==3){text.setText(new Double(no1-no2).toString());}
		 if(i==4){text.setText(new Double(no1+no2).toString());}
		}
		}
	};
	//储存事件
	class KeepButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
		String cha = e.getActionCommand();
		    if(cha== "MC"  ){madd = null;text2.setVisible(true);} 
		    else if(cha== "MR"  ){text.setText(madd);}
		    else if(cha== "MS"  ){}
		    else if(cha== "Madd"){madd = text.getText();text2.setVisible(true);;}
		  		}
	};
	//储存事件服务类
	KeepButtonListener myKeepButtonListener = new KeepButtonListener();
	 MC .addActionListener(myKeepButtonListener);
	 MR .addActionListener(myKeepButtonListener);
	 MS .addActionListener(myKeepButtonListener);
	Madd.addActionListener(myKeepButtonListener);
	//1234567890
	NoButtonListener noButtonListener = new NoButtonListener();
	B0.addActionListener(noButtonListener);
	B1.addActionListener(noButtonListener);	
	B2.addActionListener(noButtonListener);
	B3.addActionListener(noButtonListener);
	B4.addActionListener(noButtonListener);
	B5.addActionListener(noButtonListener);
	B6.addActionListener(noButtonListener);
	B7.addActionListener(noButtonListener);
	B8.addActionListener(noButtonListener);
	B9.addActionListener(noButtonListener);
	//"."
	dianButtonListener mydianButtonListener = new dianButtonListener();
	dian.addActionListener(mydianButtonListener);
	//加减乘除
	YunSuanFuButtonListener myYunSuanFuButtonListener =new YunSuanFuButtonListener();
	 Bchu.addActionListener(myYunSuanFuButtonListener);
	   Bx.addActionListener(myYunSuanFuButtonListener);
	Bjian.addActionListener(myYunSuanFuButtonListener);
	 Bjia.addActionListener(myYunSuanFuButtonListener);
	//一下为辅助运算符sqrt,%,1/x,+/-.
	FuZhuButtonListener myFuZhuButtonListener = new FuZhuButtonListener();
	Bsqrt.addActionListener(myFuZhuButtonListener);
	  bai.addActionListener(myFuZhuButtonListener);
	  ds .addActionListener(myFuZhuButtonListener);
	  zfu.addActionListener(myFuZhuButtonListener);
	//=
	DyButtonListener myDyButtonListener = new DyButtonListener();
	dy.addActionListener(myDyButtonListener);
	//添加容器
	 com.setLayout(new FlowLayout());
		com.add(comboAccType1);
		com.add(comboAccType2);
		com.add(comboAccType3);
	pane.setLayout(new GridLayout(0,1));
		pane.add(com);
		pane.add(text);
	pane2.setLayout(new FlowLayout());
		pane2.setBorder(BorderFactory.createEmptyBorder(2,1,0,0));
		pane2.setSize(80,20);
		pane2.add(text2);
		pane2.add(BB);
		pane2.add(BCE);
		pane2.add(BC);
	jp4.setLayout(new FlowLayout());
		jp4.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
		jp4.setSize(100,20);
		jp4.add(MC   );
		jp4.add(B7   );
		jp4.add(B8   );
		jp4.add(B9   );
		jp4.add(Bchu );
		jp4.add(Bsqrt);
	jp5.setLayout(new FlowLayout());
		jp5.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
		jp5.setSize(100,20);
		jp5.add(MR);
		jp5.add(B4);
		jp5.add(B5);
		jp5.add(B6);
		jp5.add(Bx);
		jp5.add(bai);
	jp6.setLayout(new FlowLayout());
		jp6.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
		jp6.setSize(100,20);
		jp6.add(MS);
		jp6.add(B1);
		jp6.add(B2);
		jp6.add(B3);
		jp6.add(Bjian);
		jp6.add(ds);
	jp3.setLayout(new FlowLayout());
		jp3.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
		jp3.setSize(100,20);
		jp3.add(Madd);
		jp3.add(B0);
		jp3.add(zfu);
		jp3.add(dian);
		jp3.add(Bjia);
		jp3.add(dy);
	frame.getContentPane().setLayout(new GridLayout(0,1));
		frame.setBounds((1024-280)>>1,(768-280)*618/1000,280,280);//定义窗口出现位置
		frame.getContentPane().add(com);
		frame.getContentPane().add(pane);
		frame.getContentPane().add(pane2);
		//frame.getContentPane().add(jp2);
		frame.getContentPane().add(jp4);
		frame.getContentPane().add(jp5);
		frame.getContentPane().add(jp6);
		frame.getContentPane().add(jp3);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗口关闭程序
		frame.pack();
		frame.setVisible(true);
		//frame.setResizable(true);
	}//构造方法结束*/

	
	//主方法
	public static void main(String[] args){
		shiyanGUI shiyan = new shiyanGUI();
		System.out.println("ok");
	}//**
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -