把setEchoChar()放在构造函数外,会报错“需要<标识符>”。如果放在构造函数内,则不会报错,请问这是什么原因?
public class Form extends javax.swing.JFrame {
JPasswordField password = new JPasswordField(15);
//password.setEchoChar('&'); public Form() {
super("测试文本输入");
setBounds(300,300,300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
password.setEchoChar('&'); JPanel pane = new JPanel();
JLabel passwordLabel = new JLabel("Password:");
pane.add(passwordLabel);
pane.add(password);
setContentPane(pane);
} public static void main (String[] arguments) {
Form frm = new Form();
frm.setVisible(true);
}
}
public class Form extends javax.swing.JFrame {
JPasswordField password = new JPasswordField(15);
//password.setEchoChar('&'); public Form() {
super("测试文本输入");
setBounds(300,300,300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
password.setEchoChar('&'); JPanel pane = new JPanel();
JLabel passwordLabel = new JLabel("Password:");
pane.add(passwordLabel);
pane.add(password);
setContentPane(pane);
} public static void main (String[] arguments) {
Form frm = new Form();
frm.setVisible(true);
}
}
//password.setEchoChar('&');····肯定会报错,编译都不能过。在方法外运行语句只有一种可能:static代码块中的语句