Results 1 to 3 of 3
- 03-07-2008, 11:15 PM #1
Member
- Join Date
- Feb 2008
- Posts
- 16
- Rep Power
- 0
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.
Java 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
- 03-08-2008, 12:17 AM #2
For another option you can use the setActionCommand(String) method for each button to set the actionCommand you want to use. This is especially useful if your app will be used in an international setting: the button text may be translated into another language and may/wll not match the string you test for in your event code. For example, the "right" button in Spanish may have the text translated to "dereche."Java Code:public void actionPerformed(ActionEvent ae) { // Who is sending this event? System.out.println("source class name = " + ae.getSource().getClass().getName()); // If JButton is the only kind/type of component // that has registered with this ActionListener // then this cast will be safe. JButton a = (JButton)ae.getSource(); // Get the text on the button. String whichButton = a.getText(); // Or, you can do: String ac = ae.getActionCommand(); // Test for a certain button whose text is "right" if(ac.equals("right")) ; // found it System.out.println("whichButton = " + whichButton); test.setText(whichButton); changeLights(whichButton); }
- 03-12-2008, 02:51 PM #3
Member
- Join Date
- Feb 2008
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM -
pls help me out its critical ...... how can we use an arralylist reference
By raj reddy in forum Advanced JavaReplies: 1Last Post: 04-15-2008, 12:09 PM -
pls help me out its critical ...... how can we use an arralylist reference
By raj reddy in forum Web FrameworksReplies: 0Last Post: 04-15-2008, 08:12 AM -
reference to elements in array
By Igor in forum New To JavaReplies: 1Last Post: 12-14-2007, 11:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks