java报错,帮忙找一下吧。

importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjavax.swing.text.*;... import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import javax.swing.text.*;

public class JTextFieldDemo2 {
JFrame frame = new JFrame("JTextField");
JLabel nameLabel = new JLabel("UserName");
JLabel pwLabel = new JLabel("PassWord");
JTextFieldDemo2 namefield = new JTextFieldDemo2();
JPasswordField pwfield = new JPasswordField();
JTextArea ta = new JTextArea(5,20);

public static void main(String[] s) {
JTextFieldDemo2 tf1 = new JTextFieldDemo2();
tf1.go();
}
public void go(){
UpperCaseDocument ucDocument = new UpperCaseDocument();
namefield.setDocument(ucDocument); //setDocument 报错 The method setDocument(JTextField.UpperCaseDocument)
// is undefined for the type JTextField
namefield.setForeground(Color.red); //setForeground 报错The method setForeground(Color)
//is undefined for the type JTextField

namefield.addActionListener(new ActionListener() { //The method addActionListener(new ActionListener(){})
//is undefined for the type JTextField
public void actionPerformed(ActionEvent e){
String username = namefield.getText(); //The method getText() is undefined for the type JTextField
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 p1 = new JPanel();
p1.setLayout(new GridLayout(2,1));
p1.add(nameLabel);
p1.add(pwLabel);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2,1));
p2.add(namefield); //The method add(Component) in the type Container
//is not applicable for the arguments (JTextField)
p2.add(pwfield);

JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1,2));
p3.add(p1);
p3.add(p2);

JScrollPane jsp = new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

Container cp = frame.getContentPane();
cp.add(p3,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);
}
}
}
展开
 我来答
若以下回答无法解决问题,邀请你更新回答
gfy8852163
2011-09-05 · TA获得超过683个赞
知道答主
回答量:137
采纳率:0%
帮助的人:112万
展开全部
123
追问
刚才突然一下就找到错误了,虽然你是来打酱油的,但看在你是第一个来答的面子上就给你吧,天上会掉馅饼的。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式