java swing入门 计算器布局

//package adruill;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JButtonTest {
	public static void main(String args[]){
		JFrame main = new JFrame("AD RUI's Calculator");
		main.setIconImage(new ImageIcon("ct.jpg").getImage());
		main.setSize(600, 500);
		main.setLocationByPlatform(true);
		main.add(new CalPanel());
		main.setVisible(true);
		main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}


class CalPanel extends JPanel{
	private String[] tmp = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
	private JButton tt, show;
	private JTextField ans;
	private JPanel r1, r2, r3;
	private boolean ff = false, login = false;
	private String c1 = "", c2 = "";
	private int operID = -1;
	public CalPanel(){/**mainLayout*/
		GridLayout base = new GridLayout(3, 1);
		setLayout(base);
		
		ans = new JTextField ("Please click test to login", JTextField.LEFT);
		ans.setFont(new Font("Consolas", 12, 50));
		ans.setOpaque(true);
		/**part1*/
		r1 = new JPanel();
		r1.setLayout(new BorderLayout());
		r1.add(ans);
		add(r1);
		/**part2*/
		tt = new JButton("CLEAR");
		JButton test = new JButton("test");
		test.setBackground(Color.BLUE);
		test.setFont(new Font("Consolas", 12, 30));
		test.addActionListener(new MyListener());
		
		tt.setFont(new Font("Consolas", 12, 30));
		tt.setBackground(Color.ORANGE);
		tt.setEnabled(false);
		tt.addActionListener(new MyListener());
		r2 = new JPanel();
		r2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 40));
		r2.add(test);
		r2.add(tt);
		add(r2);
			
		r3 = new JPanel();
		r3.setLayout(new GridLayout(4, 4));
		for(int i = 0; i < 16; ++i) addButton(tmp[i], i);
		add(r3);
		//add(tt);
	}
	public void addButton(String txt, int cnt){
		JButton tmp = new JButton(txt);
		tmp.setFont(new Font("Consolas", 12, 30));
		if(cnt == 14) tmp.setBackground(Color.RED);
		tmp.addActionListener(new MyListener());
		r3.add(tmp);
	}
	public int getID(String s){
		
		for(int i = 0; i < 16; ++i) 
			if(tmp[i].equals(s)) return i;
		return -1;
	}
	class MyListener implements ActionListener{

		
		public void actionPerformed(ActionEvent e) {
			boolean f = false;
			if(e.getSource() instanceof JButton) f = true;
			show = (JButton)e.getSource();
			if(show.getText().equals("test")) {
				login = true;
				ans.setText("");
			}
			else
			if(f && login){
				String t = show.getText();
				int id = getID(t);
				if(id == -1){ 
					tt.setEnabled(false);
					ans.setText("");
					init();
				}
				else{
					tt.setEnabled(true);
					if(id == 14){
						cal();
					}else{
						ans.setText(ans.getText() + t);
						if(id % 4 == 3){
							ff = true;
							operID = id;
						}else{
							if(ff) c2 = c2 + t;
							else c1 = c1 + t;
						}
					}
				}
			}else{
				ans.setText("Sign gain!");
			}
			//System.out.println(e.getActionCommand());
		}
		
	}
	public void cal() {
		System.out.println(c1 + " " + tmp[operID] + " " + c2);
		double a = tran(c1), b = tran(c2), res = 0;
		System.out.println(a + " " + tmp[operID] + " " + b);
		switch(operID){
		case 3 : if(abs(b) < 1e-6) {
			ans.setText("Divided By ZERO");
			init();
			return;
		}
		else res = a / b;
		break;
		case 7 : res = a * b;
		break;
		case 11 : res = a - b;
		break;
		case 15 : res = a + b;
		break;
		}
		init();
		//System.out.println(res);
		//c2 = "";
		String s = String.format("%.6f", res);
		init();
		c1 = s;
		ans.setText(s);
	}
	public void init(){
		c1 = "";
		c2 = "";
		operID = -1;
		ff = false;
	}
	public double tran(String s) {
		int f = 0;
		boolean mid = false;
		double l = 0, r = 0;
		if(s.charAt(0) == '-') f = 1;
		int len = s.length();
		for(int i = 0; i < len; ++i){
			char t = s.charAt(i);
			if(t == '.'){
				mid = true; double p = 1;
				for(int j = i + 1; j < len; ++j){
					r = 10 * r + s.charAt(j) - '0';
					p *= 10;
				}
				r /= p;
			}
			if(mid) break;
			l = 10 * l + t - '0';
		}
		double ans = (f == 1) ? -(l + r) : l + r;
		return ans;
	}
	public double abs(double b) {
		return b < 0 ? -b : b;
	}
}	


运行效果:

java swing入门 计算器布局_第1张图片



你可能感兴趣的:(java-学习记录)