2013-03-08 5 views
7

मैं अपने बटन को "स्टार्ट" कहने से रोकने के लिए "रोकें" पर क्लिक करने की कोशिश कर रहा हूं। ऐसा करने का मेरा प्रयास नीचे है, मैंने इसे देखा और गाइड कॉपी करने की कोशिश की लेकिन मुझे नहीं पता कि मैं क्या गलत कर रहा हूं। मुझे कुछ "}" गायब हो सकता है क्योंकि मैंने बहुत सारे कोड को छोड़ दिया है जो अप्रासंगिक है। क्या कोई देख सकता है कि मैं क्या गलत कर रहा हूं?क्लिक करने के बाद बटन का नाम बदलना - जावा जेबटन

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

कृपया मेरे उत्तर में संपादित करें देखें। आपका चरम छायांकन की बजाय समस्या नहीं बल्कि परिवर्तनीय गुंजाइश है। मुझे आश्चर्य है कि आपका कोड संकलित करता है क्योंकि आपके पास स्टार्ट बटन के लिए क्लास संदर्भ नहीं है। या वास्तव में यह संकलन नहीं करता है? –

उत्तर

5

आपकी समस्या सीमित चरणीय दायरे में से एक है। आपका स्टार्टबटन वैरिएबल कन्स्ट्रक्टर में घोषित किया जा रहा है और इस प्रकार केवल निर्माता में दिखाई देता है। आपको कक्षा में इसे घोषित करने की आवश्यकता है कि इसे कन्स्ट्रक्टर में फिर से घोषित न करें, शेष वर्ग को वेरिएबल को "देखने" में सक्षम होने और इसका उपयोग करने में सक्षम होना चाहिए। यह करने के लिए

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() विधि द्वारा वापस का उपयोग , और i पर आधारित यह नया राज्य सेट करें टी वर्तमान राज्य।
उदाहरण के लिए

,

@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: खुशी है कि आप इसे हल कर चुके हैं। कृपया समझें कि "मेरा पूरा कोड" अक्सर बहुत अधिक होता है। सबसे महत्वपूर्ण बात यह है कि आप शुरू में जंक कोड पोस्ट नहीं करते हैं। वह लापता ब्रेस अप्रासंगिक नहीं है लेकिन वास्तव में काफी महत्वपूर्ण है। इसने एक सहायक, रविंद्र को अपने स्वयं के मूल्यवान खाली समय को गैर-मुद्दे के साथ मदद करने की कोशिश करने का प्रयास किया, जो उनके लिए उचित नहीं है। –

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"); 
} 
संबंधित मुद्दे