Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why doesn't this work!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Why doesn't this work!

    My listeners in this program aren't working and I'm getting some errors that I don't understand.Someone please help!(I have to classes one is named LottoMadness and the other one is called LottoEvent.)

    import java.awt.*;
    import javax.swing.*;
     
    public class LottoMadness extends JFrame
     
      {
    	LottoEvent lotto = new LottoEvent(this);
      //set up row 1
      JPanel row1 = new JPanel();
      ButtonGroup option = new ButtonGroup();
      JCheckBox quickpick = new JCheckBox("Quick Pick", false);
      JCheckBox personal = new JCheckBox("Personal", true);
     
      //set up row2
      JPanel row2 = new JPanel();
      JLabel numbersLabel = new JLabel("Your picks: ", JLabel.RIGHT);
      JTextField[] numbers = new JTextField[6];
      JLabel winnersLabel = new JLabel("winners: ", JLabel.RIGHT);
      JTextField[] winners = new JTextField[6];
     
      //set up row 3
      JPanel row3 = new JPanel();
      JButton stop = new JButton("Stop");
      JButton play = new JButton("Play");
      JButton reset = new JButton("Reset");
     
      //set up row 4
      JPanel row4 = new JPanel();
      JLabel got3Label = new JLabel("3 of 6: ", JLabel.RIGHT);
      JTextField got3 = new JTextField("0");
      JLabel got4Label = new JLabel("4 of 6: ", JLabel.RIGHT);
      JTextField got4 = new JTextField("0");
      JLabel got5Label = new JLabel("5 of 6: ", JLabel.RIGHT);
      JTextField got5 = new JTextField("0");
      JLabel got6Label = new JLabel("6 of 6: ", JLabel.RIGHT);
      JTextField got6 = new JTextField("0", 10);
     
      JLabel drawingsLabel = new JLabel("Drawings: ", JLabel.RIGHT);
      JTextField drawings = new JTextField("0");
      JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT);
      JTextField years = new JTextField("");
     
      public LottoMadness()
        {
    	super("Lotto Madness");
    	setSize(550, 270);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	GridLayout layout = new GridLayout(5, 1, 10, 10);
    	setLayout(layout);
     
    	// Add listeners
    	quickpick.addItemListener(lotto);
    	personal.addItemListener(lotto);
    	stop.addItemListener(lotto);
    	play.addActionListener(lotto);
    	reset.addActionListener(lotto);
     
    	FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
     
    	option.add(quickpick);
    	option.add(personal);
    	row1.setLayout(layout1);
    	row1.add(quickpick);
    	row1.add(personal);
    	add(row1);
     
    	GridLayout layout2 = new GridLayout(2, 7,10, 10);
    	row2.setLayout(layout2);
    	row2.add(numbersLabel);
    	for(int i = 0; i < 6; i++)
    	  {
    	    numbers[i] = new JTextField();
    	    row2.add(numbers[i]);
    	  } 
     
    	row2.add(winnersLabel);
    	for(int i =0; i < 6; i++)
    	  {
    	   winners[i] = new JTextField();
    	   winners[i].setEditable(false);
    	   row2.add(winners[i]);
    	  }
    	add(row2);
     
    	FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10,10);
    	row3.setLayout(layout3);
    	stop.setEnabled(false);
    	row3.add(stop);
    	row3.add(play);
    	row3.add(reset);
    	add(row3);
     
    	GridLayout layout4 = new GridLayout(2, 3, 20, 10);
    	row4.setLayout(layout4);
    	row4.add(got3Label);
    	got3.setEditable(false);
    	row4.add(got3);
    	row4.add(got4Label);
    	got4.setEditable(false);
    	row4.add(got4);
    	row4.add(got5Label);
    	got5.setEditable(false);
    	row4.add(got5);
    	row4.add(got6Label);
    	got6.setEditable(false);
    	row4.add(got6);
    	row4.add(drawingsLabel);
    	drawings.setEditable(false);
    	row4.add(drawings);
    	row4.add(yearsLabel);
    	years.setEditable(false);
    	row4.add(years);
    	add(row4);
     
    	setVisible(true);
     
        }
     
      public static void main(String args[])
        {
    	  LottoMadness frame = new LottoMadness();
        }
     
      }



    import javax.swing.*;
    import java.awt.event.*;
     
    public class LottoEvent implements ItemListener, ActionListener, Runnable
      {
      LottoMadness gui;
      Thread playing;
     
      public LottoEvent(LottoMadness in)
        {
    	 gui = in;  
        }
     
      public void actionPerformed(Action e)
        {
    	String command = ((AbstractButton) e).getActionCommand();
     
    	if(command.equals("play"))
    	  {
    	  startPlaying();	
    	  }
     
    	if(command.equals("Stop"))
    	  {
          stopPlaying();
    	  }
     
    	if(command.equals("Reset"))
    	  {
    	  clearAllFields();
    	  }
     
        }
     
      void startPlaying()
        {
    	  playing = new Thread(this); 
    	  playing.start();
    	  gui.play.setEnabled(false);
    	  gui.stop.setEnabled(true);
    	  gui.reset.setEnabled(false);
    	  gui.quickpick.setEnabled(false);
    	  gui.personal.setEnabled(false);
    	}
     
      void stopPlaying()
        {
    	  gui.stop.setEnabled(false);
    	  gui.play.setEnabled(true);
    	  gui.reset.setEnabled(true);
    	  gui.quickpick.setEnabled(true);
    	  gui.personal.setEnabled(true);
    	  playing = null;
        }
      void clearAllFields()
        {
    	  for(int i = 0; i < 6; i++)
    	    {
    	    gui.numbers[i].setText(null);
    	    gui.winners[i].setText(null);
    	    }
     
    	  gui.got3.setText("0");
    	  gui.got4.setText("0");
    	  gui.got5.setText("0");
    	  gui.got6.setText("0");
    	  gui.drawings.setText("0");
    	  gui.years.setText("0");
     
    	}
     
      public void itemStateChanged(ItemEvent event)
        {
    	Object item = event.getItem();
    	if(item == gui.quickpick)
    	  {
     
    	  for(int i = 0; i < 6; i++)
    	    {
    		int pick;  
    	    }
    	  int pick;
    	int i = 0;
    	do 
    	    {
    		pick = (int) Math.floor(Math.random() * 50 + 1);  
    	    }
    	  while (numberGone(pick, gui.numbers, i));
    	  gui.numbers[i].setText("" + pick);
     
    	  }
     
        }
     
     
     
     
      void addOneToField(JTextField field)
        {
    	int num = Integer.parseInt("0" + field.getText());
    	num++;
    	field.setText("" + num);
     
        }
     
      boolean numberGone(int num, JTextField[] pastNums, int count)
        {
    	for(int i = 0; i < count; i++)
    	  {
    	  if(Integer.parseInt(pastNums[i].getText()) == num)
    	    {
    		  return true;
    	    }
     
    	  }
    	 return false;
        }
     
      boolean matchedOne(JTextField win, JTextField[] allPicks)
        {
    	for(int i = 0; i < 6; i++)
    	  {
    	  String winText = win.getText();
    	  if( winText.equals(allPicks[i].getText() ) )
    	    {
    		return true;  
    	    }
     
    	  }
    	 return false;
        }
     
      public void run()
        {
    	Thread thisThread = Thread.currentThread();
    	while(playing == thisThread)
    	  {
    	  addOneToField(gui.drawings);
    	  int draw = Integer.parseInt(gui.drawings.getText());
    	  float numYears = (float)draw / 104;
    	  gui.years.setText("" + numYears);
     
    	  int matches = 0;
    	  for(int i = 0; i < 6; i ++)
    	    {
    		int ball;
    		do
    		  {
    		  ball = (int) Math.floor(Math.random() * 50 + 1);
    		  }
    		while(numberGone(ball, gui.winners, i));
    		gui.winners[i].setText("" + ball);
    		if (matchedOne(gui.winners[i], gui.numbers))
    		  {
    		  matches++;	
    		  }
    		switch(matches)
    		  {
    		case 3:
    			addOneToField(gui.got3);
    			break;
    		case 4:
    			addOneToField(gui.got4);
    			break;
    		case 5:
    			addOneToField(gui.got6);
    			break;
    		case 6:
    			addOneToField(gui.got6);
    			gui.stop.setEnabled(false);
    			gui.play.setEnabled(true);
    			playing = null;
    	 	  }
     
    		try
    		  {
    		  Thread.sleep(100);
    		  }
    		catch(InterruptedException e)
    		  {
    		  System.out.println("Error*");
    		  }
    	    }
    	  }
    	}
    Last edited by Alex-Green; February 20th, 2012 at 01:13 AM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Why doesn't this work!

    Well, as a matter of being more helpful to those trying to assist you, you should always post any information regarding your issue, such as your error message. However, I recognized the problem relatively quickly.... You never set your program to be focusable.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Why doesn't this work!

    You never set your program to be focusable.
    Care explaining it?

Similar Threads

  1. Does this work?
    By marmanq in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 20th, 2012, 10:51 AM
  2. I cant work it out!!!
    By tuts73 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: January 18th, 2012, 10:16 AM
  3. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM
  4. Do you work out?
    By thesolo in forum Totally Off Topic
    Replies: 4
    Last Post: October 24th, 2011, 08:03 AM
  5. Why does this not work?
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 09:47 AM

Tags for this Thread