Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default Need help with JFrame and JPanel

    alright so I'm trying to write a simple program. the program contains a window and it two buttons . if you click on the green button the background for the whole window changes to green and the same for the other button but it changes to orange. how do I move the two buttons from the top to the bottom of the window? and what do we use to change the background of the window?
    heres my code
    Java Code:
    
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    
    
    public class LabAssign9 extends JFrame{
    
        private JButton green;
        private JButton orange;
    
    
    
    
        public LabAssign9()
        {
    
            super("Colored Buttons");
            setLayout(new FlowLayout());
    
            green = new JButton("Green");
            add(green);
    
            green.setBackground(Color.green);
    
            setLayout(new FlowLayout());
    
            orange = new JButton("orange");
            add(orange);
    
            orange.setBackground(Color.orange);
            HandlerClass handler = new HandlerClass();
            green.addActionListener(handler);
            orange.addActionListener(handler);
            
    
    
    
        }
    
        private class HandlerClass implements ActionListener{
            public void actionPerformed(ActionEvent event){
    
            }
        }
        
        public static void main(String[] args) {
            LabAssign9 go = new LabAssign9();
            go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            go.setSize(425,375);
            go.setVisible(true);
    
        }
    
    
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class LabAssign9 extends JFrame implements ActionListener {
    	
    	private static final long serialVersionUID = 2236871408739129642L;
    	private JButton green;
    	private JButton orange;
    
    	public LabAssign9() {
    		super("Colored Buttons");
    		JPanel south = new JPanel();
    
    		green = new JButton("green");
    		south.add(green);
    
    		green.setBackground(Color.green);
    
    		orange = new JButton("orange");
    		south.add(orange);
    
    		orange.setBackground(Color.orange);
    		green.addActionListener(this);
    		orange.addActionListener(this);
    
    		getContentPane().add(south, BorderLayout.SOUTH);
    	}
    
    	public void actionPerformed(ActionEvent event) {
    		if (event.getActionCommand().equals("orange")) {
    			getContentPane().setBackground(Color.orange);
    		} else if (event.getActionCommand().equals("green")) {
    			getContentPane().setBackground(Color.green);
    		}
    	}
    
    	public static void main(String[] args) {
    		LabAssign9 go = new LabAssign9();
    		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		go.setSize(425, 375);
    		go.setVisible(true);
    
    	}
    }

  3. #3
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks soo much j2mw64. I was wondering about something. how can you change the color of the whole window? Including the area where the buttons are?
    and what am I suppose to use to do this: if you hit the green once the whole window goes green and if you hit green again the window goes back to its original color?
    Thanks again

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by j2me64 View Post
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    I have to express my disappointment for seeing spoon-feeding without offering any discussion or explanation. :(

  5. #5
    Martino is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default Possible solution.

    Hi,

    I've got a possible solution to your problem.

    I recommend you to use a layout manager. In your case, GridLayout would fit!

    First, you have to add your buttons in a panel. After that, in BorderLayout.SOUTH position, you have to add that panel to the Frame.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mikhail.abraham View Post
    Thanks soo much j2mw64. I was wondering about something. how can you change the color of the whole window? Including the area where the buttons are?
    and what am I suppose to use to do this: if you hit the green once the whole window goes green and if you hit green again the window goes back to its original color?
    Thanks again
    Yes, you can do this by setting the opaque property of all the JPanels that are sitting on the JFrame's contentPane to false. In other words, call

    setOpaque(false); on any JPanels that you've added to the GUI.

    The reason his code put your buttons on the bottom is his use of BorderLayout. JFrame contentPanes use BorderLayout by default, and you can read more about it to understand it better here: How to use BorderLayout

    Much luck!

  7. #7
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    so If I use BoarderLayout by default the gray line wont show?
    can anyone explain the difference between BoarderLayout and BoarderLayout.JFrame?
    Thanks

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    I have to express my disappointment for seeing spoon-feeding without offering any discussion or explanation.

    sorry, here is the explanation: the default borderlyaout for a JFrame is the BorderLayout which contains 5 areas. now, it you replace this with a FlowLayout all components in it will be placed from top left to bottom right. correcting the code i made a jpanel that has a default flowlayout and i placed the two buttons in it and then placed the jpanel in the south area of the jframe.

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by mikhail.abraham View Post
    so If I use BoarderLayout by default the gray line wont show?

    it's not a gray line but the south area (PAGE_END) that is shown in BorderLayout.

    Quote Originally Posted by mikhail.abraham View Post
    can anyone explain the difference between BoarderLayout and BoarderLayout.JFrame?

    the class BorderLayout has no such field called JFrame.
    Last edited by j2me64; 04-23-2011 at 09:50 AM.

  10. #10
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    ok so I think I'm on the right track but its not letting me run the program
    Java Code:
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class LabAssign9 extends JFrame implements ActionListener {
    
    	
    	private JButton green;
    	private JButton orange;
    
    	public LabAssign9() {
    		super("Colored Buttons");
    		FlowLayout experimentLayout = new FlowLayout();
    
    		green = new JButton("green");
    		experimentLayout.addLayoutComponent(null, green);
    		green.setBackground(Color.green);
    		orange = new JButton("orange");
    		experimentLayout.addLayoutComponent(null, orange);
    		orange.setBackground(Color.orange);
    		green.addActionListener(this);
    		orange.addActionListener(this);
    		getContentPane().add(null,FlowLayout.RIGHT);
    
    
    
    	}
    
    	public void actionPerformed(ActionEvent event) {
    		if (event.getActionCommand().equals("orange")) {
    			getContentPane().setBackground(Color.orange);
    
    		} else if (event.getActionCommand().equals("green")) {
    			getContentPane().setBackground(Color.green);
    		}
    	}
    
    	public static void main(String[] args) {
    		LabAssign9 go = new LabAssign9();
    		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		go.setSize(425, 375);
    		go.setVisible(true);
    
    	}
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If your code is not compiling or showing exceptions, please post the complete error message(s) and indicate which line(s) are the source of the problem.

  12. #12
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Here's what i'm getting
    Java Code:
    Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
            at java.awt.Container.addImpl(Container.java:1035)
            at java.awt.Container.add(Container.java:408)
            at LabAssign9.<init>(LabAssign9.java:26)
            at LabAssign9.main(LabAssign9.java:42)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. jpanel in jframe-pls help
    By archanaanbu in forum Java Applets
    Replies: 3
    Last Post: 02-14-2011, 05:01 AM
  2. Jpanel and Jframe help!!
    By Java_n00b in forum AWT / Swing
    Replies: 4
    Last Post: 01-23-2011, 04:05 PM
  3. JFrame and JPanel
    By khiat in forum New To Java
    Replies: 6
    Last Post: 01-15-2011, 07:32 PM
  4. Help with JPanel and JFrame
    By weikang in forum AWT / Swing
    Replies: 3
    Last Post: 11-26-2010, 01:22 AM
  5. JPanel/JFrame
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 05:07 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
  •