问题描述
对于游戏GUI,我有以下三个类:-
I have three classes which are shown below ,for a game 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);
}
}
我在层次结构的末尾需要两个类,分别是Win
和Defeat
(我尚未实现的第二个类).我需要这个类是因为我希望获胜帧和失败帧仅在图像上有所不同.所以我的问题是,尽管我在类Result
和Win
中都声明了与backImage
相同的名称JLabel,但为什么我没有在后台获取图像?我已经通过将图像放入JLabel
进行了测试. Result
类的backImage
然后可以工作!但是我想利用数据屏蔽的优势,因为在我的Defeat
类(它也扩展了Result)中,我将命名JLabel
具有与backImage
相同的名称,但设置了不同的图像.希望您能理解,那么出路是什么?
提前致谢.
I need two classes at end of hierarchy which are Win
and Defeat
(second one i have not implemented).I need this because i want wining frame and Defeat frame only differ in the image.
So my question is that although i have declared same named JLabel as backImage
in both the classes Result
and Win
,why i am not getting the image at background?I have tested it by putting the image in JLabel
backImage
of Result
class and then it works!But i want to take the advantage of data shadowing because in my Defeat
class(which also extends Result)i will name JLabel
having same name as backImage
but with different image set to it.I hope you understand,So what is the way out?
Thanks in advance.
注意,请使用您的图片进行测试.
NOTE please test with your image.
推荐答案
阴影影响名称引用的变量.也就是说,由于子类定义了自己的backImage
实例变量,因此引用backImage
的Win
方法将引用Win
中的实例变量(并因此引用其值),而不是其中的一个.超类Result
.
Shadowing affects which variable a name refers to. That is, since the subclass Win
defines its own backImage
instance variable, methods of Win
that refer to backImage
will refer to the instance variable in Win
(and thus its value) rather than the one in the superclass, Result
.
Shadowing不会替换变量和其他对象指向的对象.也就是说,超类Result
仍然定义了自己的backImage
实例变量,并且Result
的方法仍然引用该变量(因此也引用了它的值).因此,Win#backImage
会遮盖Result#backImage
,但不会改变结果的工作方式.
Shadowing does not replace an object that variables and other objects point to. That is, the superclass Result
still defines its own backImage
instance variable, and Result
's methods still refer to that variable (and thus its value). So Win#backImage
shadows Result#backImage
but it doesn't change how Result works.
还要注意,像JLabel backImage = ...
这样的初始化行作为类的构造函数的一部分运行,而子类的构造函数从运行其超类Result
构造函数开始.因此,如果子类未声明另一个backImage
并且其构造函数将新值分配给继承的实例变量Result#backImage
,则将在Result
构造函数构建内容窗格之后发生,因此不会更改显示
Also note that the initialization lines like JLabel backImage = ...
run as part of a class's constructor, and the subclass Win
's constructor begins by running its superclass Result
constructor. So if the subclass didn't declare another backImage
and its constructor assigned a new value to the inherited instance variable Result#backImage
, this would happen after the Result
constructor built the content pane, so it wouldn't change the display.
您可以更改backImage
对象的内容:
You could change the contents of the backImage
object:
public class Win extends Result {
public Win() {
super();
backImage.setIcon(new ImageIcon("C:\\Users\\BSK\\Desktop\\win.png"));
}
...
修改Win
子类的backImage图标.
to modify backImage's icon for the Win
subclass.
这篇关于为什么带有实例变量的数据镜像在我的程序中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!