出现错误:组件中的变量私有访问权限 [英] Getting error: variable private acces in Component

查看:109
本文介绍了出现错误:组件中的变量私有访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被这个错误所困扰,名称在Component中具有私有访问权限".我不明白这是什么意思,但是我想我可能在main方法中错误地初始化了变量'name'.错误指向startGame()方法内部,在这里我初始化了"label1".

I'm stuck at this error, 'name has a private access in Component'. I don't understand what it means, but I think I may have wrongly initialized the variable 'name' in the main method. The error points inside the startGame() method, where I initialized 'label1'.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Gamey extends JFrame
{
private JPanel panelUp;
private JPanel panelDown;
private JButton btnPlay, btnNext;
private JLabel label1;

public Gamey()
{
    super("Game");
    startGame();
}

public void startGame()
{
    Container c = getContentPane();
    panelUp = new JPanel();
    panelDown = new JPanel();
    label1 = new JLabel(name + "Snow glows white on the mountain tonight");  //name has a private access in Component
    btnPlay = new JButton("Play");
    btnNext = new JButton("Next");

    btnPlay.addActionListener(new Handler());

    panelUp.add(label1);
    panelDown.add(btnPlay);

    c.add(panelUp, BorderLayout.CENTER);
    c.add(panelDown, BorderLayout.PAGE_END);


}

public class Handler implements ActionListener
{


    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == btnPlay)
        {
            btnPlay.setText("Next");
            label1.setText("Not a footprint to be seen");

        }

    }

}




public static void main(String[] args)
{
    String name = JOptionPane.showInputDialog(null, "enter name: ");
    Gamey game = new Gamey();

    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setSize(450,450);
    game.setVisible(true);
    game.setLocationRelativeTo(null);
}

}

推荐答案

您的类Gamey扩展了

Your class Gamey extend the JFrame class which in-turn extend the Component class. In the method startGame(), you have used a field called name in this statement.

label1 = new JLabel(name + "Snow glows white on the mountain tonight");

由于在您的Gamey类中没有使用该名称的实例变量,因此它在层次结构中进行了检查以查找此类字段,并在Component类中找到了该变量.但是该字段name具有private访问修饰符,这就是为什么出现错误

Since there is no instance variable by that name in your Gamey class, it has gone up the hierarchy to check for such a field and found one present in the Component class. But this field name has the private access modifier and that is why you're getting the error

name has a private access in Component.

要消除此错误,请根据需要在Gamey类或startGame()中声明一个name字段.

To get rid of this error, either declare a name field in your Gamey class or in the startGame() depending on your requirements.

注意:您的代码有些混乱,但是我可以看到您在main()方法中有一个变量name.您可以将其设置为实例变量,并在main()方法中填充其值,然后可以在startGame()方法中使用该值.像这样:

Note: Your code is bit jumbled but I could see that you're having a variable name in the main() method. You can make it an instance variable, and populate its value in the main() method which can then be used in the startGame() method. Something like this:

public class Gamey extends JFrame {
    // Other fields
    private String name;
    // Getter & setter for name

    ...

    public static void main(String[] args) {
        Gamey game = new Gamey();
        game.setName(JOptionPane.showInputDialog(null, "enter name: ")); // Set the name with the value from the input dialog
    ...
    }
}

这篇关于出现错误:组件中的变量私有访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆