Getting error: variable private acces in Component

允我心安 提交于 2019-12-25 06:28:23

问题


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);
}

}

回答1:


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");

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.

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

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
    ...
    }
}


来源:https://stackoverflow.com/questions/21777123/getting-error-variable-private-acces-in-component

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!