weixin_51370920 2021-05-26 10:31 采纳率: 100%
浏览 64
已采纳

Java的awt,我想知道,每触发一个按钮,弹出来一个子窗体,

我想知道,每触发一个按钮,弹出来一个子窗体,该页面的任务栏是怎么做的?用了什么方法,还是某个类,

  • 写回答

3条回答 默认 最新

  • 关注

    使用按钮的action事件,实现ActionLinster接口,new 窗口类就可以了。

     

    登录界面

    package java3.T9;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    
    public class LoginUI extends JFrame implements ActionListener,MouseListener{
    	
    	JLabel bgImg,lblAccount,lblPassword,lblReg,lblForgetPwd;
    	JTextField txtAccount;
    	JPasswordField txtPassword;
    	JButton btnLogin,btnExit;
    	
    	public LoginUI() {
    		setTitle("QQ登录");
    		ImageIcon icon = new ImageIcon("upload/touxiang.png");
    		//设置图标
    		setIconImage(icon.getImage());
    		
    		//设置背景图片
    		Icon bgIcon = new ImageIcon("upload/login.jpg");
    		bgImg = new JLabel(bgIcon);
    		//窗口的默认布局是边框布局
    		//把bgImg设置的窗口
    		add(bgImg);
    		
    		lblAccount = new JLabel("账号:",JLabel.RIGHT);
    		lblPassword = new JLabel("密码:",JLabel.RIGHT);
    		txtAccount = new JTextField(20);
    		txtPassword = new JPasswordField(20);
    		
    		lblAccount.setBounds(78, 173, 50, 30);
    		txtAccount.setBounds(128, 173, 194, 30);
    		
    		lblPassword.setBounds(78, 203, 50, 30);
    		txtPassword.setBounds(128, 203, 194, 30);
    		//设置密码框显示的字符
    		txtPassword.setEchoChar('*');
    		
    		lblReg = new JLabel("注册账号");
    		lblForgetPwd = new JLabel("找回密码");
    		lblReg.setBounds(330, 170, 80, 30);
    		lblForgetPwd.setBounds(330, 200, 80, 30);
    		
    		btnLogin = new JButton("登录(L)");
    		//设置热键
    		btnLogin.setMnemonic('L');
    		btnExit = new JButton("关闭(C)");
    		btnExit.setMnemonic('C');
    		btnLogin.setBounds(128, 250, 80, 30);
    		btnExit.setBounds(228, 250, 80, 30);
    		
    		//添加到bgImg里面
    		bgImg.add(lblAccount);
    		bgImg.add(txtAccount);
    		bgImg.add(lblPassword);
    		bgImg.add(txtPassword);
    		bgImg.add(lblReg);
    		bgImg.add(lblForgetPwd);
    		bgImg.add(btnLogin);
    		bgImg.add(btnExit);
    		//为按钮添加事件
    		btnLogin.addActionListener(this);
    		btnExit.addActionListener(this);
    		//添加鼠标事件
    		lblReg.addMouseListener(this);
    		lblForgetPwd.addMouseListener(this);
    		//不允许改变窗口的大小
    		setResizable(false);
    		
    		setSize(427,321);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
    		
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		if(e.getSource() == lblReg) {
    			//弹出注册窗口
    			new RegUI();
    		}
    		
    	}
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		if(e.getSource() == btnLogin) {
    			JOptionPane.showMessageDialog(null, "登录成功");
    		}else if(e.getSource() == btnExit) {
    			System.exit(0);
    		}
    		
    	}
    	public static void main(String[] args) {
    		new LoginUI();
    	}
    }
    

    点击注册,弹出注册窗口

    package java3.T9;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ButtonGroup;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    /*
     * Swing所在的包:java.swing
     * 	1.常用组件的使用
     * */
    public class RegUI extends JFrame implements ActionListener{
    	
    	//姓名,年龄
    	JTextField txtUserName,txtAge; //文本框
    	//学历,民族,星座
    	JComboBox cbDegree,cbNation,cbStar; //下拉列表框
    	//性别
    	JRadioButton rbMale,rbRemale; //多选框
    	//兴趣爱好
    	JCheckBox cbFootball,cbBasketBall,cbNet,cbWatch; //多选框
    	//备注
    	JTextArea txtRemark; //文本区域框
    	//注册,关闭
    	JButton btnSave,btnClose; //按钮
    	
    	//定义学历数组
    	String degreeArr[] = {"小学","初中","高中","大专","本科","研究生","博士"};
    	//定义民族数组
    	String nationArr[] = {"汉族","苗族","藏族","高山族","朝鲜族","畲族","壮族","其他"};
    	//定义星座数组
    	String starArr[] = {"双鱼座","处女座","狮子座","金牛座","巨蟹座","天秤座","水瓶座","双子座","白羊座","天蝎座","摩羯座","射手座"};
    	public RegUI() {
    		
    		super("注册用户");
    		//设置布局为绝对布局
    		setLayout(null);
    		//工具类
    		Toolkit kit = Toolkit.getDefaultToolkit();
    		Image image = kit.getImage("upload/qq.jpg");
    		//设置窗口图标
    		setIconImage(image);
    
    //		ImageIcon icon = new ImageIcon("upload/qq.jpg");
    //		setIconImage(icon.getImage());
    		
    		JLabel lblTitle,lblUserName,lblAge,lblDegree,lblNation,lblStar,lblSex,lblHover,lblRemark;
    		lblTitle = new JLabel("用户注册",new ImageIcon("upload/qq.jpg"),JLabel.CENTER);
    		Font font = new Font("宋体",Font.BOLD,20);
    		lblTitle.setFont(font);		
    		lblTitle.setBounds(0, 10, 500, 30);
    		lblTitle.setForeground(Color.RED);
    		//添加到窗口
    		add(lblTitle);
    		
    		lblUserName = new JLabel("用户名称",JLabel.RIGHT);
    		txtUserName = new JTextField(20); //默认显示的字符数
    		//设置控件在窗口的位置
    		lblUserName.setBounds(80, 50, 100, 20);
    		txtUserName.setBounds(180, 50, 200, 20);
    		//添加控件到窗口指定位置显示
    		add(lblUserName);
    		add(txtUserName);
    		lblAge = new JLabel("年龄",JLabel.RIGHT);
    		txtAge = new JTextField(20);
    		lblAge.setBounds(80, 80, 100, 20);
    		txtAge.setBounds(180, 80, 200, 20);
    		add(lblAge);
    		add(txtAge);
    		
    		lblDegree = new JLabel("学历",JLabel.RIGHT);
    		//学历控件中显示的是数组的值
    		cbDegree = new JComboBox(degreeArr);
    		
    		lblDegree.setBounds(80, 110, 100, 20);
    		cbDegree.setBounds(180, 110, 200, 20);
    		add(lblDegree);
    		add(cbDegree);
    		
    		lblNation = new JLabel("民族",JLabel.RIGHT);
    		//民族控件中显示的是数组的值
    		cbNation = new JComboBox(nationArr);
    		
    		lblNation.setBounds(80, 140, 100, 20);
    		cbNation.setBounds(180, 140, 200, 20);
    		add(lblNation);
    		add(cbNation);
    		
    		lblStar = new JLabel("星座",JLabel.RIGHT);
    		//民族控件中显示的是数组的值
    		cbStar = new JComboBox(starArr);
    		
    		lblStar.setBounds(80, 170, 100, 20);
    		cbStar.setBounds(180, 170, 200, 20);
    		add(lblStar);
    		add(cbStar);
    		
    		lblSex = new JLabel("性别",JLabel.RIGHT);
    		rbMale = new JRadioButton("男");
    		rbRemale = new JRadioButton("女");
    		//同一组的单选框必须添加到一个ButtonGroup组件
    		ButtonGroup gb = new ButtonGroup();
    		gb.add(rbMale);
    		gb.add(rbRemale);
    		lblSex.setBounds(80, 200, 100, 20);
    		rbMale.setBounds(180, 200, 50, 20);
    		//默认选中
    		rbMale.setSelected(true);
    		rbRemale.setBounds(230, 200, 50, 20);
    		add(lblSex);
    		add(rbMale);
    		add(rbRemale);
    		
    //		cbFootball,cbBasketBall,cbNet,cbWatch
    		lblHover = new JLabel("兴趣爱好",JLabel.RIGHT);
    		cbFootball = new JCheckBox("足球");
    		cbBasketBall = new JCheckBox("篮球");
    		cbNet = new JCheckBox("上网");
    		cbWatch = new JCheckBox("看书");
    		lblHover.setBounds(80, 230, 100, 20);
    		cbFootball.setBounds(180, 230, 60, 20);
    		cbBasketBall.setBounds(240, 230, 60, 20);
    		cbNet.setBounds(300, 230, 60, 20);
    		cbWatch.setBounds(360, 230, 60, 20);
    		add(lblHover);
    		add(cbFootball);
    		add(cbBasketBall);
    		add(cbNet);
    		add(cbWatch);
    		
    		lblRemark = new JLabel("备注",JLabel.RIGHT);
    		txtRemark = new JTextArea(5, 80);
    		lblRemark.setBounds(80, 260, 100, 20);
    		txtRemark.setBounds(80,290, 330, 100);
    		
    		add(lblRemark);
    		JScrollPane bar = new JScrollPane(txtRemark);
    		bar.setBounds(80,290, 330, 100);
    		add(bar);
    		
    		btnSave = new JButton("注册(S)");
    		//设置热键(先按住ALT键+S)
    		btnSave.setMnemonic('S');
    		btnClose = new JButton("关闭(X)");
    		//设置热键(先按住ALT键+X)
    		btnClose.setMnemonic('X');
    		btnSave.setBounds(250,430, 100, 30);
    		btnClose.setBounds(360,430, 100, 30);
    		//注册按钮事件
    		btnClose.addActionListener(this);
    		btnSave.addActionListener(this);
    		
    		add(btnSave);
    		add(btnClose);
    		setSize(500,500);
    		setVisible(true);
    		//DISPOSE_ON_CLOSE,关闭当前窗口,但不退出程序
    		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		setLocationRelativeTo(null);
    	}
    	//Action事件
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		if(e.getSource() == btnSave) {
    			//弹出对话框
    			JOptionPane.showMessageDialog(null, "你点击了注册按钮");
    		}else if(e.getSource() == btnClose) {
    			JOptionPane.showMessageDialog(null, "你点击了关闭按钮");
    			//退出程序
    //			System.exit(0);
    			//关闭窗口
    //			setVisible(false);
    			dispose();
    		}
    	}
    	public static void main(String[] args) {
    		new RegUI();
    	}
    
    	
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效