Results 1 to 13 of 13
Thread: Need help with JFrame and JPanel
- 04-22-2011, 09:41 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
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); } }
- 04-22-2011, 09:56 PM #2
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); } }
- 04-22-2011, 10:17 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
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
-
- 04-22-2011, 10:21 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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.
-
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!
- 04-22-2011, 10:47 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
so If I use BoarderLayout by default the gray line wont show?
can anyone explain the difference between BoarderLayout and BoarderLayout.JFrame?
Thanks
- 04-23-2011, 09:22 AM #8
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.
- 04-23-2011, 09:38 AM #9
Last edited by j2me64; 04-23-2011 at 09:50 AM.
- 04-23-2011, 06:15 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
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); } }
-
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.
- 04-23-2011, 06:24 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
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)
-
You may wish to read up on how to add components to a gui and how to use layout managers, and you can find this information here:
Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
How to Use Panels (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
Similar Threads
-
jpanel in jframe-pls help
By archanaanbu in forum Java AppletsReplies: 3Last Post: 02-14-2011, 05:01 AM -
Jpanel and Jframe help!!
By Java_n00b in forum AWT / SwingReplies: 4Last Post: 01-23-2011, 04:05 PM -
JFrame and JPanel
By khiat in forum New To JavaReplies: 6Last Post: 01-15-2011, 07:32 PM -
Help with JPanel and JFrame
By weikang in forum AWT / SwingReplies: 3Last Post: 11-26-2010, 01:22 AM -
JPanel/JFrame
By Mayur in forum New To JavaReplies: 2Last Post: 12-20-2009, 05:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks