要实现在文本框里实现输入小写,输出大写
import java.awt.*;
import java.awt.Event;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Key implements KeyListener {
public static void main(String[] args)
{
JFrame f=new JFrame("example");
TextArea t=new TextArea();
t.addKeyListener(new Key());
f.add(t);
f.setVisible(true);
}
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
TextArea ta=(TextArea)arg0.getSource();
String s=ta.getText();
String b=s.toUpperCase();
ta.setText("" );
ta.append(b);
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
报错信息:
java.lang.Error: Do not use javax.swing.JFrame.add() use javax.swing.JFrame.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Key.main(Key.java:18)
Exception in thread "main"