import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class JTextFieldDemo2 {
JFrame frame=new JFrame("JTextField Demo 2");
JLabel nameLabel=new JLabel("User Name");
JLabel pwLabel=new JLabel("Password");
JTextField nameField=new JTextField();
JPasswordField pwField=new JPasswordField();
JTextArea ta=new JTextArea(5,20);

public static void main(String args[]) {
JTextFieldDemo2  tfd2=new JTextFieldDemo2();
tfd2.go();
}
public void go() {
UpperCaseDocument ucDocument=new UpperCaseDocument();
nameField.setDocument(ucDocument);
nameField.setForeground(Color.GREEN);

nameField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String username=nameField.getText();
ta.append("\nUser Name:" +username);
}
});

pwField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
char pw[]=pwField.getPassword();
String password=new String(pw);
ta.append("\nPassword:" +password);
}
});

JPanel labelPanel=new JPanel();
labelPanel.setLayout(new GridLayout(2,1));
labelPanel.add(nameLabel);
labelPanel.add(pwLabel);

JPanel fieldPanel=new JPanel();
fieldPanel.setLayout(new GridLayout(2,1));
fieldPanel.add(nameField);
fieldPanel.add(pwField);

JPanel northPanel=new JPanel();
northPanel.setLayout(new GridLayout(1,2));
northPanel.add(labelPanel);
northPanel.add(fieldPanel);

JScrollPane jsp=new JScrollPane(ta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                                   
   Container cp=frame.getContentPane();
   cp.add(northPanel,BorderLayout.NORTH);
   cp.add(jsp,BorderLayout.CENTER);
   
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.pack() ;
   frame.setVisible(true);
}
}
class UpperCaseDocument extends PlainDocument {
public void insertString(int offset,
String string,AttributeSet attributeSet)
        throws BadLocationException{
    string=string.toUpperCase();
    super.insertString(offset, string, attributeSet);
}
    }

解决方案 »

  1.   

    [/color]class UpperCaseDocument extends PlainDocument {
            public void insertString(int offset,
                    String string,AttributeSet attributeSet)
                    throws BadLocationException{
                    string=string.toUpperCase();
                    super.insertString(offset, string, attributeSet);
     }[color=#FF6600]这个额?//////////////////////////public void insertString(int offs,
                             String str,
                             AttributeSet a)
                      throws BadLocationException向文档中插入某些内容。插入内容会导致在实际发生改变时存储写锁定,接着会向线程上抓取该写入锁定的观察者发出通知。 
    虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 Threads and Swing。 
    指定者:
    接口 Document 中的 insertString
    覆盖:
    类 AbstractDocument 中的 insertString
    参数:
    offs - 起始偏移量,该值 >= 0
    str - 要插入的字符串;null/空字符串不执行任何操作
    a - 插入内容的属性 
    抛出: 
    BadLocationException - 如果给定的插入位置不是文档中的有效位置/////////////////////////public String toUpperCase()使用默认语言环境的规则将此 String 中的所有字符都转换为大写。此方法等效于toUpperCase(Locale.getDefault())。 
    返回:
    要转换为大写的 String。