2011-09-04 10 views
5

मेरे पास मेरे प्रोग्राम में कॉलम में व्यवस्थित बटन की 4 सूचियां हैं I अभी तक मेरे पास 4 लूप हैं जो यह देखने के लिए जांचते हैं कि कोई बटन क्लिक किया गया है या नहीं। क्या यह जांचने का एक आसान तरीका है कि प्रत्येक सूची के माध्यम से लूपिंग के बजाय कोई बटन क्लिक किया गया है या नहीं, यह देखने के लिए कि क्या कुछ बटन क्लिक किया गया था या नहीं। यह जांचने का एक आसान तरीका होना चाहिए कि "actionSource == anybutton" ...जावा एक्शन श्रोता

उत्तर

20

उपयोग प्रत्येक बटन के लिए अनाम इनर क्लासों:

JButton button = new JButton("Do Something"); 
button.addActionListener(new ActionListener() 
{ 
    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     System.out.println("Do Something Clicked"); 
    } 
}); 

या अगर आपके तर्क से संबंधित है, तो आप एक श्रोता साझा कर सकते हैं:

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

public class ButtonCalculator extends JFrame implements ActionListener 
{ 
    private JButton[] buttons; 
    private JTextField display; 

    public ButtonCalculator() 
    { 
     display = new JTextField(); 
     display.setEditable(false); 
     display.setHorizontalAlignment(JTextField.RIGHT); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new GridLayout(0, 5)); 
     buttons = new JButton[10]; 

     for (int i = 0; i < buttons.length; i++) 
     { 
      String text = String.valueOf(i); 
      JButton button = new JButton(text); 
      button.addActionListener(this); 
      button.setMnemonic(text.charAt(0)); 
      buttons[i] = button; 
      buttonPanel.add(button); 
     } 

     getContentPane().add(display, BorderLayout.NORTH); 
     getContentPane().add(buttonPanel, BorderLayout.SOUTH); 
     setResizable(false); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     JButton source = (JButton)e.getSource(); 
     display.replaceSelection(source.getActionCommand()); 
    } 

    public static void main(String[] args) 
    { 
     UIManager.put("Button.margin", new Insets(10, 10, 10, 10)); 
     ButtonCalculator frame = new ButtonCalculator(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+1

कैमिकर मेरी आंखों में एक स्विंग प्राधिकरण है। – duffymo

+0

श्रोता साझा करें ... अच्छा, सलाह के लिए धन्यवाद! यह पूरी तरह से काम किया! – mbreen

1

आप प्रत्येक बटन के लिए एक व्यक्तिगत श्रोता और प्रत्येक बटन के लिए एक आम श्रोता जोड़ सकते हैं। "दबाए गए किसी भी बटन" का जवाब देने के लिए आम श्रोता कार्यक्रम।

0

किसी भी समय आप एक बटन क्लिक करें, यह चलाता है actionPerformed विधि, चाहे आप किस बटन दबाए हों।

public void actionPerformed(ActionEvent event) { 
    Object source = event.getSource(); 
    if (source instanceof JButton) System.out.println("You clicked a button!"); 
} 
+0

मेरे पास अभी भी श्रोताओं के साथ अन्य वस्तु है, जैसे कि कॉम्बो बॉक्स और मेनू आइटम। मुझे केवल एक निश्चित कार्य करने के लिए बटन चाहिए, श्रोता के साथ सभी आइटम नहीं। – mbreen

+0

@mbreen अद्यतन उत्तर देखें। – fireshadow52

संबंधित मुद्दे