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

📄 textexp.java

📁 Java的swing的课堂练习。。 。
💻 JAVA
字号:
/**
 * @(#)TextExp.java
 *
 *
 * @author 
 * @version 1.00 2007/10/31
 */
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.*;
class MyDocument extends PlainDocument {
 int count=1;
 public void insertString(int offset,String s,AttributeSet a) {
  char c=s.charAt(0);
  if (c<='9' && c>='0' && count<=8){   
  try
  { super.insertString(offset,s,a);
    count++;
  }
  catch (BadLocationException e) {
   System.out.println("exception occured in insert!");
  }
  }
  System.out.println("insertString method called!"+"offset="+offset); 
 }
 public void remove(int offset,int length) {
  try
  { super.remove(offset,length);
    count--;
  }
  catch (BadLocationException e) {
   System.out.println("exception occured in remove!");
  }
   System.out.println("remove method called!"+"offset="+offset);
 }
}
class Win extends JFrame implements ActionListener {
  JTextField  text;
  MyDocument document;
  JPasswordField  inputPassword;
  JTextArea  display;
  public Win() {
   text=new JTextField(20);
   setLayout(new FlowLayout());
   document=new MyDocument();
   text.setDocument(document);
   add(text);
   GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
   String fontName[]=ge.getAvailableFontFamilyNames();
   Font f=new Font(fontName[0],Font.BOLD+Font.ITALIC,15);
   text.setFont(f);   
   text.setHorizontalAlignment(JTextField.LEFT);
   text.setBackground(Color.green);
   text.setForeground(Color.red);
   text.requestFocus();   
   inputPassword=new JPasswordField(10);
   inputPassword.setEchoChar('&');
   inputPassword.addActionListener(this);
   add(inputPassword);   
   display=new JTextArea(5,10);
   add(display);
   setBounds(100,100,300,200);
   setVisible(true);
   validate();
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public void actionPerformed(ActionEvent e) {
   char[] password=inputPassword.getPassword();
   String str=new String(password);
   display.setText("password is:"+str);   
  }  	
}

public class TextExp {
   public static void main(String[] args) {
        new Win();
    }
}

⌨️ 快捷键说明

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