我为游戏GUI提供了以下三个类:-

//this is the parent class.
import javax.swing.*;
import java.awt.*;
public class GameGui extends JFrame{
      public void decorateButton(JButton aBut,Color forg,Color back){
         Font afont = new Font(Font.SANS_SERIF,Font.PLAIN,18);
         aBut.setFont(afont);
         aBut.setBackground(back);
         aBut.setForeground(forg);
      }
      public void setFrameDefault(){
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setSize(400, 475);
         this.setLocationRelativeTo(null);
         this.setResizable(false);
     }
     public void setConstraints(int x,int y,int weightx,int weighty,GridBagConstraints gbc){
         gbc.weighty=weighty;
         gbc.weightx=weightx;
         gbc.gridx=x;
         gbc.gridy=y;
     }
}

//this class is for result to be shown for the game.

import javax.swing.*;
import java.awt.*;
class Result extends GameGui{
      JPanel mainPanel = new JPanel();
      JLabel backImage = new JLabel();//I want this variable to be shadowed by the subclass variable,but it is not happening.
      JButton continueGame = new JButton("continueGame");
      JButton exitGame = new JButton("exitGame");
      public Result(){
            this.setFrameDefault();
            backImage.setLayout(new BorderLayout());
            this.setContentPane(backImage);
            mainPanel.setLayout(new GridBagLayout());
            decorateButton(continueGame,Color.green,Color.white);
            decorateButton(exitGame,Color.green,Color.white);
            setGui();
      }
      public void setGui(){
            GridBagConstraints gbc = new GridBagConstraints();
            mainPanel.setOpaque(false);
            gbc.gridy=200;
            gbc.gridx=0;
            gbc.insets=new Insets(410,0,0,130);
            mainPanel.add(continueGame,gbc);
            gbc.gridx=GridBagConstraints.RELATIVE;
            gbc.insets = new Insets(410,0,0,0);
            mainPanel.add(exitGame,gbc);
            setFrameDefault();
            this.getContentPane().add(mainPanel);
      }

 }

 //this class is for showing the result for a Win.

 import javax.swing.*;
 import java.awt.*;
 public class Win extends Result{
        JLabel backImage = new JLabel(new ImageIcon("C:\\Users\\BSK\\Desktop\\win.png"));//Problem is here as i have declared the same named JLabel as in Result class but iam not getting the image as background.
        public static void main(String[] args) { //this main method is for testing.
            Win w = new Win();
            w.setVisible(true);
        }
 }


我在层次结构的末尾需要两个类,分别是WinDefeat(第二个类我还没有实现)。
所以我的问题是,尽管我在类backImageResult中都声明了与Win相同的名称JLabel,为什么我没有在后台获取图像?我通过将图像放在JLabel中进行了测试backImage类的Result然后起作用了!但是我想利用数据屏蔽的优势,因为在我的Defeat类(它也扩展了Result)中,我将命名JLabel的名称与backImage相同,但是希望它能为您提供不同的图像。
提前致谢。

注意请用您的图像进行测试。

最佳答案

阴影影响名称引用的变量。也就是说,由于子类Win定义了自己的backImage实例变量,因此引用WinbackImage方法将引用Win中的实例变量(并因此引用其值),而不是其中的一个。超类Result

阴影不能替换变量和其他对象指向的对象。也就是说,超类Result仍定义其自己的backImage实例变量,并且Result的方法仍引用该变量(并因此引用其值)。因此,Win#backImage会遮盖Result#backImage,但不会更改Result的工作方式。

还要注意,像JLabel backImage = ...这样的初始化行是作为类的构造函数的一部分运行的,而子类Win的构造函数是通过运行其超类Result构造函数开始的。因此,如果子类未声明另一个backImage且其构造函数将新值分配给继承的实例变量Result#backImage,则将在Result构造函数构建内容窗格之后发生,因此不会更改显示。

您可以更改backImage对象的内容:

public class Win extends Result {
  public Win() {
    super();
    backImage.setIcon(new ImageIcon("C:\\Users\\BSK\\Desktop\\win.png"));
  }

  ...


修改Win子类的backImage图标。

09-15 23:32