2013-07-07 7 views
5

ठीक है, इसलिए मैंने एक साधारण प्रोग्राम बनाया है जो बटन को क्लिक करने पर हर बार काउंटर करने के लिए मूल्य जोड़ता है। अब, "ऑटो" बटन क्लिक होने पर काउंटर के मान को बढ़ाने के लिए मैं "ऑटो" बटन सुविधा जोड़ना चाहता हूं।बटन एक्शनलिस्टर

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.concurrent.TimeUnit; 
import javax.swing.JButton; 
import javax.swing.JFrame; 


public class Gui extends JFrame{ 

    private static final long serialVersionUID = 1L; 

    private JButton uselesButton; 

    private JButton autoButton; 

    private FlowLayout layout; 
    private long counter = 0; 

    public Gui() { 
     super("Button"); 
     layout = new FlowLayout(FlowLayout.CENTER); 
     this.setLayout(layout); 

     uselesButton = new JButton(String.format("Pressed %d times", counter)); 
     add(uselesButton); 
     uselesButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       counter++; 
       uselesButton.setText(String.format("Pressed %d times", counter)); 
      } 

     }); 

     autoButton = new JButton("Auto"); 
     add(autoButton); 
     autoButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
         for(long i =0; i < 99999999;i++) { 
         try { 
          TimeUnit.MILLISECONDS.sleep(10); 
         } catch (InterruptedException e1) { 
          System.out.println("ERROR"); 
         } 
         counter = i; 
         uselesButton.setText(String.format("Pressed %d times", counter)); 
        } 
        } 
     }); 
    } 
} 

ध्यान रखें कि मैं कर रहा हूँ: मैं इसके साथ समस्या आ रही है, क्योंकि यह है, जब पाश किया जाता है बजाय मूल्य अपडेट स्क्रीन पर प्रत्येक काउंटर मूल्य से नहीं चल पाएगी .. यहाँ मेरी कोड है एक नौसिखिया ... सभी मदद की सराहना की :)

+0

प्रश्न क्या है? – Sello

+1

क्या * बटन * ऑटो बटन क्या करना चाहिए? –

+3

मुझे लगता है [स्विंग टाइमर] (http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) इसे हल करेंगे। – Azad

उत्तर

4

के बारे में कैसे Swing Timer उपयोग करने के लिए ट्यूटोरियल पर एक नज़र डालें और फिर मेरी समाधान को देखो:

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 

public class Gui extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JButton uselesButton; 
    private JButton autoButton; 
    private FlowLayout layout; 
    private long counter = 0; 
    private javax.swing.Timer timer; 

    public Gui() { 
     super("Button"); 
     layout = new FlowLayout(FlowLayout.CENTER); 
     setLayout(layout); 
     setDefaultCloseOperation(3); 
     setSize(300, 300); 
     setLocationRelativeTo(null); 

     //initialing swing timer 
     timer = new javax.swing.Timer(100, getButtonAction()); 

     autoButton = new JButton("Auto"); 
     add(autoButton); 
     autoButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (!timer.isRunning()) { 
        timer.start(); 
       } else { 
        timer.stop(); 
       } 
      } 
     }); 
    } 

    private ActionListener getButtonAction() { 
     ActionListener action = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       autoButton.setText(String.format("Pressed %d times", ++counter)); 
       if (counter > 1000) { 
        timer.stop(); 
       } 
      } 
     }; 
     return action; 
    } 

    public static void main(String... args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Gui().setVisible(true); 
      } 
     }); 
    } 
} 
0

समस्या यह है कि सिस्टम लूप में है, इसलिए यह परिवर्तनों को पेंट नहीं कर सकता है। ऐसा करने के लिए आपको एक नया धागा खोलने की आवश्यकता है। नया धागा लूप करेगा, और मुख्य धागा फॉर्म को पेंट करेगा।

एक और बात, आपको मुख्य धागे पर नींद नहीं करना चाहिए। आप एक टाइमर है कि हर 10 मिलीसेकंड बजाय sleep(10) here एक उदाहरण

+2

क्या स्विंग टाइमर बेहतर विकल्प नहीं होगा? –

+1

क्या आपने अपनी सिफारिशों का परीक्षण किया है? ईडीटी को बांधने वाले लूप के मामले में 'repaint() 'कुछ नहीं करेगा। कृपया इस सिफारिश को हटा दें ताकि मैं डाउन-वोट हटा सकूं। –

+0

इसके अलावा, पेंटिंग थ्रेड मुख्य धागा नहीं है। यह घटना प्रेषण धागा है। –

0

अपने कोड ब्लॉक जीयूआई धागा (EDT) जब इस लूप के अंदर प्रवेश (जीयूआई रखती है टिक जाएगा उपयोग कर सकते हैं, बटन को अपडेट नहीं करेंगे जब तक आप समाप्त) है, तो आप एक और कार्यकर्ता धागा अंदर अपना कोड जोड़ना चाहिए:

autoButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
        new Thread(new Runnable() { 
         @Override 
         public void run() { 
          for(long i =0; i < 99999999;i++) { 
           try { 
            TimeUnit.MILLISECONDS.sleep(10); 
           } catch (InterruptedException e1) { 
            System.out.println("ERROR"); 
           } 
           counter = i; 

           java.awt.EventQueue.invokeLater(new Runnable() { 
             public void run() { 
             uselesButton.setText(String.format("Pressed %d times", counter)); 
             } 
           }); 
          } 
         } 
        }).start(); 
      } 
     });