简单计算器-界面实现

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class BaseFrame extends JFrame{

	public BaseFrame (String title)
	{
		setLayout(null);
		setTitle(title);
		setSize(800,800);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			System.exit(0);
			}
		});//关闭窗口的匿名类
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
	}
	public void showMe(){
		setVisible(true);
	}
	

	

}


import java.awt.Dimension;
import java.awt.GridLayout;


import javax.swing.*;
public class Calc {
	public static void main(String[] args) {
		BaseFrame f=new BaseFrame("hello");
		JPanel j1=new JPanel();
		JPanel j2=new JPanel();
		j1.setSize(800,100);
		j2.setBounds( 0,100, 800, 700);
		j1.setLayout(new GridLayout(1,1));
		JTextField t1=new JTextField();
		j1.add(t1);
		j2.setLayout(new GridLayout(4,4));
		int n=16;
		JButton[] btn=new JButton[n];
		for(int i=0;i<n;i++)
		{
			btn[i]=new JButton();
			btn[i].setPreferredSize(new Dimension(50,50));
			j2.add(btn[i]);
			
		}
		btn[0].setText("1");
		btn[1].setText("2");
		btn[2].setText("3");
		btn[3].setText("+");
		btn[4].setText("4");
		btn[5].setText("5");
		btn[6].setText("6");
		btn[7].setText("-");
		btn[8].setText("7");
		btn[9].setText("8");
		btn[10].setText("9");
		btn[11].setText("*");
		btn[12].setText("0");
		btn[13].setText(".");
		btn[14].setText("=");
		btn[15].setText("/");
		f.add(j1);
		f.add(j2);
		f.showMe();
		
	}
}




posted @ 2016-11-23 12:44  孙中明  阅读(113)  评论(0编辑  收藏  举报