学习笔记 02计算器界面

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值