利用java的swing控件实现四则混合运算计算器

其中ExperDemo2这个类在之前的文章里面写过,就不再粘贴一遍了

package expr;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Calculate extends JFrame {			
	static String strr;
	JButton but[]=new JButton[10];				
	JButton but1[]=new JButton[7];				
	private JPanel f;							
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					 Calculate frame= new Calculate();
					frame.setVisible(true);		
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Calculate() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		setBounds(100, 100, 737, 462);				
		f = new JPanel();							
		f.setBorder(new EmptyBorder(5, 5, 5, 5));	
		setContentPane(f);
		f.setLayout(null);
		JTextArea ta = new JTextArea();				
		ta.setBounds(14, 13, 691, 204);				
		
		JButton b=new JButton(new String("c"));		
		b.setBounds(160, 370, 45,50 );				
		f.add(b);									
	    f.add(ta);     							
	    b.addActionListener(new ActionListener() {			
				public void actionPerformed(ActionEvent e) {
					ta.setText("");}});
		  	JLabel label;       
		  label = new JLabel("计算一次后点击“C”清除后进行下一次运算");
		  label.setBounds(220,370, 400, 50);
		  f.add(label);
		  String str[]={"=","-","*","/","+","(",")"};
		for(int i=0;i=0 && i<5){
			 but1[i].setBounds(605-100*i, 220, 100, 150);		
			 but1[i].setFont(new Font("宋体", Font.PLAIN, 25));}	
			 if(i==5)
			 but1[5].setBounds(60, 370, 50, 50);				
			 if(i==6)
			 but1[6].setBounds(110, 370, 50, 50);
			 f.add(but1[i]);
			 but1[i].addActionListener(new ActionListener() {		
					public void actionPerformed(ActionEvent e) {
							ta.append(((JButton)e.getSource()).getText());
						}});
		}
			for (int i = 0; i < 10; i++) {
			but[i]=new JButton(new String (i+""));			
			but[i].setForeground(Color.blue);
			if(i>=0 && i<3)
			but[i].setBounds(10+50*i, 220, 50, 50);
			if(i>=3 && i<=5)
				but[i].setBounds(10+50*(i-3), 270, 50, 50);
			if(i>=6 && i<9)
				but[i].setBounds(10+50*(i-6), 320, 50, 50);
			if(i==9)
				but[i].setBounds(10, 370, 50, 50);
			f.add(but[i]);
			but[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ta.append(((JButton)e.getSource()).getText());}});	
		}
			ta.setFont(new Font("宋体", Font.PLAIN, 25));
			but1[0].addMouseListener(new MouseAdapter() {	
				public void mouseReleased(MouseEvent e) {	
				String str1=ta.getText();
				String str2=str1.substring(0, str1.length()-1);				
				ExprDemo2.s.add('#');
				String str3=ExprDemo2.transformz(str2);
				double a=ExprDemo2.Calcu(str3);
				ta.append(a+"\n");
			}});	
			f.setVisible(true);
	}	
}

利用java的swing控件实现四则混合运算计算器_第1张图片

你可能感兴趣的:(复习笔记)