关闭

学习笔记 02计算器界面

138人阅读 评论(0) 收藏 举报
package cn.edu.hytc;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.xml.stream.events.Comment;

public class GridBagGrame extends JFrame {
	public GridBagGrame()
	{
		JPanel panel=(JPanel)this.getContentPane();
		GridBagLayout bag=new GridBagLayout();
		panel.setLayout(bag);
		
		//标签
		JLabel label=new JLabel("0");
		//label内容的对齐方式
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		//GridBagConstraints constraints=new GridBagConstraints();
		//constraints.gridx=0;
		//constraints.gridy=0;
		//constraints.gridwidth=4;
	//	constraints.gridheight=1;
	//	panel.add(label,constraints);
		this.add(label,0,0,4,1);
		
		
		JButton b1=new JButton("C");
		JButton b2=new JButton("+/-");
		JButton b3=new JButton("%");
		JButton b4=new JButton("+");
		this.add(b1,0,1,1,1);
		this.add(b2,1,1,1,1);
		this.add(b3,2,1,1,1);
		this.add(b4,3,1,1,1);
		
		JButton b5=new JButton("7");
		JButton b6=new JButton("8");
		JButton b7=new JButton("9");
		JButton b8=new JButton("-");
		this.add(b5,0,2,1,1);
		this.add(b6,1,2,1,1);
		this.add(b7,2,2,1,1);
		this.add(b8,3,2,1,1);
		
		JButton b9=new JButton("4");
		JButton b10=new JButton("5");
		JButton b11=new JButton("6");
		JButton b12=new JButton("*");
		this.add(b9,0,3,1,1);
		this.add(b10,1,3,1,1);
		this.add(b11,2,3,1,1);
		this.add(b12,3,3,1,1);
		
		JButton b13=new JButton("1");
		JButton b14=new JButton("2");
		JButton b15=new JButton("3");
		JButton b16=new JButton("/");
		this.add(b13,0,4,1,1);
		this.add(b14,1,4,1,1);
		this.add(b15,2,4,1,1);
		this.add(b16,3,4,1,1);
		
		JButton b17=new JButton("0");
		JButton b18=new JButton(".");
		JButton b19=new JButton("=");
		this.add(b17,0,5,2,1);
		this.add(b18,2,5,1,1);
		this.add(b19,3,5,1,1);

		this.setBounds(200, 200, 300, 400);
		this.setVisible(true);
		
	
	}
	
	
	public void add(Component cop,int x,int y,int width,int height)
	{
		JPanel panel=(JPanel)this.getContentPane();
		GridBagConstraints constraints=new GridBagConstraints();
		constraints.fill=GridBagConstraints.BOTH;
		constraints.weightx=100;
		constraints.weighty=100;
		constraints.gridx=x;//列
		constraints.gridy=y;//行
		constraints.gridwidth=width;
		constraints.gridheight=height;
		panel.add( cop,constraints);
	}
	public static void main(String args[])
	{
		
		new GridBagGrame();
	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:993次
    • 积分:49
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档