java不可访问怎么办_Java对象方法不可访问

在Java Authenticator类中,作者尝试在创建JPasswordField对象时直接设置echoChar属性,但发现无法在构造函数中实现。文章探讨了可能的原因,如范围限制,并解释了为何在后续调用setEchoChar方法可以正常工作。
摘要由CSDN通过智能技术生成

在下面的代码中,如果我调用password.setEchoCar(char)方法,则文件运行正常。为什么我不能在它上方创建对象时调用它?

有一个范围问题,我检查了方法的javadoc,它似乎是指定非默认密码字符的正确方法。

由于

import javax.swing.*;

public class Authenticator extends javax.swing.JFrame {

JTextField username = new JTextField(15);

JPasswordField password = new JPasswordField(15);

password.setEchoChar('%');

JTextArea comments = new JTextArea(4, 15);

JButton ok = new JButton("OK");

JButton cancel = new JButton("Cancel");

public Authenticator () {

super("Account Information");

setSize(300, 220);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pane = new JPanel();

JLabel usernameLabel= new JLabel("Username: ");

JLabel passwordLabel = new JLabel("Password: ");

JLabel commentsLabel = new JLabel("Comments: ");

comments.setLineWrap(true);

comments.setWrapStyleWord(true);

pane.add(usernameLabel);

pane.add(username);

pane.add(passwordLabel);

pane.add(password);

pane.add(commentsLabel);

pane.add(comments);

pane.add(ok);

pane.add(cancel);

add(pane);

setVisible(true);

}

private static void setLookAndFeel() {

try {

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

} catch (Exception exc) {

System.out.println(exc.getMessage());

}

}

public static void main(String[] arguments) {

Authenticator.setLookAndFeel();

Authenticator auth = new Authenticator();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值