
使用Java swing组件设计如下图像界面程序 105
展开全部
package utils1;
import java.awt.Container;
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.JPasswordField;
import javax.swing.JTextField;
public class AAA extends JFrame{
private JTextField username;
// 密码
private JPasswordField password;
// 小容器
private JLabel jl1;
private JLabel jl2;
private JLabel jl3;
private JLabel jl4;
// 小按钮
private JButton bu1;
private JButton bu2;
/*
* 构造方法
*/
public AAA() {
// 设置窗口标题
this.setTitle("登录练习");
// 窗体组件初始化
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局方式为绝对定位
this.setLayout(null);
this.setBounds(0, 0, 355, 265);
// 窗体大小不能改变
this.setResizable(false);
// 居中显示
this.setLocationRelativeTo(null);
// 窗体可见
this.setVisible(true);
}
/*
* 初始化方法
*/
public void init() {
// 创建一个容器
Container con = this.getContentPane();
jl4 = new JLabel("",JLabel.CENTER);
jl4.setBounds(15, 200, 300, 20);
jl4.setVisible(false);
jl1 = new JLabel();
jl1.setBounds(0, 0, 355, 265);
jl2 = new JLabel("姓名:");
jl2.setBounds(40, 80, 60, 20);
// 用户号码登录输入框
username = new JTextField();
username.setBounds(100, 80, 150, 20);
jl3 = new JLabel("密码:");
jl3.setBounds(40, 110, 60, 20);
// 密码输入框
password = new JPasswordField();
password.setBounds(100, 110, 150, 20);
// 按钮设定
bu1 = new JButton("登录");
bu1.setBounds(112, 150, 65, 20);
// 给按钮添加1个事件
bu1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if("登录".equals(str)){
String getName =username.getText();
String pwd = password.getText();
// String getPwd =password.getText();
if(getName.equals("user")&&pwd.equals("psd")){
jl4.setText("姓名和密码正确!");
}else{
jl4.setText("姓名和密码错误!");
}
jl4.setVisible(true);
}
}
});
bu2 = new JButton("取消");
bu2.setBounds(177, 150, 65, 20);
bu2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
username.setText("");
password.setText("");
jl4.setText("");
jl4.setVisible(false);
}
});
// 所有组件用容器装载
jl1.add(jl2);
jl1.add(jl3);
jl1.add(bu1);
jl1.add(bu2);
con.add(jl1);
con.add(username);
con.add(password);
con.add(jl4);
}
public static void main(String[] args) {
// 实例化对象
AAA qq = new AAA();
}
}
展开全部
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class test extends JFrame {
public static void main(String[] args) {
test test = new test();
}
private JButton submit,cancel;
private JLabel password,username,info;
private JTextField textusername;
private JPasswordField textpassword;
public test() {
super("login");
super.setSize(300, 200);
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.init();
this.addinfo();
this.setLayout(null);
this.repaint();
}
//初始化界面
void init()
{
username = new JLabel("用户名:");
username.setBounds(10, 10, 80, 20);
this.add(username);
textusername = new JTextField();
textusername.setBounds(90,10,150,20);
this.add(textusername);
password = new JLabel("密码:");
password.setBounds(10, 40, 80, 20);
this.add(password);
textpassword = new JPasswordField();
textpassword.setBounds(90,40,150,20);
this.add(textpassword);
submit = new JButton("登 录");
submit.setBounds(60,80,60,20);
this.add(submit);
cancel = new JButton("取消");
cancel.setBounds(130,80,60,20);
this.add(cancel);
info = new JLabel("请输入用户名和密码");
info.setBounds(90, 100, 150, 20);
this.add(info);
}
//加入监听器
void addinfo() {
submit.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
//用户名密码比较
if(textusername.getText().equals("123")&&textpassword.getText().equals("123"))
{
info.setText("用户名密码正确");
}
else
{
info.setText("用户名密码错误");
}
}
});
cancel.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
textusername.setText("");
textpassword.setText("");
}
});
}
}
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class test extends JFrame {
public static void main(String[] args) {
test test = new test();
}
private JButton submit,cancel;
private JLabel password,username,info;
private JTextField textusername;
private JPasswordField textpassword;
public test() {
super("login");
super.setSize(300, 200);
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.init();
this.addinfo();
this.setLayout(null);
this.repaint();
}
//初始化界面
void init()
{
username = new JLabel("用户名:");
username.setBounds(10, 10, 80, 20);
this.add(username);
textusername = new JTextField();
textusername.setBounds(90,10,150,20);
this.add(textusername);
password = new JLabel("密码:");
password.setBounds(10, 40, 80, 20);
this.add(password);
textpassword = new JPasswordField();
textpassword.setBounds(90,40,150,20);
this.add(textpassword);
submit = new JButton("登 录");
submit.setBounds(60,80,60,20);
this.add(submit);
cancel = new JButton("取消");
cancel.setBounds(130,80,60,20);
this.add(cancel);
info = new JLabel("请输入用户名和密码");
info.setBounds(90, 100, 150, 20);
this.add(info);
}
//加入监听器
void addinfo() {
submit.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
//用户名密码比较
if(textusername.getText().equals("123")&&textpassword.getText().equals("123"))
{
info.setText("用户名密码正确");
}
else
{
info.setText("用户名密码错误");
}
}
});
cancel.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
textusername.setText("");
textpassword.setText("");
}
});
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询