2013-03-08 20 views
7

私のボタンをクリックすると、ボタンが「開始」から「停止」に変わるように変更しようとしています。これを行う私の試みは以下の通りです。私はそれを見て、ガイドをコピーしようとしましたが、私が間違っていることはわかりません。私はいくつかの "}"が足りないかもしれません。なぜなら、私は無関係なコードをたくさん残していたからです。誰かが私が間違っていることを見ることができますか?クリック後にボタンの名前を変更する - Java JButton

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 

public class PipeGameApp extends JFrame implements ActionListener { 

    private static int BOARD_SIZE = 11; 
    private PipeGame game;  // The model 
    private PipeGameView view;  // The view 

    // This constructor builds the window 
    public PipeGameApp(String title) { 
     super(title); 

     game = new PipeGame(BOARD_SIZE); 
     view = new PipeGameView(game); 

     //THE TOP BAR 
     JPanel topBar = new JPanel(); 
     JButton startButton = new JButton("Start"); 
     startButton.addActionListener(this); 



     ButtonGroup bg1 = new ButtonGroup(); 
     JRadioButton rb1 = new JRadioButton("2 minutes", true); 
     rb1.addActionListener(this); 


     JRadioButton rb2 = new JRadioButton("10 minutes", false); 
     JRadioButton rb3 = new JRadioButton("No Time Limit", false); 
     bg1.add(rb1); 
     bg1.add(rb2); 
     bg1.add(rb3); 





     topBar.add(startButton); 
     topBar.add(rb1); 
     topBar.add(rb2); 
     topBar.add(rb3); 
     //END OF TOP BAR 

     //THE BOTTOM BAR 
     JPanel bottomBar = new JPanel(); 
     JLabel timeLeft = new JLabel("Time Left: "); 
     JProgressBar bar = new JProgressBar(); 
     bottomBar.add(timeLeft); 
     bottomBar.add(bar); 
     bottomBar.setVisible(false); 
     //end of bottom 

     /* 
     //bottom 2 
     int fscore=10; 
     JPanel bottomBar2 = new JPanel(); 
     JLabel score = new JLabel("Final score:" + fscore); 
     bottomBar2.add(score); 
     bottomBar2.setVisible(false); 
     */ 


     getContentPane().add(view); //CHANGE LOCATION OF BOARD GAME HERE BorderLayout.SOUTH 

     getContentPane().add(topBar, BorderLayout.NORTH); 
     getContentPane().add(bottomBar, BorderLayout.SOUTH); 

     //getContentPane().add(bottomBar2, BorderLayout.SOUTH); 



     // Add the listeners to the view's buttons 
     for (int r = 0; r < BOARD_SIZE; r++) { 
      for (int c = 0; c < BOARD_SIZE; c++) { 
       view.getButton(r, c).addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         handleTileSelection(e); 
        } 
       }); 
      } 
     } 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     //setSize(446,466); 
     setSize(446, 530); 
     setResizable(false); 
    } 

    // Handle a Tile Selection. Just change the model and update the view. 
    private void handleTileSelection(ActionEvent e) { 
     // Find the row and column of the pressed button, then make the change 
     int r = 0, c = 0; 
     for (int i = 0; i < BOARD_SIZE; i++) { 
      for (int j = 0; j < BOARD_SIZE; j++) { 
       if (e.getSource() == view.getButton(i, j)) { 
        if (game.placePipe(i, j)) { 
         view.update(); 
        } 
        return; 
       } 
      } 
     } 
    } 

    // This is where it all begins 
    public static void main(String[] args) { 
     new PipeGameApp("The Frantic Pipe Layer").setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent ae) { 
     startButton.setText("asdf"); 
    } 
} 
+0

私の答えに編集を参照してください。あなたは可変シャドウイングの問題ではなく可変スコープです。 startButtonへのクラス参照がないので、コードがコンパイルされるのには驚いています。それとも、実際にコンパイルしていないのですか? –

答えて

5

この問題は、可変範囲が限られています。 startButton変数はコンストラクタで宣言されているため、コンストラクタでのみ表示されます。あなたはそれをクラス内で宣言する必要があります。コンストラクタ内でそれを再宣言しないで、クラスの残りの部分が変数を参照して使用できるようにします。

すなわち、この変更:これに

public class PipeGameApp extends JFrame implements ActionListener { 

    private static int BOARD_SIZE = 11; 
    private PipeGame game;  // The model 
    private PipeGameView view;  // The view 

    public PipeGameApp(String title) { 

     JButton startButton = new JButton("Start"); 
     startButton.addActionListener(this); 
     // etc... 

public class PipeGameApp extends JFrame implements ActionListener { 

    private static int BOARD_SIZE = 11; 
    private PipeGame game;  // The model 
    private PipeGameView view;  // The view 
    private JButton startButton; // *** note change *** 

    public PipeGameApp(String title) { 

     startButton = new JButton("Start"); // *** note change *** 
     startButton.addActionListener(this); 
     // etc... 

代わり:

  • JToggleButton
  • を使用するかのActionEventのgetSource()メソッドによって返されるオブジェクトを使用します、それに基づいて新しい状態を設定するts 現在状態です。たとえば、

@Override 
public void actionPerformed(ActionEvent ae) { 
    Object source = ae.getSource(); 
    if (source instanceof JButton) { 
     if (ae.getText().equals("Start")) { 
      ae.setText("Stop"); 
      // do other stuff 
     } else if (ae.getText().equals("Stop")) { 
      ae.setText("Start"); 
      // do more stuff 
     } 
    } 
} 

"I might be missing some "}" because I left out a lot of the code that's irrelevant."に関してはこれが起こらないように努力をしてください。 「}」の行方不明は間違ってはいけません。コードを理解して助けてください。彼らが自由な時間にあなたを助ける努力をしてくれるように他の人に求めているのであれば、あなたにあまりにも多くのジャンクコードを投稿しないように求めているわけではありません。

+0

本当にありがとう、私は自分のすべてのコードを投稿することに留意します。私はスイッチを切り替えるためにボタンが必要なので、getSourceメソッドを使用すると思います。 – user1692517

+0

@ user1692517:うれしいことを解決しました。 "すべてのコード"が多くの場合、あまりにも多くのことを理解してください。あなたが最初にやったような迷惑コードを投稿しないことが最も重要です。その欠けているブレースは無関係ではありませんが、実際には非常に重要です。それは1人の助手Ravindraに、彼には公平ではない非問題であなたを助けようとする彼自身の貴重な自由時間を無駄にしました。 –

2

mainメソッドの後にコンストラクタの終了ブレースを保持していました。ここに訂正されたコードがあります。

public PipeGameApp(String title) { 
    super(title); 

    JPanel topBar = new JPanel(); 
    JButton startButton = new JButton("Start"); 
    startButton.addActionListener(this); 
} 

public static void main(String[] args) { 
    new PipeGameApp("The Frantic Pipe Layer").setVisible(true); 
} 

@Override 
public void actionPerformed(ActionEvent ae) { 
    startButton.setText("asdf"); 
} 
関連する問題