if a method that is returning an interface , what should i do so that i can get the return value ..?
instance : in ItemEvent class , there is a method named getItemSelectable() with ItemSelectable as a return value .
can i just declare Object type reference to get the return value ..?
Object source = ItemEvent.getItemSelectable();
![]()
2.6 The Checkbox and CheckboxGroup classes
You need to either check its type, compare it against know objects or typecast it if you want to access values.
Just lookup "java getItemSelectable" on google and you will find a bunch of examples.
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/
the problem is i dunno how to check the return type of getItemSelectable(since it return a interface type value)
Same as a normal class. Use getClass and either compare to a class or use getClass().getName() and compare to a string.
You can also just typecast and if its not correct, Java will spit the dummy (ie: throw an exception), which you can catch. It depends on how you prefer to code.Code:interface MyInterface { } MyInterface myObject; if (myObject.getClass() == MyInterface.class) { }
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/
i get what u r trying to say ..~
if that a possibility to downcast the object class to string class..?
like this example ..~
i try to search getSelectedObject method in ItemEvent , but i cant find it...~Code:static private String selectedString(ItemSelectable is) { Object selected[] = is.getSelectedObjects(); return ((selected.length == 0) ? "null" : (String)selected[0]);
Um. Short answer, um no. Long answer, maybe, if the object that is selected was derived from a String, which Im not even sure is possible.
Why would you think the ItemSelectable is a string? Check its ClassName if you want to know what it is.
You can only typecast an object to itself or a class it inherited from. So, find out what the object is and typecast it in order to access any variables/methods.Code:System.out.print(this.getClass().getName());
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/
btw, getSelectedObject() is from ItemSelectable, not ItemEvent
ItemSelectable (Java 2 Platform SE v1.4.2)
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/
the "this" is refer to the Object type ?
this code is from yoursCode:System.out.print(this.getClass().getName());
so i can compare the class by using myObject.getClass() == MyInterface.class ?Code:interface MyInterface { } MyInterface myObject; if (myObject.getClass() == MyInterface.class) { }
the .class is from where..?![]()
O god. Just go do something else!
You are either playing this forum, or completely stupid. As i dont beleive you are stupid (based on your english), I beleive you are just playing this forum.
Do you get paid for doing this? What is your goal?
No human can be as stupid as you, therefore, you are a fake.
Why? Are you paid by this site?
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/
this is my assignment title
this is an incomplete codeCode:A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given a six cards in the deck, two might be labeled 1, two labeled 2, and two labeled 3. The cards are shuffled and placed face down on the table. A player then selects two cards that are face down, turns the face up, and if the cards match they are left face up. If the two cards do not match, they are returned to their original face down position. The game continues until all the cards are face up.
this is the algorithm i have writtenCode:import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import javax.swing.JOptionPane; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.util.Random; import java.awt.GridLayout; public class Assignment1 extends JPanel { private JFrame frame; private JButton buttons[]; private GridLayout layout; private Random $rand; private String label_name[]; private int shuffled public Assignment1() { frame = new JFrame("Xtreme Game"); layout = new GridLayout(4 , 4 , 5 , 5); super(layout); buttons = new JButton[8]; $rand = new Random(); label_name = { "1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8" }; shuffled_int = new int[16]; shuffling(); createButton(); add_buttons_to_layout(); } private void shuffling() { for(int i = 0 ; i < label_name.length ; i++) { int x = $rand.nextInt(16); String temp = label_name[i]; label_name[i] = label_name[x]; shuffled[i] = Integer.parseInt(label_name[i]); } } private void createButton() { for(int i = 0 ; i < buttons.length ; i++) { buttons[i] = new JButton("*"); } } private void add_buttons_to_layout() { for(int i = 0 ; i < 16 ; i++) { this.add(buttons[shuffled_int[i]-1]); } }
Code:/* Class extends from JPanel JFrame object created 8 objects of JButton will be created every buttons are labelled with "*" for(int i = 0 ; i < length ; i++) { buttons[i] = new JButton("*"); } String label_name[] = {"1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8" }; int shuffled[] = { }; for( int i = 0 ; i < length ; i++) { int x = random.nextInt(16); String temp = label_name[i]; label_name[i] = label_name[x]; label_name[x] = temp; shuffled[i] = Integer.parseInt(shuffled[i]); } add every button into the layout for(int i = 0 ; i < length ; i++) { JPanel.add(buttons[shuffled[i]-1]); } add the layout to the content pane JFrame.setContentPane(JPanel's reference); create a event handler EventInstance reference = new EventInstance(); register with the event for(int i = 0 ; i < length ; i++) { buttons[i].addItemListener(reference); } create an inner class for the EventInstance with extending the ItemListener private class EventInstance extends ItemListener { public void itemStateChange(ItemEvent event) { if(toggle_button == 1) { Object source = event.getItemSelectable(); for(int i = 0 ; i < length ; i++) { if(source == buttons[i]) { buttons[i].setLabel((string)shuffled[i]); x = i; } } toggle_button++; } if(toggle_button == 2) { Object source = event.getItemSelectable(); for(int i = 0 ; i < length ; i++) { if(source == buttons[i]) { buttons[i].setLabel((string)shuffled[i]); y = i; } } toggle_button = 1; if(buttons[x] == buttons[y]) { buttons[x].setEnabled(false); buttons[y].setEnabled(false); pair_matched++ } else { buttons[x].setLabel("*"); buttons[y].setLabel("*"); } if(pair_matched == 8) { JOptionPane(You have won); } } } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks