bagBean 2019-04-16 11:23 采纳率: 66.7%
浏览 281
已采纳

萌新想问问为什么会报错呢?怎么访问构造函数里的name和pass啊?

为什么会报错啊,是不能访问到构造函数里的Name和Pass吗?为什么两句代码报错的位置还不同呢?

public class Test01 extends JFrame implements ActionListener {

    public Test01(){
    //1.设置标题
    this.setTitle("登陆");        
    //2.设置在屏幕上的位置
    GraphicsEnvironment a=GraphicsEnvironment.getLocalGraphicsEnvironment();
    Rectangle rec=a.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
    double x=(rec.getWidth()-260)/2;
    double y=(rec.getHeight()-220)/2;
    this.setLocation((int)x, (int)y);
    //3.设置大小
    this.setSize(260, 220);
    //4.设置logo
    Image image=Toolkit.getDefaultToolkit().createImage("D:/图片/杂/logo.jpg");
    this.setIconImage(image);
    //5.正常退出
    this.setDefaultCloseOperation(Test01.EXIT_ON_CLOSE);
    //6.关闭最大化
    this.setResizable(false);
    //7.布局管理
    //框架默认布局 管理器 边界布局
    //添加面板 透明的容器 不能单独存在 默认布局 流式 FlowLayout
    JPanel jplMain=new JPanel();  //主面板
    this.getContentPane().add(jplMain); //用自己的面板替换框架的内容面板
    JPanel jplTop=new JPanel(); //上面版
    JPanel jplMid=new JPanel(); //中面板
    JPanel jplBot=new JPanel(); //下面版
    //将面板添加到容器中
    jplMain.add(jplTop);
    jplMain.add(jplMid);
    jplMain.add(jplBot);
    //修改面板的布局管理器为网格且3行1列
    jplMain.setLayout(new GridLayout(3,1));
    //接下来设置各个面板
    //上面版
    JLabel lblTop=new JLabel(new ImageIcon("D:/图片/杂/main.jpg"));
    jplTop.add(lblTop);
    //中面板
    JLabel lblName=new JLabel("用户名:");
    JTextField Name=new JTextField(16);
    JLabel lblPass=new JLabel("密    码:");       
    JPasswordField Pass=new JPasswordField(16);
    Name.setForeground(Color.red);
    jplMid.add(lblName);
    jplMid.add(Name);
    jplMid.add(lblPass);
    jplMid.add(Pass);
    //下面版
    JLabel lblSelect=new JLabel("请选择服务器:    ");
    JComboBox Select=new JComboBox(
            new String[]{"1","2","3","4"}
            );
    JButton Login=new JButton("登陆(L)");
    JButton Exit=new JButton("退出(X)");
    //设置热键
    Login.setMnemonic('L');
    Exit.setMnemonic('X');
    //设置提示
    Login.setToolTipText("点击登录");
    Exit.setToolTipText("点击退出");
    //设置监听器
    Login.addActionListener(this);

    jplBot.add(lblSelect);
    jplBot.add(Select);
    jplBot.add(Login);
    jplBot.add(Exit);

}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String name=Name.getText();
    String pass=Pass.getText();
    if(name.equals("admin")&&pass.equals("666")) {
        JOptionPane.showMessageDialog(null, "登陆成功!");
    }
    else {
        JOptionPane.showMessageDialog(null, "登陆失败!");
    }

}


public static void main(String[] args) {
    new Test01().setVisible(true);
}

}

在这两个地方报错图片说明
错误原因:1.The method getText() is undefined for the type Attributes.Name
2.Pass cannot be resolved

拜托拜托!想了好久!

  • 写回答

2条回答 默认 最新

  • 毕小宝 博客专家认证 2019-04-16 15:21
    关注

    把构造函数中定义的局部变量都变成成员变量,像这样:

    public class Test01 extends JFrame implements ActionListener {
    
        JPanel jplMain;  //主面板
        JPanel jplTop; //上面版
        JPanel jplMid; //中面板
        JPanel jplBot; //下面版
    
        JLabel lblName;
        JTextField Name;
        JLabel lblPass;       
        JPasswordField Pass;
    
        public Test01(){
        jplMain=new JPanel();  //主面板
        jplTop=new JPanel(); //上面版
        jplMid=new JPanel(); //中面板
        jplBot=new JPanel(); //下面版
        this.getContentPane().add(jplMain); //用自己的面板替换框架的内容面板
    
        lblName=new JLabel("用户名:");
        Name=new JTextField(16);
        lblPass=new JLabel("密    码:");   
        Pass=new JPasswordField(16);
        。。。。。
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器