良い一日!
設定とゲームの2つのクラスを作成しました。私のゲームでは、最初に設定クラスにアクセスします。
私の設定クラスでは、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());
}
次に、ゲームである2番目のCLassにアクセスします。しかし、setterメソッドの外でdifferfulLvlの値を取得することはできません。(コードに関する私のコメントを参照してください)
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クラスの外部でdifferfulLvl値にアクセスできないことです...デフォルトの割り当て値(この場合は0)に戻ります。何が間違っているのでしょうか。setterメソッド内の値にアクセスするにはどうすればよいですか。使用this.difficultLvl
しましたが、結果がありません。私はJavaの初心者です...助けてください!あなたの助けをいただければ幸いです。ありがとうございました。