
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);
}
}
} 展开
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);
}
}
} 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询