为什么数据使用实例变量进行阴影操作在我的程序中不起作用?
对于游戏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
(第二个我还没有实现)。我需要这个,因为我想要赢取帧,而Defeat帧只在图像上有所不同。
所以我的问题是虽然我在Result
和Win
类中都声明了相同的命名JLabel作为backImage
,为什么我没有在后台获取图像?我已经通过将图像放在Result
类的JLabel
backImage
来测试它然后它的工作原理!但我想利用数据阴影的优势,因为在我的Defeat
类(也扩展了Result)中,我将JLabel
命名为backImage
但设置了不同的图像。我希望你能理解,那么出路是什么? ?
提前致谢。
注意请测试您的图像。
阴影会影响名称所指的变量。 也就是说,由于子类Win
定义了自己的backImage
实例变量,因此引用backImage
的Win
方法将引用Win
的实例变量(因此它的值)而不是超类Result
的实例变量。
阴影不会替换变量和其他对象指向的对象。 也就是说,超类Result
仍然定义了自己的backImage
实例变量,而Result
的方法仍然引用该变量(以及它的值)。 所以Win#backImage
shadows Result#backImage
但它不会改变Result的工作方式。
还要注意初始化行像JLabel backImage = ...
作为类的构造函数的一部分运行,子类Win
的构造函数首先运行其超类Result
构造函数。 因此,如果子类没有声明另一个backImage
并且其构造函数为inheritance的实例变量Result#backImage
分配了一个新值,那么在Result
构造函数构建内容窗格之后会发生这种情况,因此它不会更改显示。
您可以更改backImage
对象的内容:
public class Win extends Result { public Win() { super(); backImage.setIcon(new ImageIcon("C:\\Users\\BSK\\Desktop\\win.png")); } ...
修改Win
子类的backImage图标。
Java中的字段不会被覆盖为方法,因此这对您不起作用。 幸运的是,有很多解决方案。
其中一个(我绝对不会说最好的!)是使用抽象的getIcon()
方法或者其他东西使你的Result
游戏抽象化并从你的setGui()
方法调用它,然后你的Win / Lose类将返回不同的那里的图标。