解答1

package test;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class re {
	private static void placeComponents(JPanel panel) {

		/* 布局部分我们这边不多做介绍
		 * 这边设置布局为 null
		 */
		panel.setLayout(null);

		// 创建 JLabel
		JLabel userLabel = new JLabel("请输入您的新密码");
		/* 这个方法定义了组件的位置。
		 * setBounds(x, y, width, height)
		 * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
		 */
		userLabel.setBounds(10,20,120,25);
		panel.add(userLabel);

		/* 
		 * 创建文本域用于用户输入
		 */
		JPasswordField userText = new JPasswordField(20);
		userText.setBounds(10,50,165,25);
		panel.add(userText);

		// 输入密码的文本域
		JLabel passwordLabel = new JLabel("请再次输入您的新密码");
		passwordLabel.setBounds(10,80,120,25);
		panel.add(passwordLabel);

		/* 
		 *这个类似用于输入的文本域
		 * 但是输入的信息会以点号代替,用于包含密码的安全性
		 */
		JPasswordField passwordText = new JPasswordField(20);
		passwordText.setBounds(10,110,165,25);
		panel.add(passwordText);
		
		JLabel label=new JLabel();
		label.setBounds(10, 135, 80, 25);
		panel.add(label);
		// 创建登录按钮
		JButton loginButton = new JButton("修改");
		loginButton.setBounds(10, 160, 80, 25);
		loginButton.addActionListener(new ActionListener() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (userText.getText().equals(passwordText.getText())) {
					label.setText("");
				}else {
					label.setText("密码不一致");
				}
				
			}
		});
		panel.add(loginButton);
	}
	public static void main(String[] args) {    
		// 创建 JFrame 实例
		JFrame frame = new JFrame("Login Example");
		// Setting the width and height of frame
		frame.setSize(500, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		/* 创建面板,这个类似于 HTML 的 div 标签
		 * 我们可以创建多个面板并在 JFrame 中指定位置
		 * 面板中我们可以添加文本字段,按钮及其他组件。
		 */
		JPanel panel = new JPanel();    
		// 添加面板
		frame.add(panel);
		/* 
		 * 调用用户定义的方法并添加组件到面板
		 */
		placeComponents(panel);

		// 设置界面可见
		frame.setVisible(true);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值