Results 1 to 6 of 6
Thread: JButton ActionLister return int?
- 04-05-2013, 02:37 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
JButton ActionLister return int?
Hey guys :)
I am working on a Hardcore "Rock-Paper-Scissors"
and I have a problem, i know that a Void cant return an Int. I just what to ask you guys, if you cant help me :)
some of the code is in danish, I can translate it, if needed :)
her is the problem i think:
Java Code:button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { spiller=1; return spiller; } });
and her is the hole code:
Java Code:public class PatrickGMed5 { //Her defineres variable, som skal bruges i hele klassen. private final static int STEN =0; private final static int ILD =1; private final static int SAKS =2; private final static int SLANGE =3; private final static int MENNESKE =4; private final static int TRÆ =5; private final static int HUND =6; private final static int SVAMP =7; private final static int PAPIR =8; private final static int LUFT =9; private final static int VAND =10; private final static int DRAGE =11; private final static int DJÆVEL =12; private final static int LYN =13; private final static int PISTOL =14; static String resultat="Spillet er uafgjort"; static int spiller=1, computer=1; static String sp="STEN", pc="STEN"; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Dette er et hardcore sten-saks-papir spil, hvor man kan vælge mellem 5 muligheder: sten, ild, saks, slang, menneske, træ, hund, svamp, papir, luft, vand, drage, djævel, lyn eller pistol."); // Hent spillerens valg spiller = spillerValg(); // Hent computerens valg computer = computerensValg(); // Oversæt valg fra int til s fraIntTilString(); // Centrér spillerens valg centrerSpillerValg(); // Beregn resultat resultat = beregnResultat(spiller, computer); // Udskriv resultat udskrivResultat(sp, pc, resultat); } private static int spillerValg(){ JFrame frame = new JFrame("Set JFrame layout using Grid Layout"); //Create grid layout that contains : //2 rows //2 columns GridLayout layout=new GridLayout(4,6); //Set JFrame layout to grid layout with 2 rows and 2 columns frame.setLayout(layout); JButton button1 = new JButton("Sten"); JButton button2 = new JButton("Pistol"); JButton button3 = new JButton("lyn"); JButton button4 = new JButton("Djævel"); JButton button5 = new JButton("Drage"); JButton button6 = new JButton("Vand"); JButton button7 = new JButton("Ild"); JLabel label8 = new JLabel(" "); JLabel label9 = new JLabel(" "); JLabel label10 = new JLabel(" "); JLabel label11 = new JLabel(" "); JButton button12 = new JButton("Luft"); JButton button13 = new JButton("Saks"); JLabel label14 = new JLabel(" "); JLabel label15 = new JLabel(" "); JLabel label16 = new JLabel(" "); JLabel label17 = new JLabel(" "); JButton button18 = new JButton("Papir"); JButton button19 = new JButton("Slang"); JButton button20 = new JButton("Menneske"); JButton button21 = new JButton("Træ"); JButton button22 = new JButton("Hund"); JButton button23 = new JButton("Svamp"); JButton button24 = new JButton("Back"); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { spiller=1; return spiller; } }); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.add(button7); frame.add(label8); frame.add(label9); frame.add(label10); frame.add(label11); frame.add(button12); frame.add(button13); frame.add(label14); frame.add(label15); frame.add(label16); frame.add(label17); frame.add(button18); frame.add(button19); frame.add(button20); frame.add(button21); frame.add(button22); frame.add(button23); frame.add(button24); //Set default close operation for JFrame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set JFrame size : //Width : 400 pixels //Height : 400 pixels frame.setSize(800,700); //Make JFrame visible. So we can see it. frame.setVisible(true); } private static void centrerSpillerValg(){ System.out.println("Nu er vi inde i metoden centrerSpillerValg()"); int tæller=0; while(spiller!=2){ tæller++; if(spiller>2){ spiller--; computer--; } else{ spiller++; computer++; } //antal gennemløb begrænses if(tæller>10) spiller=3; } } private static int computerensValg(){ double valg = 14*Math.random(); System.out.println("computerens valg: "+valg); int intValg = (int) valg; System.out.println("computerens valg som integer: "+intValg); return intValg; } private static void fraIntTilString(){ //IF SPILLER if(spiller==0) {sp="STEN";} else if(spiller==1) {sp="ILD";} else if(spiller==2) {sp="SAKS";} else if(spiller==3) {sp="SLANG";} else if(spiller==4) {sp="MENNESKE";} else if(spiller==5) {sp="TRÆ";} else if(spiller==6) {sp="HUND";} else if(spiller==7) {sp="SVAMP";} else if(spiller==8) {sp="PAPIR";} else if(spiller==9) {sp="LUFT";} else if(spiller==10) {sp="VAND";} else if(spiller==11) {sp="DRAGE";} else if(spiller==12) {sp="DJÆVEL";} else if(spiller==13) {sp="LYN";} else if(spiller==14) {sp="PISTOL";} // IF COMPUTER if(computer==0) {sp="STEN";} else if(computer==1) {sp="ILD";} else if(computer==2) {sp="SAKS";} else if(computer==3) {sp="SLANG";} else if(computer==4) {sp="MENNESKE";} else if(computer==5) {sp="TRÆ";} else if(computer==6) {sp="HUND";} else if(computer==7) {sp="SVAMP";} else if(computer==8) {sp="PAPIR";} else if(computer==9) {sp="LUFT";} else if(computer==10) {sp="VAND";} else if(computer==11) {sp="DRAGE";} else if(computer==12) {sp="DJÆVEL";} else if(computer==13) {sp="LYN";} else if(computer==14) {sp="PISTOL";} } private static String beregnResultat(int spiller, int computer){ if(spiller>computer) return "Du har vundet"; else if(spiller==computer) return "Spillet er uafgjort"; else return "Du har tabt"; } private static void udskrivResultat(String sp, String pc, String resultat){ System.out.println("Spiller valgte: "+sp+". PC valgte: "+pc+". Resultat: "+resultat); } }
- 04-05-2013, 03:58 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: JButton ActionLister return int?
Why do you want to return something from the action listener?
Please do not ask for code as refusal often offends.
** This space for rent **
- 05-15-2013, 06:34 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
Re: JButton ActionLister return int?
So i can change the: static int spiller = 1;
- 05-15-2013, 06:46 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: JButton ActionLister return int?
Why not just change it in the actionListener?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 05-15-2013, 09:56 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
- 05-15-2013, 10:32 PM #6
Senior Member
- Join Date
- Nov 2012
- Posts
- 257
- Rep Power
- 9
Similar Threads
-
How set JButton icon Relative to the JButton size???
By farshad in forum AWT / SwingReplies: 1Last Post: 01-15-2013, 07:44 PM -
ActionListener for JButton after changing Button to JButton
By ravi.joshi53 in forum Java AppletsReplies: 2Last Post: 10-07-2011, 08:35 AM -
Actionlister problem
By jmoutia in forum New To JavaReplies: 7Last Post: 10-30-2010, 11:22 AM -
Actionlister problem
By jmoutia in forum AWT / SwingReplies: 0Last Post: 10-30-2010, 07:27 AM -
Help with JButton
By geoffreybarwise in forum New To JavaReplies: 4Last Post: 05-21-2008, 11:48 AM
Bookmarks