鱼C论坛

 找回密码
 立即注册
查看: 3253|回复: 3

[技术交流] [2021/8/20]每周一题-1

[复制链接]
发表于 2021-8-20 12:16:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 不会起名字的我 于 2021-8-20 12:24 编辑

Swing开发


仔细阅读下面这段代码,回答问题。
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;

  3. import javax.swing.*;

  4. public class Demo extends JFrame {
  5.         private String username;
  6.         private String pwd;
  7.         private JButton login;
  8.         private JTextField inputusername;
  9.         private JPasswordField inputpwd;

  10.         public Demo() {
  11.                 init();
  12.         }

  13.         void init() {
  14.                 setSize(500, 300);
  15.                 setDefaultCloseOperation(EXIT_ON_CLOSE);
  16.                 setTitle("Demo");
  17.                 JLabel l1 = new JLabel("用户名");
  18.                 JLabel l2 = new JLabel("密码");
  19.                 add(l1);
  20.                 add(getUsernameTextfield());
  21.                 add(l2);
  22.                 add(getPwdpasswordfield());
  23.                 add(getloginButton());
  24.         }

  25.         private JButton getloginButton() {
  26.                 login = new JButton("登录");
  27.                 login.setSize(30, 20);
  28.                 login.addActionListener(new ActionListener() {

  29.                         @Override
  30.                         public void actionPerformed(ActionEvent arg0) {
  31.                                 if (getUsernameTextfield().getText() == username) {
  32.                                         String password = String.valueOf(getPwdpasswordfield().getPassword());
  33.                                         if (password == pwd) {
  34.                                                 JOptionPane.showMessageDialog(null, "登录成功!");
  35.                                         } else {
  36.                                                 JOptionPane.showMessageDialog(null, "密码错误", "错误", JOptionPane.ERROR_MESSAGE);
  37.                                         }
  38.                                 } else {
  39.                                         JOptionPane.showMessageDialog(null, "不存在的用户名", "错误", JOptionPane.ERROR_MESSAGE);
  40.                                 }

  41.                         }
  42.                 });

  43.                 return login;
  44.         }

  45.         private JTextField getUsernameTextfield() {
  46.                 inputusername = new JTextField(10);
  47.                 return inputusername;
  48.         }

  49.         private JPasswordField getPwdpasswordfield() {
  50.                 inputpwd = new JPasswordField(10);
  51.                 inputpwd.setEchoChar('*');
  52.                 inputpwd.addActionListener(new ActionListener() {

  53.                         @Override
  54.                         public void actionPerformed(ActionEvent e) {
  55.                                 login.doClick();
  56.                         }
  57.                 });

  58.                 return inputpwd;
  59.         }

  60.         public static void main(String[] args) {
  61.                 new Demo().setVisible(true);
  62.         }
  63. }
复制代码


你知道为什么运行后整个窗体都是一个按钮吗?

答案

游客,如果您要查看本帖隐藏内容请回复
[/hide]

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-26 19:37:02 | 显示全部楼层
催更!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-8 17:23:27 From FishC Mobile | 显示全部楼层
6
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-1-7 19:13:33 | 显示全部楼层
why
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 19:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表