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

Thread: Problems with GUI, seems simple fix but I can't figure it out

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Problems with GUI, seems simple fix but I can't figure it out

    Heres my code:

    //Thomas Harrald
    //IT215 Checkpoint Inventory Program
    //Camera Inventory program
    import java.util.Arrays;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import javax.swing.*;
    import java.awt.*;
    import java.awt;
    import java.awt.event.*;
     
     
    // Stores and then gets info on a Camera
    public class CameraProgram5 extends JFrame
    {
    	private JTextArea txt;
    	private Inventory inv;
    	private int current = 0;
    	private JButton next;
     
    	CameraProgram5 gui = new CameraProgram5();
    	gui.pack();
    	gui.setVisible(true);
     
    	public static void main(String args[])
    	{
     
     
     
    		super("Camera Shack");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		CameraClass[] cameras;
     
    		CameraClass nikonCamera= new CameraClass( 3100, "Nikon", "Electronics", 5, 10.00);
    		CameraClass sonyCamera = new CameraClass( 4500, "Sony", "Electronics", 10, 300.00);
    		CameraClass canonCamera= new CameraClass( 1550, "Canon", "Electronics", 3, 40.00);
     
    		cameras = new CameraClass[ 3 ];
     
    		cameras[ 0 ] = nikonCamera;
    		cameras[ 1 ] = sonyCamera;
    		cameras[ 2 ] = canonCamera;
     
     
    		inv = new Inventory();
    		inv.add(nikonCamera);
    		inv.add(sonyCamera);
    		inv.add(canonCamera);
     
     
     
    		// sort
    		inv.sort();
     
     
    		//output them.
    		for (int i = 0; i < inv.size(); i++) {
    			System.out.println(inv.get(i));
    			System.out.println();
    		}
     
    		System.out.println("Total value: $" + String.format("%.2f",inv.value()));
     
    		// gui stuff
    		JPanel panel = new JPanel();
    		txt = new JTextArea(15,20);
    		txt.setEditable(false);//user shouldn't change it
    		panel.add(txt);
     
    		JPanel buttonpanel = new JPanel();
    		buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));
     
    		JButton first = new JButton("First");
    		first.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				current = 0;// go to the beginning
    				showCamera();
    			}
    		});
    		buttonpanel.add(first);
    		JButton previous = new JButton("Previous");
    		previous.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				if (current > 0) current--;
    				else current = inv.size()-1;
    				showCamera();
    			}
    		});
    		buttonpanel.add(previous);
    		JButton next = new JButton("Next");
    		next.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				if (current < inv.size()-1) current++; //advance to the end
    				else current = 0;
    				showCamera();
    			}
    		});
    		buttonpanel.add(next);
    		JButton last = new JButton("Last");
    		last.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				current = inv.size()-1;
    				showCamera();
    			}
    		});
    		buttonpanel.add(last);
     
    		buttonpanel.add(new Logo());
     
    		panel.add(buttonpanel);
     
    		getContentPane().add(panel);
     
    		showCamera();
     
    	}
     
    	public void showCamera() 
    	{
    		txt.setText("Camera Details:\n");
    		txt.append(inv.get(current) + "\n");
     
     
    		txt.append("Total value: $" + String.format("%.2f",inv.value()));
     
     
    	}
     
    	class Logo extends JPanel {
    		public Logo() {
    			super();
    			setPreferredSize(new Dimension(200,200));
    		}
    		public void paint(Graphics g) {
    			g.setColor(Color.white);
    			g.fillRoundRect(60,60,110,90,5,5);
    			g.setColor(Color.black);
    			g.drawString("Camera Shack", 70, 105);
    		}
    	}
    }
    I get three errors,

    identifier expected: gui.pack();
    identifier expected: gui.setVisible(true);
    Illegal start of expression: gui.setVisible(true);

    I have tried rereading the readings, and importing specifically java.awt since that is where pack() is. As I'm writing this, I think the problem might be in the first part of that ( CameraProgram5 gui = new CameraProgram5(); )

    Any help would be greatly appreciated!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problems with GUI, seems simple fix but I can't figure it out

    The lines of code:
    gui.pack();
    gui.setVisible(true);
    ...those are just floating around inside your class, outside of any method.
    You can declare and define variables in that area, but not "run code" (so to speak). Tuck them away neatly within a method where they will be executed.

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

    Harrald (August 31st, 2012)

  4. #3
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Problems with GUI, seems simple fix but I can't figure it out

    Quote Originally Posted by jps View Post
    The lines of code:
    gui.pack();
    gui.setVisible(true);
    ...those are just floating around inside your class, outside of any method.
    You can declare and define variables in that area, but not "run code" (so to speak). Tuck them away neatly within a method where they will be executed.
    Thats what I was afraid of, because when I put it in the main method, i get 35 errors. most of them centering around referencing non static variables; inv, current, showCamera, a few of the get(). I know static methods are methods in which you don't have to declare the class, but how does that apply to my methods?

  5. #4
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Problems with GUI, seems simple fix but I can't figure it out

    New code: Works perfectly

    //Thomas Harrald
    //IT215 Checkpoint Inventory Program
    //Camera Inventory program
    import java.util.Arrays;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    // Stores and then gets info on a Camera
    public class CameraProgram5 extends JFrame
    {
    	private JTextArea txt;
    	private Inventory inv;
    	private JButton next;
    	int current = 0;
     
     
    	public static void main(String args[])
    	{
     
    		CameraProgram5 gui = new CameraProgram5();
    		gui.pack();
    		gui.setVisible(true);
    	}	
    	public CameraProgram5()
    	{
    		super("Camera Shack");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		Camera[] cameras;
     
    		Camera nikonCamera= new Camera( 3100, "Nikon", "Electronics", 5, 10.00);
    		Camera sonyCamera = new Camera( 4500, "Sony", "Electronics", 10, 300.00);
    		Camera canonCamera= new Camera( 1550, "Canon", "Electronics", 3, 40.00);
     
    		cameras = new Camera[ 3 ];
     
    		cameras[ 0 ] = nikonCamera;
    		cameras[ 1 ] = sonyCamera;
    		cameras[ 2 ] = canonCamera;
     
     
    		inv = new Inventory();
    		inv.add(nikonCamera);
    		inv.add(sonyCamera);
    		inv.add(canonCamera);
     
     
     
    		// sort
    		inv.sort();
     
     
    		//output them.
    		for (int i = 0; i < inv.size(); i++) {
    			System.out.println(inv.get(i));
    			System.out.println();
    		}
     
    		System.out.println("Total value: $" + String.format("%.2f",inv.value()));
     
    		// gui stuff
    		JPanel panel = new JPanel();
    		txt = new JTextArea(15,20);
    		txt.setEditable(false);//user shouldn't change it
    		panel.add(txt);
     
    		JPanel buttonpanel = new JPanel();
    		buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));
     
    		JButton first = new JButton("First");
    		first.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				current = 0;// go to the beginning
    				showCamera();
    			}
    		});
    		buttonpanel.add(first);
     
    		JButton previous = new JButton("Previous");
    		previous.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				if (current > 0) current--;
    				else current = inv.size()-1;
    				showCamera();
    			}
    		});
    		buttonpanel.add(previous);
     
    		JButton next = new JButton("Next");
    		next.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e)
    			 {
    				if (current < inv.size()-1) current++; //advance to the end
    				else current = 0;
    				showCamera();
    			}
    		});
     
    		buttonpanel.add(next);
    		JButton last = new JButton("Last");
    		last.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				current = inv.size()-1;
    				showCamera();
    			}
    		});
     
    		buttonpanel.add(last);
     
    		buttonpanel.add(new Logo());
     
    		panel.add(buttonpanel);
     
    		getContentPane().add(panel);
     
    		showCamera();
     
    	}
     
    	public void showCamera() 
    	{
    		txt.setText("Camera Details:\n");
    		txt.append(inv.get(current) + "\n");
     
     
    		txt.append("Total value: $" + String.format("%.2f",inv.value()));
     
     
    	}
     
    	class Logo extends JPanel {
    		public Logo() {
    			super();
    			setPreferredSize(new Dimension(200,200));
    		}
    		public void paint(Graphics g) {
    			g.setColor(Color.blue);
    			g.fillRoundRect(60,60,110,90,5,5);
    			g.setColor(Color.white);
    			g.drawString("Camera Shack", 70, 105);
    		}
    	}
    }

    InventoryC() was originally saved as Inventory(), I made InventoryC() and changed the declaration in the code to reflect that, but for some reason it kept trying to find Inventory()... So I ended up saving over the old Inventory() with inventoryC().

    Also, I didn't construct an object of CameraProgram5 lol... so that made it difficult for all the methods to reference it, giving me all of those non-static variable and method errors.

  6. #5
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Problems with GUI, seems simple fix but I can't figure it out

    jps you are awesome, thanks for not spoon feeding me and just giving me hints on which direction, it really helps a lot and I feel like I learn quite a bit as a result.

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problems with GUI, seems simple fix but I can't figure it out

    You are welcome and thanks for the kind words.

Similar Threads

  1. Please help i cant figure it out - problems with Location and Line
    By tuts73 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 08:58 PM
  2. Defining a rational class. Simple but i cant figure it out to save my life
    By spetillo3 in forum Object Oriented Programming
    Replies: 3
    Last Post: October 26th, 2011, 11:02 PM
  3. [SOLVED] Simple error that I can't figure out how to fix
    By javapenguin in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 11th, 2011, 07:27 AM
  4. Simple error can't figure out.
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 30th, 2010, 12:19 PM
  5. simple problem w/ appelets which i cant figure out
    By JavaGreg in forum Java Applets
    Replies: 7
    Last Post: August 15th, 2009, 07:22 PM