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 4 of 4

Thread: GUI Help

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default GUI Help

    Hey,

    I am making a test for my first GUI program.

    Here is the code so far:

    package Tests;
     
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class GUITest  {
     
    	private static class Display extends JPanel  {
     
    		public void paintComponent(Graphics g)  {
     
    			super.paintComponent(g);
    			g.drawString("This is my first GUI Test.", 20, 30);
    			g.drawString("Hello World!", 20, 40);
     
    		}
     
    	}
     
    	private static class ButtonHandler implements ActionListener  {
     
    		public void actionPerformed(ActionEvent e) {
    			System.exit(0);
    		}
     
    	}
     
    	public static void main(String[] arg)  {
     
    		JButton button = new JButton("Click to close");
    		Display displayPanel = new Display();
    		ButtonHandler listener = new ButtonHandler();
    		button.addActionListener(listener);
     
    		JFrame window = new JFrame("GUI Test");
    		window.setContentPane(content);
                                window.setSize(300, 300);
    		window.setLocation(100, 100);
    		window.setVisible(true);
     
    		JPanel content = new JPanel();
    	    content.add(displayPanel, BorderLayout.CENTER);
    		content.add(button, BorderLayout.SOUTH);
    		content.setLayout(new BorderLayout());
     
     
     
     
     
    	}
     
    }

    In the main() routine, the content in window.setContentPane(content); is underlined and it says it can't find the variable content, but it is clearly there under the JPanel definition (JPanel content = new JPanel().

    Help?

    -Silent


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: GUI Help

    In the main() routine, the content in window.setContentPane(content); is underlined and it says it can't find the variable content, but it is clearly there under the JPanel definition (JPanel content = new JPanel()
    content has to be declared before you use it. The compiler won't "read ahead" for a declaration, and things would get very confusing if it tried.

    -----

    Why do you have static classes defined? If you have trouble answering that in terms of what a static class is, then my advice would be to remove those classes. Oracle's Tutorial includes a graphical "Hello world" program that you could use as a model. As that code notes the use of invokeLater() means that the GUI construction occurs on the event dispatch thread (not in the thread that main() runs in) which can help avoid problems later.

    That code occurs in the context of the Getting Started with Swing section.

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: GUI Help

    Thank you, I figured that out.

  4. #4
    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: GUI Help

    Well, a simple example
    System.out.println(x);
    int x = 5;
    Oh God, why it can't find x???? I can see x in the program.
    Compiler starts compiling from the first line to last.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford