JavaSwing JTextField

复制代码
try
        {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
            //UIManager.put("RootPane.setupButtonVisible", false);
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            
        }
        catch(Exception e)
        {
            //TODO exception
        }
        
        // 创建窗体对象
        JFrame jFrame =new JFrame();
        // 设置窗体大小
        jFrame.setSize(800, 500);
        // 设置窗体全屏展示
        //jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        // 设置窗体显示位置
        //jFrame.setLocation(100,200);
        // 设置窗体显示正中间
        jFrame.setLocationRelativeTo(null);
        // 设置窗体标题
        jFrame.setTitle("窗体标题");
        // 设置窗体不可全屏显示
        //jFrame.setResizable(false);
        // 设置窗体关闭后退出程序
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置此窗口是否应该始终位于其他窗口上方
        jFrame.setAlwaysOnTop(true);
        // 设置窗体图标
        jFrame.setIconImage(new ImageIcon(HelloWorld.class.getResource("/images/book.png")).getImage());
        

        
        // 创建容器
        JPanel jPanel =new JPanel(null);
        
        JLabel usernameLabel =new JLabel("用户名:");
        usernameLabel.setFont(new Font("微软雅黑",Font.PLAIN,15));        
        usernameLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        usernameLabel.setSize(80, 30);
        usernameLabel.setLocation(100, 50);
        
        JTextField usernameTextField =new JTextField();
        usernameTextField.setFont(new Font("微软雅黑",Font.PLAIN,15));        
        usernameTextField.setSize(200, 30);
        usernameTextField.setLocation(220, 50);

         //设置是否可编辑

          usernameTextField.setEditable(true);


        
        
        JLabel passwordLabel =new JLabel("密码:");
        passwordLabel.setFont(new Font("微软雅黑",Font.PLAIN,15));        
        passwordLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        passwordLabel.setSize(80, 30);
        passwordLabel.setLocation(100, 100);
        
        
        JPasswordField passwordTextField =new JPasswordField();
        passwordTextField.setFont(new Font("微软雅黑",Font.PLAIN,15));        
        passwordTextField.setSize(200, 30);
        passwordTextField.setLocation(220, 100);
        
        
        JButton jButton =new JButton("确定");
        jButton.setFont(new Font("微软雅黑",Font.PLAIN,15));    
        jButton.setSize(200, 30);
        jButton.setLocation(220, 150);
        jButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        jButton.setForeground(Color.white);
        // beautyeye框架设置按钮背景颜色
        jButton.setUI(new BEButtonUI().setNormalColor(NormalColor.blue));
        
        
        jButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                String username=usernameTextField.getText();// 获取文本
                String password=new String(passwordTextField.getPassword());//获取密码
                System.out.println(username);
                System.out.println(password);
                
            }
        });
        
        
        usernameTextField.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // 输入用户名回车,自动进入到密码框
                passwordTextField.requestFocus();
                
            }
        });
        
        passwordTextField.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // 输入密码后回车,当按钮自动进入到监听器执行登录
                jButton.doClick();
                
            }
        });
        
        
        
        jPanel.add(usernameLabel);
        jPanel.add(passwordLabel);
        jPanel.add(usernameTextField);
        jPanel.add(passwordTextField);
        jPanel.add(passwordTextField);
        jPanel.add(jButton);
        
        jFrame.setContentPane(jPanel);

        
        // 设置窗体可见
        jFrame.setVisible(true);
复制代码

 

 

posted @   leungqingyun  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示