JavaSwing JTextField

leungqingyun發表於2024-12-04
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);
        
        
        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);