⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textapplet.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * TextApplet.java  E.L. 2001-08-20
 *
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class TextApplet extends JApplet {

  private static final String defaultText =
    "You are allowed to enter text here, if you input the correct password.";
  private JTextField usernameField = new JTextField(15);
  private JPasswordField passwordField = new JPasswordField(15);
  private JTextArea textArea = new JTextArea(10, 20);
  private JButton printButton = new JButton("Print");

  public void init() {
    Container guiContainer = getContentPane();
    guiContainer.setLayout(new BorderLayout(5, 5)); // gaps between the panels

    guiContainer.add(new LoginPanel(), BorderLayout.NORTH);
    guiContainer.add(new TextAreaPanel(), BorderLayout.CENTER);
    guiContainer.add(new ButtonPanel(), BorderLayout.SOUTH);

    usernameField.requestFocus();
  }

  /* Describes northern panel */
  private class LoginPanel extends JPanel {
    public LoginPanel() {
      setLayout(new GridLayout(2, 2, 5, 5));
      add(new JLabel("Username:", JLabel.RIGHT));
      add(usernameField);
      add(new JLabel("Password:", JLabel.RIGHT));
      add(passwordField);
      PasswordListener thePasswordListener = new PasswordListener();
      passwordField.addFocusListener(thePasswordListener);
    }
  }

  /* Describes middle panel */
  private class TextAreaPanel extends JPanel {
    public TextAreaPanel() {
      textArea.setLineWrap(true);
      textArea.setWrapStyleWord(true);
      textArea.setEditable(false);
      textArea.setText(defaultText);
      JScrollPane scrollPane = new JScrollPane(textArea);
      add(scrollPane);
    }
  }

  /* Describes southern panel */
  private class ButtonPanel extends JPanel {
    public ButtonPanel() {
      ButtonListener theButtonListener = new ButtonListener();
      printButton.addActionListener(theButtonListener);
      printButton.setEnabled(false);
      printButton.setMnemonic('P');
      add(printButton);
    }
  }

  /*
   * The objects of this class listen for changes in focus.
   *
   * The program reacts after the user has left the password field.
   * Username and password are checked. If ok, focus is moved to the text area,
   * and the user is allowed to edit the text. If not ok, focus is moved
   * to the username field, and the user may re-enter username and password.
   */
  private class PasswordListener implements FocusListener {
    public void focusLost(FocusEvent event) {
      String user = usernameField.getText();
      char[] password = passwordField.getPassword();
      if (okPassword(user, password)) {
        showStatus("Ok. You may edit the text.");
        printButton.setEnabled(true);
        textArea.setEditable(true);
        textArea.requestFocus();
      } else {
        showStatus("Username/password not valid.");
        printButton.setEnabled(false);
        textArea.setEditable(false);
        usernameField.requestFocus();
      }
    }

    public void focusGained(FocusEvent event) {
    }

    private boolean okPassword(String username, char[] password) {
      /*
       * Here the password is shown. 
       * This is of course not safe enough, but ok for this example program.
       */
      char[] correctPassword = {'t', 'e', 's', 't'};
      if (username.equals("test") && Arrays.equals(password, correctPassword)) {
        Arrays.fill(password, ' ');  // clears the password entered by user
        return true;
      }
      else return false;
    }
  }

  /* Describes objects which listen for button pushes */
  private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
      String text = textArea.getText();
      System.out.println("Here is the text: ");
      System.out.println(text);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -