7 Replies - 649 Views - Last Post: 22 December 2009 - 03:01 PM Rate Topic: -----

#1 funstufflol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-December 09

Glitch...

Posted 22 December 2009 - 07:09 AM

I can't solve this glitch and it would be hard to explain unless you have a program that can compile this code such as dr.java. So the problem is when you click on a Jbutton(game), the action listener i put on that button doesn't show up unless you minimize the window and restore it back to normal.
Here is my code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import javax.swing.event.*;
import java.util.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.lang.Math;

public class AJRGAMES
{ 
  
  static JButton nextGame, game;
  static JLabel games;
  static JPanel hedPanel;
  static JFrame frmMain;
  static Container pane;
  static int player, player2, gameTotal, gameTotal2;
 
	public static void main ( String[] args ){
  { 
	 //Prepare frame
		frmMain = new JFrame ("AJR Basketball Games and Schedule"); //Create frame
		frmMain.setSize(320, 375); //Set size to 400x400 pixels
		pane = frmMain.getContentPane(); //Get content pane	   
		frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close when X is clicked

	//controls
	nextGame = new JButton ("next game >>");		
	games = new JLabel("DAY 1");
	game = new JButton("Game Time!");	
	hedPanel = new JPanel(); 
   
	games.setBounds(10, 30,  300, 20);
	game.setBounds( 90, 0, 110, 20 );
	nextGame.setBounds(260, 50, 50, 25);

	pane.add(hedPanel);
	hedPanel.add (games);
	hedPanel.add( game );
	hedPanel.add( nextGame);
	
   //Register action listeners
   game.addActionListener(new game_Action());
	
   //make frame visible
   frmMain.setResizable(true);
   frmMain.setVisible(true);   
  }
  }
	static class game_Action implements ActionListener{
		public void actionPerformed (ActionEvent e){
	Random r = new Random();
	Random t = new Random();
	int[] player = new int[5];
	int[] player2 = new int[5];
	int gameTotal, gameTotal2;
	 
   //random team
	String[] eastdivision = new String[8]; 
	String[] westdivision = new String[8];
	 
	eastdivision[0] = "Toronto"; 
	eastdivision[1] = "Quebec City";
	eastdivision[2] = "Mississauga";
	eastdivision[3] = "Montreal";
	eastdivision[4] = "Charlottetown";
	eastdivision[5] = "Halifax";
	eastdivision[6] = "St.Johns";  
	eastdivision[7] = "Ottawa";
	  
	 
	westdivision[0] = "Vancouver";
	westdivision[1] = "Victoria";
	westdivision[2] = "Edmonton";
	westdivision[3] = "Calgary";
	westdivision[4] = "Whitehorse";
	westdivision[5] = "Winnipeg";
	westdivision[6] = "Iqualuit";
	westdivision[7] = "Yellowknife"; 
	  
	String east = eastdivision[t.nextInt(8)];
	String west = westdivision[t.nextInt(8)];

	//create border
	Border border = LineBorder.createGrayLineBorder();
   
	//Random for the first team  
	player[0] = r.nextInt(50);
	player[1] = r.nextInt(50);
	player[2] = r.nextInt(50);
	player[3] = r.nextInt(50);
	player[4] = r.nextInt(50);
	
	//Random for the second team
	player2[0] = r.nextInt(50);
	player2[1] = r.nextInt(50);
	player2[2] = r.nextInt(50);
	player2[3] = r.nextInt(50);
	player2[4] = r.nextInt(50);
	
	//First team
	JLabel hs = new JLabel();
	hs.setText( east + " stats : ");
	hs.setBorder(border);
	hs.setHorizontalTextPosition(JLabel.LEFT);
	hs.setVerticalTextPosition(JLabel.BOTTOM);
	hs.setForeground(Color.blue);
	hedPanel.add( hs ); 
	
	JLabel hp = new JLabel();
	hp.setText("Point Guard[0] has " + player[0] + " points	   ");
	hp.setBounds( 0, 45, 150, 30 );
	hedPanel.add( hp );
   
	JLabel hp1 = new JLabel();
	hp1.setText("Shooting Guard[1] has " + player[1] + " points	 ");
	hp1.setBounds( 0, 60, 150, 30 );
	hedPanel.add( hp1 );
	
	JLabel hp2 = new JLabel();
	hp2.setText("Small Forward[2] has " + player[2]  + " points	");
	hp2.setBounds( 0, 75, 150, 30 );
	hedPanel.add( hp2 );
   
	JLabel hp3 = new JLabel(); 
	hp3.setText("Power Foward[3] has " + player[3] + " points		 ");
	hp3.setBounds( 0, 90, 150, 30 );
	hedPanel.add( hp3 );
   
	JLabel hp4 = new JLabel();
	hp4.setText("Center[4] has " + player[4] + " points									   ");
	hp4.setBounds( 0, 105, 150, 30 );
	hedPanel.add( hp4 );
	
	
	//Second team
	JLabel as = new JLabel();
	as.setText(west + " Stats : ");
	as.setBounds( 0, 135, 150, 30 );
	as.setBorder(border);
	as.setForeground(Color.red);
	hedPanel.add( as );
	
	JLabel ap = new JLabel();
	ap.setText("Point Guard[0] has " + player2[0]  + " points		 " );
	ap.setBounds( 0, 150, 150, 30 );
	hedPanel.add( ap );
   
	JLabel ap1 = new JLabel();
	ap1.setText("Shooting Guard[1] has " + player2[1] + " points		" );
	ap1.setBounds( 0, 165, 150, 30 );
	hedPanel.add( ap1 );
	
	JLabel ap2 = new JLabel();
	ap2.setText("Small Forward[2] has " + player2[2] + " points	   ");
	ap2.setBounds( 0, 180, 150, 30 );
	hedPanel.add( ap2 );
   
	JLabel ap3 = new JLabel();
	ap3.setText("Power Foward[3] has " + player2[3]  + " points		  ");
	ap3.setBounds( 0, 195, 150, 30 );
	hedPanel.add( ap3 );
   
	JLabel ap4 = new JLabel();
	ap4.setText("Center[4] has " + player2[4]  + " points											   ");
	ap4.setBounds( 0, 210, 150, 30 );
	hedPanel.add( ap4 );
	
	//Total points 
	gameTotal = player[0] + player[1] + player[2] + player[3] + player[4];
	gameTotal2 = player2[0] + player2[1] + player2[2] + player2[3] + player2[4];
	
   
	JLabel hts = new JLabel();
	hts.setText(east + " scores " + gameTotal + " points ");
	hts.setBounds( 0, 240, 150, 30 );
	hts.setBorder(border);
	hts.setForeground(Color.blue);
	hedPanel.add( hts );
   
	JLabel ats = new JLabel();
	ats.setText(west + " scores " + gameTotal2 + " points ");
	ats.setBounds( 0, 270, 150, 30 );
	ats.setBorder(border);
	ats.setForeground(Color.red);
	hedPanel.add( ats );
	
	if ( gameTotal > gameTotal2 ) {
	  JLabel htw = new JLabel();
	  htw.setText(east + " wins! ");
	  htw.setBounds( 0, 285, 150, 50 );
	  htw.setBorder(border);
	  htw.setForeground(Color.blue);
	  hedPanel.add( htw );
	 } else if ( gameTotal < gameTotal2) {
	   JLabel atw = new JLabel();
	   atw.setText(west + " wins! ");
	   atw.setBounds( 0, 300, 150, 50 );
	   atw.setBorder(border);
	   atw.setForeground(Color.red);
	   hedPanel.add( atw );
	 }
   
	 
  
	   }
	}
}

This post has been edited by funstufflol: 22 December 2009 - 07:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Glitch...

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Glitch...

Posted 22 December 2009 - 07:30 AM

You probably need to add frmMain.repaint(); at the end of your actionPerformed method.
Was This Post Helpful? 0
  • +
  • -

#3 funstufflol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-December 09

Re: Glitch...

Posted 22 December 2009 - 07:35 AM

hmm. I did that but there's an error saying identifier expected for that part
<frmMain.repaint();>
Was This Post Helpful? 0
  • +
  • -

#4 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Glitch...

Posted 22 December 2009 - 07:46 AM


	 // ... all the shtuff ...
	 // ... and now the end of actionPerformed
	 } else if ( gameTotal < gameTotal2) {
	   JLabel atw = new JLabel();
	   atw.setText(west + " wins! ");
	   atw.setBounds( 0, 300, 150, 50 );
	   atw.setBorder(border);
	   atw.setForeground(Color.red);
	   hedPanel.add( atw );
	 }
	 // ... momentary pause in your code ...
	 
	 // -------%<-----------------------------
	 frmMain.repaint();
	 // -------%<-----------------------------

	 // ... your code starts again here ...
	   }
	}
}


Was This Post Helpful? 1

#5 funstufflol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 20-December 09

Re: Glitch...

Posted 22 December 2009 - 12:13 PM

cool its working now thnx


I have one more question though, since when u click on the game button (Jbutton) it shows the action listener but when u accidentally click it twice it will overlap the previous content of action listener. How do i limit that or stop it from overlapping the previous content when u press the jbutton more than once?

This post has been edited by funstufflol: 22 December 2009 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 HunterE30633  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-October 09

Re: Glitch...

Posted 22 December 2009 - 02:33 PM

View Postfunstufflol, on 22 Dec, 2009 - 11:13 AM, said:

cool its working now thnx


I have one more question though, since when u click on the game button (Jbutton) it shows the action listener but when u accidentally click it twice it will overlap the previous content of action listener. How do i limit that or stop it from overlapping the previous content when u press the jbutton more than once?


well a quick fix to this is to create a boolean that would be set to false once your action starts, and set to true once it ends so that way, it shows availability. If you press it, switches to false thus allowing the user to press the button but nothing would happen.

Otherwise, same concept, just enable it or disable it. That would be a little cleaner and user-friendly approach
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Glitch...

Posted 22 December 2009 - 02:34 PM

You could, in the actionListener, set the button, so that you can no longer click it. But I don't really know if that's what you want.

jButtonObject.setEnabled(false);


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Glitch...

Posted 22 December 2009 - 03:01 PM

Not a good idea to add a JComponent in an ActionListener
Better to add it in your constructor and make it setVisible(false);
in the actionPerformed() method just set its text (if required) and setVisible(true);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1