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: Funny output

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Funny output

    For learning purposes, I made a driver program for a
    Counter class. All it does is increments and decrements
    by integer. I decided to turn the driver into a GUI. But,
    I'm having output problems. For some reason, the first
    time I run the program, everything is fine, but when I
    run it again and again, most of the time the components
    (label and buttons) are invisible until I scroll my mouse
    over them. It intermittently works fine. Everything looks
    good to me, so I have no idea why everything disappears
    when I run it, but sometimes doesn't.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    // class extends JFrame to make a GUI and creates a Counter
    // object from the Counter class
    public class Lab2 extends JFrame {
     
    	private Counter aCounter = new Counter(); // counter object used by this class
     
    	private JLabel counterLabel = new JLabel("0"); // label that displays the count
     
    	private JButton incrementButton = new JButton("+"); // button to incrememnt the count
    	private JButton decrementButton = new JButton("-"); // button to decrememnt the count
    	private JButton resetButton     = new JButton("Reset"); // button to set the count to 0
     
    	                                            // action listener objects to pass through 
    															  // addActionListener methods of each Button
    	private IncrementButtonHandler ibHandler = new IncrementButtonHandler();
    	private DecrementButtonHandler dbHandler = new DecrementButtonHandler();
    	private ResetButtonHandler     rbHandler = new ResetButtonHandler();
     
    	private Container c = getContentPane(); // container to hold contents of the content pane
     
    	private Dimension size; // dimension to hold the preferred size of each component
     
    	private Font labelFont = new Font(Font.SANS_SERIF, Font.BOLD, 100); // label font object
    	private Font buttonFont = new Font(Font.MONOSPACED, Font.BOLD, 25); // + and - font object
     
    	private static final int WIDTH  = 140; // width of the window
       private static final int HEIGHT = 205; // height of the window
     
    	// default constructor that calls methods to set up the frame
    	public Lab2()
    	{
    		setFrameProperties();
    		setComponentProperties();
    		addComponents();
    		addListeners();		
    	}
     
    	// method to set the properties of the components
    	public void setComponentProperties()
    	{
    		counterLabel.setFont(labelFont);
    		size = counterLabel.getPreferredSize();
    		counterLabel.setBounds(40, 10, size.width, size.height);
     
    		decrementButton.setFont(buttonFont);
    		size = decrementButton.getPreferredSize();
    		decrementButton.setBounds(10, 125, size.width, size.height);
     
    		incrementButton.setFont(buttonFont);
    		size = incrementButton.getPreferredSize();
    		incrementButton.setBounds(75, 125, size.width, size.height);
     
    		size = resetButton.getPreferredSize();
    		resetButton.setBounds(35, 5, size.width, size.height);
    	}
     
    	// method to initialize the frame properties
    	public void setFrameProperties()
    	{	
    		setVisible(true);
    		setTitle("Counter");
    		setSize(WIDTH, HEIGHT);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setResizable(false);
    		setLocationRelativeTo(null);
    	}
     
    	// method to add the components in the content pane
    	public void addComponents()
    	{	
    		c.setLayout(null);
    		c.add(counterLabel);
    		c.add(incrementButton);
    		c.add(decrementButton);
    		c.add(resetButton);
    	}
     
    	// method to add the listeners to the buttons
    	public void addListeners() 
    	{
    		incrementButton.addActionListener(ibHandler);
    		decrementButton.addActionListener(dbHandler);
    		resetButton.addActionListener(rbHandler);
    	}
     
    	// action listener for the incrementButton
    	public class IncrementButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e) 
    		{	
    			aCounter.incrementCount();
     
    			counterLabel.setText(Integer.toString(aCounter.getCount()));
     
    			size = counterLabel.getPreferredSize();
     
    			if (aCounter.getCount() < 10)
    				counterLabel.setBounds(40, 10, size.width, size.height);
    			else 
    				counterLabel.setBounds(10, 10, size.width, size.height);
    		}
    	}
     
    	// action listener for the decrementButton
    	public class DecrementButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			aCounter.decrementCount();
     
    			counterLabel.setText(Integer.toString(aCounter.getCount()));
     
    			size = counterLabel.getPreferredSize();
     
    			if (aCounter.getCount() < 10)
    				counterLabel.setBounds(40, 10, size.width, size.height);
    			else 
    				counterLabel.setBounds(10, 10, size.width, size.height);
    		}	
    	}
     
    	// action listener for the resetButton
    	public class ResetButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e) 
    		{	
    			aCounter.resetCount();
    			counterLabel.setText(Integer.toString(aCounter.getCount()));
     
    			counterLabel.setBounds(40, 10, size.width, size.height);
    		}	
    	}
     
    	// main method instantiates the GUI
    	public static void main(String[] args)
    	{
    		Lab2 lab2GUI = new Lab2();
    	}
    }

    Here is the Counter class

    public class Counter
    {
     
    	private int count = 0; // instance variable that holds the count 
     
    	// mutator methods
     
    	// sets the count to zero
    	public void resetCount()
    	{
    		count = 0;
    	}
     
    	// increases the count by 1
    	public void incrementCount()
    	{
    		++count;
    	}
     
    	// decreases the count by 1 but not below zero
    	public void decrementCount()
    	{
    		if (count > 0)
    			--count;
    	}
     
    	// accessor methods
     
    	// returns the current count of the object
    	public int getCount()
    	{
    		return count;
    	}
     
    	// prints the count
    	public void printCount()
    	{
    		System.out.print(count);
    	}
    }
    Last edited by Leiferson; September 16th, 2011 at 07:05 PM. Reason: added the Counter class


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Funny output

    You are better off not adding components after a container (eg your JFrame) is realized (eg is visible). In other words, move you setVisible(true) call to after the component additions and layout changes, and then call pack() just prior to setVisible.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Leiferson (September 16th, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Funny output

    Worked like a charm. Now that makes sense. It fooled me how it was doing it intermittently though. Also, calling pack scrunched my
    window up too small to see any of my components. It's not causing me any problems anymore though, thanks.

Similar Threads

  1. Funny computer and office related cartoons
    By Flash in forum The Cafe
    Replies: 6
    Last Post: April 9th, 2010, 04:13 AM
  2. Funny business with JFrame, JPanel and JLabel
    By JeffC in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2010, 12:26 PM
  3. Funny NullPointerException
    By Marcus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2009, 09:14 AM
  4. Robo Code - The funny Java Programming Game
    By Freaky Chris in forum The Cafe
    Replies: 20
    Last Post: October 8th, 2009, 03:42 PM
  5. How to make java based animation?
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: June 7th, 2008, 06:55 AM