|
-
February 15th, 2009, 05:02 PM
#1
why do the buttons not show?
I tried to create a simple GUI,but but my buttons and label dont show.I tried rearranging my lines of code in the launchFrame method but i couldnt get the correct result.I want to create a GUI with three buttons aligned ,a panel on top and a label under the buttons.Here are the lines of code i used
import java.awt.*;
public class FlowExample
{
private Panel p;
private Button b1;
private Button b2;
private Button b3;
private Label l;
private Frame f;
public FlowExample()
{
p = new Panel();
b1 = new Button(" Blue ");
b2 = new Button(" Red ");
b3 = new Button("Yellow");
l = new Label();
f = new Frame("Choose Color");
}
public void launchFrame()
{
p.setSize(500,50);
p.setBackground(Color.green);
f.add(p, BorderLayout.NORTH);
f.add(b1, BorderLayout.WEST);
f.add(b2, BorderLayout.CENTER);
f.add(b3, BorderLayout.EAST);
f.add(l, BorderLayout.SOUTH);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
FlowExample GUI = new FlowExample();
GUI.launchFrame();
}
}
-
February 15th, 2009, 06:58 PM
#2
Re: why do the buttons not show?
Use javax.swing.JFrame and other JComponets. AWT classes are a bit outdated anyway. Should you use those, top container in JFrame is the contentPane, set it or get it using setContentPane() or getContentPane().
-
February 16th, 2009, 04:51 AM
#3
Re: why do the buttons not show?
 Originally Posted by VolatileObject
Use javax.swing.JFrame and other JComponets. AWT classes are a bit outdated anyway. Should you use those, top container in JFrame is the contentPane, set it or get it using setContentPane() or getContentPane().
If you use JFrame, you don't need to add components to the content pane, you can add them directly to the JFrame.
As far as the OP goes, it doesn't seem sensible to create a panel, add it to the frame, but then add the other components to the frame instead of the panel. if you're going to do this, why have the panel at all?
Also, it seems odd to add the components to the frame using BorderLayout constraints, but then to set the frame layout to null - why do that?
The purpose of computing is insight, not numbers...
R. Hamming
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 16th, 2009, 07:49 PM
#4
Re: why do the buttons not show?
It is because you set the borderlayout of the Frame to null. That effective erases all the components within the layout. Delete that one line and you see the buttons.
Delete it. It's magic.
I agree with the others... use the swing package... JFrame, JButton, JLabel, JPanel, etc... they're prettier .
-
February 17th, 2009, 05:23 PM
#5
Re: why do the buttons not show?
Thanks you guys,this is the best java site ever......i'll be back with more questions,but please remember not everyone is as good as you guys,some times its a bit difficult to understand some of the stuff you say,like i didnt even know there was a Swing package.........but thanks,i'll be back.
-
February 18th, 2009, 01:29 PM
#6
Re: why do the buttons not show?
i'll be back with more questions,but please remember not everyone is as good as you guys
If you knew as much as we do then we wouldn't be able to help you 
,some times its a bit difficult to understand some of the stuff you say
Don't worry, if we wrote every reply as if you knew nothing then we'd spend hours typing replies and you might get offended. If you don't understand a reply just ask for clarification, we are always happy to give more detail. Unless of course you could have easily found out the info for yourself by a simple google.
-
February 18th, 2009, 07:07 PM
#7
Re: why do the buttons not show?
Thats why i think you guys are the best
-
February 19th, 2009, 04:43 PM
#8
Re: why do the buttons not show?
please how do i divide the frame into 3 vertical sections(columns),do i still use panels to do that?
and afterward, can i still add components to the different columns,i also know panels are used to create rows ,its just the columns i'm unsure of
-
February 19th, 2009, 05:23 PM
#9
Re: why do the buttons not show?
Yes you can use panels to represent 3 vertical sections. You need to choose an appropriate LayoutManager to get the layout effect you are after ie do all columns need to be the same width or different widths or should they size themselves according to the components in each of the panels
-
February 19th, 2009, 06:02 PM
#10
Re: why do the buttons not show?
they need to size them selves according to the components in each panel,in the first vertiacal panel i'll have two vertically arranged buttons ,in the second textfields and a textarea,inthe third two button like components with the symbol">> and <<" on them.the last panel is horizontal,that one i can manage,but how do i set the buttons to be in the middle of the panel?
-
February 19th, 2009, 06:04 PM
#11
Re: why do the buttons not show?
wat if i want the panels to be different sizes but the components to be fixed?
-
February 21st, 2009, 06:28 PM
#12
Re: why do the buttons not show?
 Originally Posted by seken1
they need to size them selves according to the components in each panel,in the first vertiacal panel i'll have two vertically arranged buttons ,in the second textfields and a textarea,inthe third two button like components with the symbol">> and <<" on them.the last panel is horizontal,that one i can manage,but how do i set the buttons to be in the middle of the panel?
Use the borderlayout manager. The first vertical panel would go in the west. The second would go in the center. The third would go in the east. The last horizontal panel would go in the south (or north).
Last edited by Nim; February 21st, 2009 at 06:33 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|