再会!
我创建了两个类,即设置和游戏;在我的游戏中,首先访问设置类。
在我的设置类中,我从 Game 中调用 setter 方法.setDifficulty.
并为其分配一个值,例如 == 2。
public class Setting extends javax.swing.JDialog {
public Setting (JFrame owner) {
super(owner, true);
initComponents();
setSize(400, 250);
setLocation(370, 250);
getContentPane().setBackground(new Color(128, 201, 20));
}
private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {
dispose();
MainGame m2 = new MainGame(this);
m2.setDifficulty(jComboBox1.getSelectedIndex());
}
然后我访问我的第二个 CLass,即游戏。但是我无法在 setter 方法之外获得 hardLvl 的值。(请参阅我对代码的评论)
public class Game extends javax.swing.JDialog {
private int difficultLvl = 0;
public Game(JFrame owner) {
super(owner, true);
initComponents();
setSize(500, 500);
setLocation(300, 120);
getContentPane().setBackground(Color.getHSBColor(204, 204, 255));
System.out.println(difficultLvl); //SHOULD BE == 2, but == 0;
}
public void setDifficulty(int Difficulty) {
this.difficultLvl = Difficulty;
System.out.println(difficultLvl); == to 2 which is correct...
}
问题是我无法访问 setter 类之外的 hardLvl 值......它返回到它的默认分配值,在这种情况下为 0。我做错了什么?如何访问 setter 方法中的值。我用过this.difficultLvl
但没有结果。我是java新手...请帮忙!您的帮助将不胜感激。谢谢你。