Swing组件的运用

import javax.swing.*;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JAVAJLABLE extends JFrame implements ActionListener{
	JLabel Name=null;       
	JLabel Cipher=null;         //密码
	JLabel Sex=null;         //性别
	JLabel Speciality=null;     //专业标签
	JLabel Photo=null;
	JRadioButton boy,girl;   //创建两个单选按钮
	ButtonGroup bg;   //创建一个选项组,把按钮放入其中
	JButton btn=new JButton("登录");
	JButton Registered=new JButton("注册");
	JButton Cancel=new JButton("取消");
	JTextField name=new JTextField(10);    //创建数字文本框    容量int为十
	JPasswordField cipher=new JPasswordField(10);  //创建密码框  容量string为十
	
	JMenuBar mb;
	JMenu m1,m2,m3;
	JMenuItem menuItem;
	
	
	void MyFarme(){
		this.setSize(300,400);  //设置窗口大小
		this.setTitle("JAVA程序!");
		this.setLayout(null);     //将面板默认布局为null
		//设计布局
		
		//实例化文本标签
		Name=new JLabel("用户名");
		Cipher=new JLabel("密码");
		
		Sex=new JLabel("性别:");
		Speciality=new JLabel("专业:");
		ImageIcon photo=new ImageIcon("C:/Users/hp/Desktop/123.jpg");   //照片实例化
		
		//将图片放大或者缩小,要导入java.awt.Image;包
		photo.setImage(photo.getImage().getScaledInstance(50, 50,Image.SCALE_DEFAULT));  
		JLabel Photo=new JLabel(photo);
		
		//创建两个单选按钮,分别为男,女
		 boy=new JRadioButton("男");
		 girl=new JRadioButton("女");
		//创建一个选项组,把按钮放入其中
		 bg=new ButtonGroup();
		bg.add(boy);bg.add(girl);
		
		//创建专业下拉列表
		Object[] Content=new Object[] {"大数据","嵌入式","软英","软件会计"};  //列表内容
		JComboBox speciality=new JComboBox(Content);
		
		//标签位置以及 大小
		Name.setBounds(50,55 , 60, 26);          //账户,密码的位置和大小
		Cipher.setBounds(50,80 , 60, 26);
		
		name.setBounds(90, 55, 120, 26);
		name.setEditable(false);
		cipher.setBounds(90, 80, 120, 26);
		cipher.setEditable(false);
		
		Sex.setBounds(50, 110, 60, 26);             //性别单选按钮的位置和大小
		boy.setBounds(90, 110, 60, 26);
		girl.setBounds(130, 110, 60, 26);
		
		Speciality.setBounds(50, 140, 60, 26);      //专业以及专业滚动面板的位置以及大小
		speciality.setBounds(90, 140, 120, 26);
		
		Photo.setBounds(10, 10, 60, 40);    //图片的位置以及大小
		
		btn.setBounds(100, 180, 90, 40);     //登录注册取消按钮的位置以及大小
		Registered.setBounds(10, 230, 60, 40);
		Cancel.setBounds(215, 230, 60, 40);
		
		//分隔面板的文件和编辑
		mb=new JMenuBar();
		m1=new JMenu("文件");
		m2=new JMenu("编辑");
		menuItem=new JMenuItem("保存");
		mb.add(m1);
		mb.add(m2);
		m2.add(menuItem);
		
		//将组件显示在窗口里
		this.add(Name);
		this.add(Cipher);
		this.add(name);
		this.add(cipher);
		this.add(Sex);
		this.add(girl);
		this.add(boy);
		this.add(Speciality);
		this.add(speciality);
		this.add(Photo);
		this.add(btn);
		this.add(Registered);
		this.add(Cancel);
		this.setJMenuBar(mb);
		
		//事件监测所必须
		Registered.addActionListener(this);
		Cancel.addActionListener(this);
		btn.addActionListener(this);
		menuItem.addActionListener(this);
		
		
		this.setLocationRelativeTo(null);//将窗口居中显示
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == Cancel) {
			name.setText("");
			 // 创建数字文本框 容量int为十
			cipher.setText("");
			name.setEditable(false);
			cipher.setEditable(false);
		}

		if (e.getSource() == Registered||e.getSource() ==btn) {
			name.setEditable(true);
			cipher.setEditable(true);
		}
		if(e.getSource() == menuItem){
			name.setEditable(true);
			cipher.setEditable(true);
			name.setText("");
			cipher.setText("");
			name.setEditable(false);
			cipher.setEditable(false);
		}
	}
}
public class test {
	public static void main(String args[]) {
		new JAVAJLABLE().MyFarme();
		
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值