Java_GUI实现简易计算器界面

import java.awt.*;
import javax.swing.*;

public class Calculater {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame f = new JFrame("简易计算器");
		f.setSize(440,500);//设置窗口的大小
		f.setLocation(500, 200);//位置
		f.setResizable(false);//设置窗口的大小不可修改

		
		//设置面板
		JPanel p=new JPanel(null);
		p.setBounds(20,60,400, 400);
		p.setLayout(new GridLayout(4, 4));//4行4列
		
		
		String[] name={"7","8","9","*","4","5","6","-","1","2","3","+","/","0",".","="};
		for(int i=0;i<name.length;i++){
			JButton b=new JButton(name[i]);
			b.setSize(50, 50);
			p.add(b);
		}				
					
		 //设置文本框,初始化文字为0        
		JTextField text=new JTextField("0");           
		text.setBounds(20, 10, 400, 50);         
		text.setBackground(Color.pink);              
		text.setHorizontalAlignment(JTextField.RIGHT);   //设置文本框的水平对齐格式为右对齐              
		
		
		f.setLayout(null);
		f.add(p);        
		f.add(text);        
		f.setVisible(true);//可见
	}

}

你可能感兴趣的:(Java_GUI实现简易计算器界面)