JApplet改造成JFrame參考程式碼import java.awt.*;import java.awt.event.*;import javax.swing.*;public class JPasswordExample extends JFrame {//該類繼承與JFrameprivate String pw = "password115";private JPasswordField passwordField = new JPasswordField(16);public JPasswordExample() {//寫一個建構函式init();//呼叫方法,完成介面的繪製,並且顯示出來}public void init(){Container contentPanel = getContentPane();JPanel panel = new JPanel();panel.add(new JLabel("Password:"));panel.add(passwordField);passwordField.setEchoChar("*");contentPanel.add(panel, BorderLayout.CENTER);JLabel jl = new JLabel();//新增一個JLabel標籤 ,該標籤用於顯示提示的文字contentPanel.add(jl,BorderLayout.SOUTH);//標籤放到最下面passwordField.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String password = new String(passwordField.getPassword());if (pw.equals(password))jl.setText("密碼正確,歡迎你!");//jl.setText()方法來修改標籤的文字elsejl.setText("錯誤的密碼,請重新輸入!");//jl.setText()方法來修改標籤的文字}});setTitle("測試");//視窗標題名setDefaultCloseOperation(EXIT_ON_CLOSE);//點選視窗關閉按鈕退出系統setLocationRelativeTo(null);//視窗居中顯示setSize(220,300);//視窗大小setVisible(true);//視窗顯示可見}public static void main(String[] args) {new JPasswordExample();//建立一個視窗物件}}測試程式碼
JApplet改造成JFrame參考程式碼import java.awt.*;import java.awt.event.*;import javax.swing.*;public class JPasswordExample extends JFrame {//該類繼承與JFrameprivate String pw = "password115";private JPasswordField passwordField = new JPasswordField(16);public JPasswordExample() {//寫一個建構函式init();//呼叫方法,完成介面的繪製,並且顯示出來}public void init(){Container contentPanel = getContentPane();JPanel panel = new JPanel();panel.add(new JLabel("Password:"));panel.add(passwordField);passwordField.setEchoChar("*");contentPanel.add(panel, BorderLayout.CENTER);JLabel jl = new JLabel();//新增一個JLabel標籤 ,該標籤用於顯示提示的文字contentPanel.add(jl,BorderLayout.SOUTH);//標籤放到最下面passwordField.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String password = new String(passwordField.getPassword());if (pw.equals(password))jl.setText("密碼正確,歡迎你!");//jl.setText()方法來修改標籤的文字elsejl.setText("錯誤的密碼,請重新輸入!");//jl.setText()方法來修改標籤的文字}});setTitle("測試");//視窗標題名setDefaultCloseOperation(EXIT_ON_CLOSE);//點選視窗關閉按鈕退出系統setLocationRelativeTo(null);//視窗居中顯示setSize(220,300);//視窗大小setVisible(true);//視窗顯示可見}public static void main(String[] args) {new JPasswordExample();//建立一個視窗物件}}測試程式碼