永发信息网

如何用java做登录界面

答案:1  悬赏:70  手机版
解决时间 2021-12-31 09:04
  • 提问者网友:我们很暧昧
  • 2021-12-31 03:14
import java.awt.Container;
import java.awt.Image;

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

public class loginDe extends JFrame {
//账号
JTextField txtName;
//密码
JPasswordField password;
//小容器
JLabel jl1,jl2,jl3;
//小按钮
JButton bu1,bu2;
public loginDe(){
super("学生成绩管理系统");
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局方式为绝对定位
this.setLayout(null);

this.setBounds(0, 0, 800, 400);

// 窗体大小不能改变
this.setResizable(true);

// 居中显示
this.setLocationRelativeTo(null);

// 窗体可见
this.setVisible(true);
}
private void init() {
// TODO 自动生成的方法存根
Container cc = this.getContentPane();
jl1=new JLabel();
//设置背景图片
Image image1=new ImageIcon("f:/1.jpg").getImage();
jl1.setIcon(new ImageIcon(image1));
jl1.setBounds(0,0,800,400);
txtName=new JTextField();
txtName.setBounds(100,100,150,20);
jl2=new JLabel("账号");
jl2.setBounds(260, 100, 70, 20);

password = new JPasswordField();
password.setBounds(100, 130, 150, 20);
// 密码输入框旁边的文字
jl3 = new JLabel("密码");
jl3.setBounds(260, 130, 70, 20);
bu1 = new JButton("登录");
bu1.setBounds(280, 200, 65, 20);
bu2 = new JButton("快速注册");
bu2.setBounds(280, 200, 65, 20);
jl1.add(jl2);
jl1.add(jl3);
jl1.add(bu1);
jl1.add(bu2);
cc.add(jl1);
cc.add(txtName);
cc.add(password);

}

public static void main(String[] args) {
// TODO 自动生成的方法存根
new loginDe();
}

}

哪里错了,我找不出来,要求登录和快速注册两个按钮分别排成两行,不要排成一行,要四行,如何做的
要求如下:

账号:
密码:
登录
快速注册
最佳答案
  • 五星知识达人网友:低音帝王
  • 2021-12-31 04:11
import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按钮
import javax.swing.JLabel;//标签
import javax.swing.JTextField;//文本框
import java.awt.Font;//字体
import java.awt.Color;//颜色
import javax.swing.JPasswordField;//密码框
import java.awt.event.ActionListener;//事件监听
import java.awt.event.ActionEvent;//事件处理
import javax.swing.JOptionPane;//消息窗口public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;

public UserLogIn(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}

public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序
this.setSize(300,200);//设置框架大小为长300,宽200
this.setResizable(false);//设置框架不可以改变大小
this.setTitle("用户登录");//设置框架标题
this.pnluser.setLayout(null);//设置面板布局管理
this.pnluser.setBackground(Color.cyan);//设置面板背景颜色
this.lbluserLogIn.setText("用户登录");//设置标签标题
this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14));//设置标签字体
this.lbluserLogIn.setForeground(Color.RED);//设置标签字体颜色
this.lbluserName.setText("用户名:");
this.lbluserPWD.setText("密 码:");
this.btnSub.setText("登录");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//设置标签x坐标120,y坐标15,长60,宽20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//加载标签到面板
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//加载面板到框架
this.setVisible(true);//设置框架可显
}

public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
return;
}
}

public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}

public static void main(String[] args){
new UserLogIn();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯