|
■ swingコンポーネント->JPasswordField |
|||
|
パスワードフィールドはフィールドに入力された全ての文字を’*’を表示します。 ’*’はエコー文字と呼ばれます。 |
|||
↑ パスワードは javanyumon です。 |
|||
| コンストラクタ(抜粋) | |||
|
|||
| メソッド(抜粋) | |||
|
|||
|
|
|||
| PassSample.java | |||
| import java.awt.*; | |||
| import java.awt.event.*; | |||
| import javax.swing.*; | |||
|
public class
PassSample extends JApplet implements ActionListener{ |
|||
| Container contentPane; | |||
| JPasswordField pass = new JPasswordField(); | ← @インスタンス生成 | ||
| JLabel lbl = new JLabel("パスワードを入力してください。"); | |||
| JButton btn = new JButton("パスワード入力"); | |||
| private String passStr = "javanyumon"; | |||
| public void init() { | |||
| contentPane = getContentPane(); | |||
| setLayout(new BorderLayout()); | |||
| add(BorderLayout.NORTH, lbl); | |||
| add(BorderLayout.CENTER, pass); | |||
| add(BorderLayout.SOUTH, btn); | |||
| btn.addActionListener(this); | |||
| } | |||
| public void actionPerformed(ActionEvent e) { | |||
| String str = new String(pass.getPassword()); | ← A | ||
| if(str.equals(passStr)){ | ← B | ||
| lbl.setText("パスワードが一致しました"); | |||
| }else{ | |||
| lbl.setText("パスワードが違います。"); | |||
| } | |||
| } | |||
| } | |||
| @JPasswordField
pass = new JPasswordField(); インスタンスを生成しています。(エコー文字はデフォルトの*になります。) |
|||
| AString
str = new String(pass.getPassword()); パスワードフィールドから取得した文字の配列を文字列strにnewしています。 |
|||
| Bif(str.equals(passStr)){・・・} 文字列strと文字列passStrが同じか比べています。 |
|||
| もどる | |||