练习java布局管理器

    试着发一篇博文.
package com.dusker.calc;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TestCalc extends JFrame implements ActionListener {

	/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * @param args
 */
JPanel p1=null;
JPanel p2=null;
JPanel p3=null;

public static void main(String[] args) {
	new TestCalc().layOut();
}

public void  layOut()  {
	
		p1 = new JPanel(new FlowLayout());
		JButton backButton = new JButton("BackSpace");
		JButton clearButton = new JButton("Clear");
		p1.add(backButton);
		p1.add(clearButton);
		
		p2 = new JPanel(new GridLayout(2,1));
		JTextField text = new JTextField();
		p2.add(text);
		p2.add(p1);
		
		p3 = new JPanel(new GridLayout(4,5));
		String buttonStr = "789/A456*B123-C0D.+=";
		
		for(int i=0;i<buttonStr.length();i++) {
		this.addButton(p3, buttonStr.substring(i,i+1));
		}
		
		this.setLocation(500, 200);
		this.setSize(300,330);
		this.setTitle("计算器");
		this.setLayout(new BorderLayout());
		this.add(p2,BorderLayout.NORTH);
		this.add(p3,BorderLayout.CENTER);

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
}
	
	private void addButton(Container c,String s) {
		JButton b = new JButton(s);
		
		if(s.equals("A")) {
			b.setText("sqrt");
		}
		if(s.equals("B")) {
			b.setText("%");
		}
		if(s.equals("C")) {
			b.setText("1/x");
		}
		if(s.equals("D")) {
			b.setText("+/-");
		}
		
		c.add(b);
		b.addActionListener(this);			
	}
	public void actionPerformed(ActionEvent arg0) {
	}
}

你可能感兴趣的:(java,练习代码,计算器界面)