2017-11-08 4 views
1

私は現在非常に単純なJava GUIアプリケーションを作成していますが、変数が更新できないという問題にぶつかっています。アプリケーションは単純なバスケットボールのスコアキーパーであり、スコアの整数は更新されず、ラベルのテキストもそれらを表示しません。何もエラーがないので、何も更新されていない理由は分かりません。コード:GUIアプリケーションの変数が更新されない

ScoreWindow.java

import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.SpringLayout; 
import javax.swing.JLabel; 
import java.awt.Font; 
import java.awt.FlowLayout; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.SwingConstants; 
import javax.swing.SwingUtilities; 

public class ScoreWindow implements ScoreListener { 

    private JFrame frmScorewindow; 

    public volatile JLabel homeScoreLabel; 

    public JLabel awayScoreLabel; 

    public volatile int homeScore, awayScore; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ScoreWindow window = new ScoreWindow(); 
        window.frmScorewindow.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public ScoreWindow() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 

     // Init Scores 
     homeScore = 0; 
     awayScore = 0; 

     frmScorewindow = new JFrame(); 
     frmScorewindow.setResizable(false); 
     frmScorewindow.setTitle("Score Keeper"); 
     frmScorewindow.setBounds(100, 100, 551, 348); 
     frmScorewindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frmScorewindow.getContentPane().setLayout(null); 

     JButton homeScore2 = new JButton("+2"); 
     homeScore2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       ScoreListener listener = new ScoreWindow(); 
       listener.homeScore(2); 
      } 
     }); 
     homeScore2.setBounds(110, 129, 117, 29); 
     frmScorewindow.getContentPane().add(homeScore2); 

     JButton homeScore3 = new JButton("+3"); 
     homeScore3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       ScoreListener listener = new ScoreWindow(); 
       listener.homeScore(3); 
      } 
     }); 
     homeScore3.setBounds(110, 156, 117, 29); 
     frmScorewindow.getContentPane().add(homeScore3); 

     JButton awayScore2 = new JButton("+2"); 
     awayScore2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       ScoreListener listener = new ScoreWindow(); 
       listener.awayScore(2); 
      } 
     }); 
     awayScore2.setBounds(332, 129, 117, 29); 
     frmScorewindow.getContentPane().add(awayScore2); 

     JButton awayScore3 = new JButton("+3"); 
     awayScore3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       ScoreListener listener = new ScoreWindow(); 
       listener.awayScore(3); 
      } 
     }); 
     awayScore3.setBounds(332, 156, 117, 29); 
     frmScorewindow.getContentPane().add(awayScore3); 

     JButton resetButton = new JButton("Reset"); 
     resetButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       ScoreListener listener = new ScoreWindow(); 
       listener.reset(); 
      } 
     }); 
     resetButton.setBounds(225, 220, 117, 29); 
     frmScorewindow.getContentPane().add(resetButton); 

     homeScoreLabel = new JLabel("000"); 
     homeScoreLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24)); 
     homeScoreLabel.setHorizontalAlignment(SwingConstants.CENTER); 
     homeScoreLabel.setBounds(138, 88, 61, 29); 
     frmScorewindow.getContentPane().add(homeScoreLabel); 

     awayScoreLabel = new JLabel("000"); 
     awayScoreLabel.setHorizontalAlignment(SwingConstants.CENTER); 
     awayScoreLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24)); 
     awayScoreLabel.setBounds(361, 88, 61, 29); 
     frmScorewindow.getContentPane().add(awayScoreLabel); 

     JLabel lblHome = new JLabel("Home"); 
     lblHome.setHorizontalAlignment(SwingConstants.CENTER); 
     lblHome.setBounds(138, 60, 61, 16); 
     frmScorewindow.getContentPane().add(lblHome); 

     JLabel lblAway = new JLabel("Away"); 
     lblAway.setHorizontalAlignment(SwingConstants.CENTER); 
     lblAway.setBounds(361, 60, 61, 16); 
     frmScorewindow.getContentPane().add(lblAway); 

     JLabel title = new JLabel("Score Keeper App"); 
     title.setHorizontalAlignment(SwingConstants.CENTER); 
     title.setBounds(180, 33, 200, 16); 
     frmScorewindow.getContentPane().add(title); 
    } 

    @Override 
    public void reset() { 
     print("reset();"); 
     homeScore = 0; 
     awayScore = 0; 
     awayScoreLabel.setText("" + awayScore); 
     homeScoreLabel.setText("" + homeScore); 
    } 

    @Override 
    public void awayScore(int n) { 
     print("awayScore();"); 
     awayScore+=n; 
     awayScoreLabel.setText("" + awayScore); 
    } 

    @Override 
    public void homeScore(int n) { 
      print("homeScore();"); 
      print(homeScoreLabel.getText()); 
      homeScore = homeScore + n; 
      homeScoreLabel.setText("" + homeScore); 
      homeScoreLabel.repaint(); 
      homeScoreLabel.revalidate(); 
    } 

    static void print(Object o) { 
     System.out.println(o); 
    } 
} 

ScoreListener.java

public interface ScoreListener { 
     public void reset(); 
     public void awayScore(int n); 
     public void homeScore(int n); 
} 

をありがとう!

+2

あなたは間違ったScoreWindowインスタンスでメソッドを呼び出しています。リスナー内に新しいScoreWindowインスタンスを作成していますが、これらのインスタンスは表示されていないため、表示が変更されることはありません。それがここの鍵であるので、あなたが使用している参照を注意してください。 –

+1

あなたがこれをやっていないことを確認するためには、あなたがそれをやっているように15回ではなく、プログラム全体で 'new ScoreWindow()'を1回だけ見てください。 –

答えて

2
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       ScoreWindow window = new ScoreWindow(); 
       window.frmScorewindow.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

上記のコードを使用してウィンドウを作成します。

JButton homeScore2 = new JButton("+2"); 
    homeScore2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      ScoreListener listener = new ScoreWindow(); 
      listener.homeScore(2); 
     } 
    }); 

ただし、ウィンドウの2番目のインスタンスを作成します。

これを行わないでください。クラスのインスタンスを一度作成するだけで済みます。このインスタンスを参照する他のすべてのコード。

あなたのActionListnerクラスは、 "homeScore()"メソッドを直接参照できるScoreWindowクラスで定義されています。

関連する問題