Results 1 to 6 of 6
  1. #1
    MrSulfat is offline Member
    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Post 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);
    		
    		 
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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 **

  3. #3
    MrSulfat is offline Member
    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Default Re: JButton ActionLister return int?

    So i can change the: static int spiller = 1;

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: JButton ActionLister return int?

    Why not just change it in the actionListener?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    MrSulfat is offline Member
    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Default Re: JButton ActionLister return int?

    Quote Originally Posted by jim829 View Post
    Why not just change it in the actionListener?

    Regards,
    Jim
    Java Code:
    button1.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent e) {
                 Static int spiller = 1;
                    }
                });
    Like this?

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: JButton ActionLister return int?

    well, did it do what you wanted it to? Thats the only real test of success :)

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2013, 07:44 PM
  2. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 08:35 AM
  3. Actionlister problem
    By jmoutia in forum New To Java
    Replies: 7
    Last Post: 10-30-2010, 11:22 AM
  4. Actionlister problem
    By jmoutia in forum AWT / Swing
    Replies: 0
    Last Post: 10-30-2010, 07:27 AM
  5. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 11:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •