View Single Post
  #1 (permalink)  
Old 03-08-2008, 12:15 AM
Deathmonger Deathmonger is offline
Member
 
Join Date: Feb 2008
Posts: 16
Deathmonger is on a distinguished road
Getting the Object Reference Name
Hi, I'm creatinga simple Lights Out game, and I'm trying to figure out a way to access the name of the button that is pressed. So if I have three buttons named a1, a2, and a3, and the user presses on a2, then I can have a String that is equal to "a2." I looked up getSource() and getAccessibleName(), but I'm not entirely sure how to use it correctly. Any help is greatly appreciated.

Here is my code, the problem right now is with the method actionPerformed(), and I'm pretty sure this isn't the most efficient way to the write the code for this program either. So any tips would also be great.

Code:
import javax.accessibility.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.lang.Object.*; public class LightsOut { static JButton a1; static JButton a2; static JButton a3; static JButton a4; static JButton b1; static JButton b2; static JButton b3; static JButton b4; static JButton c1; static JButton c2; static JButton c3; static JButton c4; static JButton d1; static JButton d2; static JButton d3; static JButton d4; static JLabel test; static JFrame frame; static Container pane; static class OnOff implements ActionListener { public void actionPerformed(ActionEvent ae) { JButton a = new JButton(); String whichButton; a = ae.getSource(); whichButton = a.getAccessibleName(); test.setText(whichButton); changeLights(whichButton); } public void changeLights(String button) { int row; int col; char first; first = button.charAt(0); col = Integer.valueOf(button.charAt(1)).intValue(); switch(first) { case('a') : switch(col) { case(1) : switchLight(a2); switchLight(b1); break; case(2) : switchLight(a1); switchLight(a3); switchLight(b2); break; case(3) : switchLight(a2); switchLight(a4); switchLight(b3); break; case(4) : switchLight(a3); switchLight(b4); break; } break; case('b') : switch(col) { case(1) : switchLight(a1); switchLight(c1); switchLight(b2); break; case(2) : switchLight(b1); switchLight(a2); switchLight(b3); switchLight(c2); break; case(3) : switchLight(b2); switchLight(b4); switchLight(a3); switchLight(c3); break; case(4) : switchLight(b3); switchLight(a4); switchLight(c4); break; } break; case('c') : switch(col) { case(1) : switchLight(b1); switchLight(c2); switchLight(d1); break; case(2) : switchLight(b2); switchLight(c1); switchLight(c3); switchLight(d2); break; case(3) : switchLight(b3); switchLight(c2); switchLight(c4); switchLight(d3); break; case(4) : switchLight(b4); switchLight(c3); switchLight(d4); break; } break; case('d') : switch(col) { case(1) : switchLight(c1); switchLight(d2); break; case(2) : switchLight(c2); switchLight(d1); switchLight(d3); break; case(3) : switchLight(c3); switchLight(d2); switchLight(d4); break; case(4) : switchLight(c4); switchLight(d3); break; } break; } } public void switchLight(JButton button) { String status; status = button.getText(); if(status.equals("^OFF^")) { status = "*ON*"; } else { status = "^OFF^"; } button.setText(status); } } public static void main(String[] args) { OnOff oo = new OnOff(); a1= new JButton("^OFF^"); a2= new JButton("^OFF^"); a3= new JButton("^OFF^"); a4= new JButton("^OFF^"); b1= new JButton("^OFF^"); b2= new JButton("^OFF^"); b3= new JButton("^OFF^"); b4= new JButton("^OFF^"); c1= new JButton("^OFF^"); c2= new JButton("^OFF^"); c3= new JButton("^OFF^"); c4= new JButton("^OFF^"); d1= new JButton("^OFF^"); d2= new JButton("^OFF^"); d3= new JButton("^OFF^"); d4= new JButton("^OFF^"); test = new JLabel(); frame = new JFrame("Lights Out"); pane = new Container(); a1.addActionListener(oo); a2.addActionListener(oo); a3.addActionListener(oo); a4.addActionListener(oo); b1.addActionListener(oo); b2.addActionListener(oo); b3.addActionListener(oo); b4.addActionListener(oo); c1.addActionListener(oo); c2.addActionListener(oo); c3.addActionListener(oo); c4.addActionListener(oo); d1.addActionListener(oo); d2.addActionListener(oo); d3.addActionListener(oo); d4.addActionListener(oo); frame.setSize(400,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane = frame.getContentPane(); pane.setLayout(new GridLayout(4,4)); pane.add(a1); pane.add(a2); pane.add(a3); pane.add(a4); pane.add(b1); pane.add(b2); pane.add(b3); pane.add(b4); pane.add(c1); pane.add(c2); pane.add(c3); pane.add(c4); pane.add(d1); pane.add(d2); pane.add(d3); pane.add(d4); pane.add(test); frame.setVisible(true); } }
__________________
Jai guru deva om, Nothing's gonna change my world
Reply With Quote
Sponsored Links