java 设置回显字符出错
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassff{publicstati...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ff{
public static void main(String[]args){
new ee();
}}
class ee extends JFrame{
ee(){
JTextField qq=new JTextField();
add(qq);
monter bl=new monter();
qq.addActionListener(bl);
qq.setEchoChar('*');
setSize(100,100);
setVisible(true);
pack();
setDefaultCloseOperation(ee.EXIT_ON_CLOSE);
}}
class monter implements ActionListener{
public void actionPerformed(ActionEvent e){
JTextField qq=(JTextField)e.getSource();
System.out.print(qq.getText());
qq.setText(" ");
}
} 展开
import java.awt.*;
import java.awt.event.*;
public class ff{
public static void main(String[]args){
new ee();
}}
class ee extends JFrame{
ee(){
JTextField qq=new JTextField();
add(qq);
monter bl=new monter();
qq.addActionListener(bl);
qq.setEchoChar('*');
setSize(100,100);
setVisible(true);
pack();
setDefaultCloseOperation(ee.EXIT_ON_CLOSE);
}}
class monter implements ActionListener{
public void actionPerformed(ActionEvent e){
JTextField qq=(JTextField)e.getSource();
System.out.print(qq.getText());
qq.setText(" ");
}
} 展开
2个回答
展开全部
你的程序主要问题是JTextField没有setEchoChar('*');方法,JPasswordField和TextField才有。
所以我把你的JTextField都改成了JPasswordField,你看看吧。
完整的程序如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ff{
public static void main(String[]args){
new ee();
}}
class ee extends JFrame{
ee(){
JPasswordField qq=new JPasswordField(); //这里JextField改成了JPasswordField
add(qq);
monter bl=new monter();
qq.addActionListener(bl);
qq.setEchoChar('*');
setSize(100,100);
setVisible(true);
pack();
setDefaultCloseOperation(ee.EXIT_ON_CLOSE);
}
}
class monter implements ActionListener{
public void actionPerformed(ActionEvent e){
JPasswordField qq=(JPasswordField)e.getSource(); //这里JTextField改成了JPasswordField
System.out.print(qq.getPassword()); //这里qq.getText()改成了qq.getPassword()
qq.setText("");//这里qq.setText(" ");改成了qq.setText("");去掉了空格
}
}
所以我把你的JTextField都改成了JPasswordField,你看看吧。
完整的程序如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ff{
public static void main(String[]args){
new ee();
}}
class ee extends JFrame{
ee(){
JPasswordField qq=new JPasswordField(); //这里JextField改成了JPasswordField
add(qq);
monter bl=new monter();
qq.addActionListener(bl);
qq.setEchoChar('*');
setSize(100,100);
setVisible(true);
pack();
setDefaultCloseOperation(ee.EXIT_ON_CLOSE);
}
}
class monter implements ActionListener{
public void actionPerformed(ActionEvent e){
JPasswordField qq=(JPasswordField)e.getSource(); //这里JTextField改成了JPasswordField
System.out.print(qq.getPassword()); //这里qq.getText()改成了qq.getPassword()
qq.setText("");//这里qq.setText(" ");改成了qq.setText("");去掉了空格
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询