?? textexp.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();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -