import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * HangFrame.java * * Created on Dec 16, 2011, 11:04:41 AM */ /** * * @author hanlchri */ public class HangFrame extends javax.swing.JFrame implements ActionListener { JButton[] butArray = new JButton[26]; /** Creates new form HangFrame */ public HangFrame() { initComponents(); //for loop to create the 26 buttons Character c = new Character('A'); //start with 'A' for (int i = 0; i < 26; i++) { butArray[i] = new JButton(c.toString()); butArray[i].setSize(50, 40); butPAN.add(butArray[i]); butArray[i].addActionListener(this); c++; } } public void actionPerformed(ActionEvent e) { //One of the buttons was selected for (int i = 0; i < 26; i++) { if (e.getSource() == butArray[i]) { //get Text from front of String letter = ((JButton) e.getSource()).getText(); System.out.println("Here is the button pressed " + letter); } } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { butPAN = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); butPAN.setLayout(new java.awt.GridLayout(3, 9)); getContentPane().add(butPAN); butPAN.setBounds(20, 80, 440, 110); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 150, Short.MAX_VALUE) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 110, Short.MAX_VALUE) ); getContentPane().add(jPanel2); jPanel2.setBounds(450, 90, 150, 110); pack(); }// //GEN-END:initComponents /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new HangFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel butPAN; private javax.swing.JPanel jPanel2; // End of variables declaration//GEN-END:variables }