...个登陆页面,要求点击登录按钮时,判断用户名和密码是不是都为“123...

发布网友 发布时间:2024-10-24 11:22

我来回答

3个回答

热心网友 时间:7分钟前

楼上的辛苦了,但帮人帮到底,再加点注释,更容易理解
public class LoginFrame extends JFrame {
JTextField jtf1;
JTextField jtf2;
//构造方法,调用初始化方法
public LoginFrame() {
init();
}
//初始化当前的frame
private void init(){
//下面的方法都是从Frame继承的,用来设置Frame的标题,位置和大小
setTitle("QQ登录");
setLocation(300, 200);
setSize(300, 200);
//设置内容面板
setContentPane(createContentPane());
}
//设置内容面板

private JPanel createContentPane(){
JPanel pane = new JPanel(new BorderLayout(0,8));
pane.setBorder(new EmptyBorder(12,12,12,12));
pane.add(BorderLayout.NORTH,
new JLabel("", JLabel.CENTER));
pane.add(BorderLayout.CENTER,
createNamePwdPane());
pane.add(BorderLayout.SOUTH,
createBtnPane());
return pane;
}
//这下面都是创建用户名、密码标签和文本框,然后加到面板上显示
private JPanel createNamePwdPane(){
JPanel pane = new JPanel(new BorderLayout());
JPanel top = new JPanel(new GridLayout(2,1,0,6));
pane.add(BorderLayout.NORTH, top);
top.add(createNamePane());
top.add(createPwdPane());
return pane;
}
private JPanel createNamePane(){
JPanel pane = new JPanel(new BorderLayout(4,0));
pane.add(BorderLayout.WEST,
new JLabel("用户名:"));
jtf1= new JTextField();
pane.add(BorderLayout.CENTER,
jtf1);
return pane;
}
private JPanel createPwdPane(){
JPanel pane = new JPanel(new BorderLayout(4,0));
pane.add(BorderLayout.WEST,
new JLabel("密 码:"));
jtf2= new JPasswordField();
pane.add(BorderLayout.CENTER,jtf2 );
return pane;
}

private JPanel createBtnPane(){
JPanel pane = new JPanel(new FlowLayout());
JButton login = new JButton("登录");
login.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
//这里才是楼主要的关键代码,getText()是得到用户输入的文本内容,判断是否等于123
if(jtf2.getText().equals("123")&&jtf1.getText().equals("123")){
System.out.println("登录 ");
//不等就输出登录失败
}else{
System.out.println("登录失败");
}
}
});
//创建退出按钮
JButton cancel = new JButton("退出");
//给退出按钮加监听,点击该按钮时就退出系统
cancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
pane.add(login);
pane.add(cancel);
return pane;
}
public void showView(){
setVisible(true);
}
public static void main(String[] args) {
LoginFrame lf=new LoginFrame();
lf.showView();
}
}

热心网友 时间:5分钟前

这么EASY的问题也要拿出来问。。。看来你是没学好JAVA

热心网友 时间:1分钟前

package test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
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;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class LoginFrame extends JFrame {
JTextField jtf1;
JTextField jtf2;
public LoginFrame() {
init();
}

private void init(){
setTitle("QQ登录");
setLocation(300, 200);
setSize(300, 200);
//设置内容面板
setContentPane(createContentPane());
}
private JPanel createContentPane(){
JPanel pane = new JPanel(new BorderLayout(0,8));
pane.setBorder(new EmptyBorder(12,12,12,12));
pane.add(BorderLayout.NORTH,
new JLabel("", JLabel.CENTER));
pane.add(BorderLayout.CENTER,
createNamePwdPane());
pane.add(BorderLayout.SOUTH,
createBtnPane());
return pane;
}
private JPanel createNamePwdPane(){
JPanel pane = new JPanel(new BorderLayout());
JPanel top = new JPanel(new GridLayout(2,1,0,6));
pane.add(BorderLayout.NORTH, top);
top.add(createNamePane());
top.add(createPwdPane());
return pane;
}
private JPanel createNamePane(){
JPanel pane = new JPanel(new BorderLayout(4,0));
pane.add(BorderLayout.WEST,
new JLabel("用户名:"));
jtf1= new JTextField();
pane.add(BorderLayout.CENTER,
jtf1);
return pane;
}
private JPanel createPwdPane(){
JPanel pane = new JPanel(new BorderLayout(4,0));
pane.add(BorderLayout.WEST,
new JLabel("密 码:"));
jtf2= new JPasswordField();
pane.add(BorderLayout.CENTER,jtf2 );
return pane;
}

private JPanel createBtnPane(){
JPanel pane = new JPanel(new FlowLayout());
JButton login = new JButton("登录");
login.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if(jtf2.getText().equals("123")&&jtf1.getText().equals("123")){
System.out.println("登录 ");
}else{
System.out.println("登录失败");
}
}
});
JButton cancel = new JButton("退出");
cancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
pane.add(login);
pane.add(cancel);
return pane;
}
public void showView(){
setVisible(true);
}
public static void main(String[] args) {
LoginFrame lf=new LoginFrame();
lf.showView();
}
}
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com