JAVA实现计算器GUI

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Calculate extends JFrame implements ActionListener {
	private JLabel leftLabel,rightLabel;
	String input = "";//输入的 式子 
	JTextField textField=new JTextField();//创建显示器
	public Calculate(JLabel leftLabel, JLabel rightLabel) throws HeadlessException {
		super();
		this.leftLabel=leftLabel;
		this.rightLabel=rightLabel;
		
	}

	public Calculate() throws HeadlessException {
		super();
		// TODO Auto-generated constructor stub
	}

	public Calculate(GraphicsConfiguration arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}

	public Calculate(String arg0, GraphicsConfiguration arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	public Calculate(String arg0) throws HeadlessException {
		super(arg0);		
		this.setResizable(false);//设置窗体大小不可改变
		this.setBounds(600,200,230,230);
		Font f=new Font("楷体",Font.ITALIC,25);
		this.setFont(f);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口即为退出
		JPanel viewPanel=new JPanel();//设置显示器面板,采用默认的流布局//设置显示器面板,采用默认的流布局
		this.add(viewPanel,BorderLayout.NORTH);//将显示器面板添加到窗体顶部
		
		textField.setEditable(false);//设置显示器不可编辑
		textField.setHorizontalAlignment(SwingConstants.RIGHT);
		textField.setColumns(18);
		viewPanel.add(textField);//将显示器添加到显示器面板中
		JPanel buttonPanel=new JPanel();//创建按钮面板//创建按钮面板
		GridLayout gridLayout=new GridLayout(4,0);//网格布局
		gridLayout.setHgap(10);
		gridLayout.setVgap(10);
		
		buttonPanel.setLayout(gridLayout);//按钮面板采用网格布局
		this.add(buttonPanel,BorderLayout.CENTER);//将按钮面板添加到窗体中间
		String[][] names= {
				{"1","2","3","+"},{"4","5","6","-"},{"7","8","9","x"},{".","0","=","/"}
		};
		JButton[][] buttons=new JButton[4][4];
		for(int row=0;row s = new Stack(); 		
	double m = Double.parseDouble(str[0]); 	
	s.push(m); 		
	for(int i=1;i

上面是功能类

下面的是主函数

(应用面向对象的方法)//刚学的GUI所以有的地方没有考虑周全 

public class TestCalculate {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Calculate c=new Calculate("计算器");
c.setVisible(true);   
	}

}


 

你可能感兴趣的:(JAVA实现计算器GUI)