java - How to make JButton reference object when pressed? -
first off, first post stack overflow please forgive me if not following of proper etiquitte. im trying make gui program display different food genres , when user presses genre want show randomly generated restraunt of genre. new programming , having hard time figuing out how make buttons work. have assigned restraunts objects each value. mexican values 1 , 2. italian values 3 , 4. im wanting when user selects "mexican" program generate random number (the restraunts value attribute) between 1 , 2 , display object attributes in same window. i've been stuck on part bit , appreciated. thank in advance time. code have follows:
import trysource.trywindow; import trysource.restraunts; import java.awt.flowlayout; import javax.swing.jframe; public class trysomethingnew { public static void main (string[] args) { trywindow frame = new trywindow(); frame.settitle("try new"); frame.setsize(1000,900); frame.setlayout(new flowlayout()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationrelativeto(null); frame.setvisible(true); } }
package trysource; import trysource.trywindow.buttonclicked; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jbutton; import javax.swing.jframe; public class trywindow extends jframe { jbutton jbtmexican = new jbutton("mexican"); jbutton jbtitalian = new jbutton("italian"); public trywindow() { super("try new"); add(jbtmexican); add(jbtitalian); }//end trywindow constructor class buttonclicked implements mouselistener { @override public void mouseclicked(mouseevent e) { } @override public void mousepressed(mouseevent e) { } @override public void mousereleased(mouseevent e) { } @override public void mouseentered(mouseevent e) { } @override public void mouseexited(mouseevent e) { } } }//end class
package trysource; public class restraunts { string restrauntname; string restrauntaddress; string restrauntphone; public restraunts(string name, string address, string phone, int value) { restrauntname = name; restrauntaddress = address; restrauntphone = phone; }//end constructor restraunts joselocos = new restraunts("jose locos", "853 n glenstone ave, springfield, mo 65802", "(417) 831-1300", 1); restraunts amigos = new restraunts ("amigos mexican restaurant","2118 s campbell ave, springfield, mo 65807", "(417) 887-1401", 2); restraunts zios = new restraunts("zios italian kitchen", "1249 e kingsley st, springfield, mo 65804", "(417) 889-1919", 3); restraunts bambinos = new restraunts("bambinos cafe", "1141 e delmar st, springfield, mo 65807", "(417) 862-9999", 4); }//end restraunts
- add
restraunts
list
- group them (by type) using
map
- use
collections.shuffle
randomiselist
, select first one
take at
for more details
oh, , might gave @ how use cardlayout
Comments
Post a Comment