Results 1 to 4 of 4

Thread: GUI problems

  1. #1
    3man75 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default GUI problems

    So some questions..

    1. What is JFrame, Jpanel, JLabel, ActionListener, and ImageIcon?
    2. How do they 'work' together?
    3. When I make a GUI does it use the frame or panel to make a window appear?
    4. I had made a seprate thread but it seems that whenever I post the post does not appear. Is there some kind of limit of post per thread?


    Below is some code I made and have been trying to fanangle with for some time. I just cannot get my GUI to show me images when I click on the JButtons. I have the buttons showing and I know I need some kind of actionlistener to 'hear' the buttons' but I'm not sure how to make images appear when a button is clicked.

    I'm so sorry for double posting I'm just so lost and frustrated and I just don'w know what to do. I've been on and off of programming for a long time. I get really motivated some times and do well but alot of other times I just don't know what to do, nothing works, and I just lose steam. I know i'm smart (sometimes) but I just don't know how to read java docs very well and they seem so advanced. I've tried YT videos but sometimes I do the exact same thing and the IDE won't build them.

    I really don't know what to do so some advice would be nice.

    Java Code:
    public class main {
    
    	public static void main(String[] args) {
    	
    		GUI GUI = new GUI();
    	}
    }
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    
    public class GUI {
    	
    	private JButton buttonOne;
    	private JButton buttonTwo;
    	private JButton buttonThree;
    	private JPanel panel;
    	
    	public GUI () {
    		JFrame frame = new JFrame();
    		
    		frame.setTitle("The Begining");
    		frame.setVisible(true);
    		frame.setSize(500, 500);
    		frame.setLocation(0, 0);
    		
    		panel = new JPanel();
    		panel.setBackground(Color.black);
    		
    		
    		buttonOne = new JButton("1 - Personal BedRoom");//what the buttons will say.
    		buttonTwo = new JButton("2 - Two Person Bedroom");
    		buttonThree = new JButton("3 - Group Suite");
    		
    		panel.add(buttonOne);//buttons and panels
    		panel.add(buttonTwo);
    		panel.add(buttonThree);
    		frame.add(panel);
    		
    		thehandler handler = new thehandler(); // handler object is initiated here.
    		buttonOne.addActionListener(handler);
    		buttonTwo.addActionListener(handler);
    		buttonThree.addActionListener(handler);
    		
    		ImageIcon image = new ImageIcon("C:\\Users\\Javier\\Desktop\\Pictures for Projects\\Viking.JPG");
            JLabel imageLabel = new JLabel(image); 
            panel.add(imageLabel);
    	
    	}
    	
    	private class thehandler implements ActionListener{
    		public void actionPerformed (ActionEvent event){
    			//C:\Users\Javier\Desktop\Pictures for Projects
    	
    		}
    	}
    	
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: GUI problems

    The JFrame is your window (indeed it extends, ultimately, a class called Window).
    It has a content pane inside it, which is usually a JPanel, though it can be any Container.

    The JPanel is where you will normally build your GUI, though there are others (eg JScrollPane, or JSplitPane). It's the one that you give a Layout to, and handles all that side of things.
    Stuff like JLabels/JButtons etc are the things you put in a panel.

    Think of your GUI as a set of containers, often within containers, that hold widgets.

    Many of those widgets do things, and those things can be "listened in on" by attaching various listeners to them, like an ActionListener on a JButton. When the button is clicked it will call each ActionListener that is attached to it.

    An ImageIcon is just the GUi class for holding images to be displayed inside certain widgets (like a JLabel or button).

    As for your code, don't set the frame visible until you've finished building it.
    It should be the last thing you do in that constructor.
    You might also want to call frame,pack() before that, which tries to ensure all the bits on the frame are sized and laid out correctly.

    I would also not use a shared ActionListener.
    You can do something like this (in Java 8):
    Java Code:
    buttonOne.addActionListener();
    buttonOne.addActionListener((e) -> showImage("Some Location"));
    And then have a showImage method that simply handles the String passed in as if it were a file location.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,172
    Rep Power
    12

    Default Re: GUI problems

    These are mostly explained in the tutorials linked in my signature. Check them out.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    3man75 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default Re: GUI problems

    I still don't know what to do..

    Should I start over?

    EDIT: Also some progress I think I did

    Java Code:
    thehandler handler = new thehandler(); // handler object is initiated here.
    		buttonOne.addActionListener(handler);
    	//	buttonTwo.addActionListener(handler);
    	//	buttonThree.addActionListener(handler);
    	}
    	
    	private class thehandler implements ActionListener{
    		public void actionPerformed (ActionEvent event){
    			label1.setText("Fire");
    	
    		}
    	}
    https://docs.oracle.com/javase/tutor...nts/label.html <----------JLabel explained

    Since JLabel allows me to settext and images I thought of putting that into the actionlistener. THe idea being that when button1/Personal Room is clicked that the text "Fire" will show itself.

    No dice. Again.

    Damnation.
    Last edited by 3man75; 05-18-2017 at 03:07 AM.

Similar Threads

  1. Two problems
    By frobiz176 in forum New To Java
    Replies: 2
    Last Post: 09-25-2013, 06:16 AM
  2. GUI problems on OSX
    By Melodia in forum New To Java
    Replies: 15
    Last Post: 09-12-2013, 07:33 PM
  3. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  4. Problems here
    By Keno777 in forum New To Java
    Replies: 2
    Last Post: 11-13-2009, 11:35 AM
  5. Problems in JSP : Need help
    By raj4u in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-07-2008, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •