Results 1 to 20 of 30
Thread: GUI problem
- 03-29-2009, 11:59 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
GUI problem
Help! I'm making a graphical user interface and here's the first version of my code:
But the problem is, it only shows the two buttons "beta" and "sigma". I want all the AWT elements to be displayed on the screen. What should I do?Java Code:import java.io.*; import java.awt.*; import java.awt.event.*; public class MainProgram { Frame phi; Button beta, sigma; Choice tau; TextField alpha1, alpha2, alpha3, nu; Panel pi, pi2; public static void main (String args[]) throws IOException { MainProgram main = new MainProgram(); main.addComponents(); } public MainProgram() { phi = new Frame("MKDL Travel Agency System v1.0"); } public void addComponents() { File data = new File ("report.txt"); int q = 0; TravellingPackage prog[] = new TravellingPackage[10]; phi = new Frame("MKDL Travel Agency System v1.0"); beta = new Button("Calculate Fare"); sigma = new Button("Save Data"); tau = new Choice(); alpha1 = new TextField("Below 2",2); alpha2 = new TextField("2-16",2); alpha3 = new TextField("Above 16",2); nu = new TextField("Agent Name",50); pi = new Panel(); pi2 = new Panel(); phi.setSize(512,384); phi.setBackground(Color.gray); tau.add("Bus"); tau.add("Cruise"); tau.add("Airplane"); pi.add(tau); pi.add(alpha1); pi.add(alpha2); pi.add(alpha3); pi.add(nu); pi2.add(beta); pi2.add(sigma); phi.add(pi); phi.add(pi2); phi.pack(); phi.setVisible(true); } }
- 03-29-2009, 12:03 PM #2
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
And also add an exit button function which uses the built-in exit button at the top right of the frame.
- 03-30-2009, 12:38 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
How come no one's responding to my question???
- 03-30-2009, 02:02 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I don't know about the others but it's all Greek to me...
As well as sane variable names why don't you use Swing classes (JFrame, JPanel, etc)?
The reason you don't see a all of the components is that you are adding both pi and pi2 to phi. But phi has a border layout and this means that pi2 is replacing pi as the center component.
-
I have to second pbrockway2's suggestion to use Swing and not AWT. For one Swing is more powerful and robust than AWT. For another, there are more folks here who are familiar with Swing, and so Swing questions will be answered a lot quicker than AWT questions.
- 03-30-2009, 12:53 PM #6
I could have missed it, but I didn't see any layouts being added. Without a layout all the components will just stack on top of each other.
- 03-30-2009, 10:04 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
The components added to panels really are added because the panels have a flow layout.
The panels, however, replace one another when they are added to the frame (reply #4) because the frame has a border layout.
- 03-31-2009, 11:31 AM #8
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
New version of code.
Here's the new code:
Output: It only shows part of the title bar. When I resize it, only the panels are shown (no components insisde each of them).Java Code:import java.io.*; import java.awt.*; import java.awt.event.*; public class MainProgram { String name0x1; TravellingPackage prog[] = new TravellingPackage[10]; Frame phi; Button beta, sigma; Choice tau; TextField alpha1, alpha2, alpha3, nu, chi; Label theta1, theta2, theta3, theta3a, theta3b, theta3c, theta4; Panel pi, pi2, pi3, pi3a, pi3b, pi3c, pi4, pi5; public static void main (String args[]) throws IOException { MainProgram main = new MainProgram(); main.addComponents(); } public MainProgram() { phi = new Frame("MKDL Travel Agency System Ver. I"); } public void addComponents() { File data = new File ("report.txt"); int q = 0; phi = new Frame("MKDL Travel Agency System Ver. I"); phi.setLayout(null); phi.setSize(800,500); phi.setLocation(200,100); phi.setBackground(Color.gray); pi = new Panel(); pi2 = new Panel(); pi3 = new Panel(); pi3a = new Panel(); pi3b = new Panel(); pi3c = new Panel(); pi4 = new Panel(); pi5 = new Panel(); pi.setLayout(null); pi2.setLayout(null); pi3.setLayout(null); pi3a.setLayout(null); pi3b.setLayout(null); pi3c.setLayout(null); pi4.setLayout(null); pi5.setLayout(null); pi.setBackground(Color.blue); pi2.setBackground(Color.red); pi3.setBackground(Color.yellow); pi3a.setBackground(Color.yellow); pi3b.setBackground(Color.yellow); pi3c.setBackground(Color.yellow); pi4.setBackground(Color.magenta); pi5.setBackground(Color.green); pi.setSize(300,50); pi2.setSize(300,50); pi3.setSize(300,50); pi3a.setSize(300,50); pi3b.setSize(300,50); pi3c.setSize(300,50); pi4.setSize(300,50); pi5.setSize(300,50); pi.setLocation(50,50); pi2.setLocation(50,100); pi3.setLocation(50,150); pi3a.setLocation(50,200); pi3b.setLocation(50,250); pi3c.setLocation(50,300); pi4.setLocation(50,350); pi5.setLocation(50,400); beta = new Button("Calculate Fare"); sigma = new Button("Save Data"); tau = new Choice(); alpha1 = new TextField("",10); alpha2 = new TextField("",10); alpha3 = new TextField("",10); theta1 = new Label("Agent's Name"); theta2 = new Label("Travelling Method"); theta3 = new Label("Number of Tourists"); theta3a = new Label("Age Below 2"); theta3b = new Label("Age 2-16"); theta3c = new Label("Age Above 16"); theta4 = new Label("Total Fare:"); nu = new TextField("",30); chi = new TextField("",20); tau.add("Bus"); tau.add("Cruise"); tau.add("Airplane"); beta = new Button("Calculate Fare"); sigma = new Button("Save Data"); tau = new Choice(); alpha1.setLocation(0,0); alpha2.setLocation(0,0); alpha3.setLocation(0,0); theta1.setLocation(0,0); theta2.setLocation(0,0); theta3.setLocation(0,0); theta3a.setLocation(0,0); theta3b.setLocation(0,0); theta3c.setLocation(0,0); theta4.setLocation(0,0); nu.setLocation(0,0); chi.setLocation(0,0); beta.setLocation(0,0); sigma.setLocation(0,0); tau.setLocation(0,0); pi.add(theta1); pi.add(nu); pi2.add(theta2); pi2.add(tau); pi3.add(theta3); pi3a.add(theta3a); pi3a.add(alpha1); pi3b.add(theta3b); pi3b.add(alpha2); pi3c.add(theta3c); pi3c.add(alpha3); pi4.add(beta); pi4.add(sigma); pi5.add(theta4); pi5.add(chi); phi.add(pi); phi.add(pi2); phi.add(pi3); phi.add(pi3a); phi.add(pi3b); phi.add(pi3c); phi.add(pi4); phi.add(pi5); phi.pack(); phi.setVisible(true); } }
- 03-31-2009, 11:42 AM #9
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
so what would be the corrected code?
- 04-02-2009, 03:27 AM #10
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
New code
The only problem here is that only the panels are showing. No other components! And then it only shows a portion of the title bar. But how can I do this using AWT?Java Code:import java.io.*; import java.awt.*; import java.awt.event.*; public class MainProgram { String name0x1; TravellingPackage prog[] = new TravellingPackage[10]; Frame phi; Button beta, Choice tau; TextField alpha1, alpha2, alpha3, nu, chi; Label theta1, theta2, theta3, theta3a, theta3b, theta3c, theta4; Panel pi, pi2, pi3, pi3a, pi3b, pi3c, pi4, pi5; public static void main (String args[]) throws IOException { MainProgram main = new MainProgram(); main.addComponents(); } public MainProgram() { phi = new Frame("MKDL Travel Agency System Ver. I"); } public void addComponents() { File data = new File ("report.txt"); int q = 0; phi = new Frame("MKDL Travel Agency System Ver. I"); phi.setLayout(null); phi.setSize(800,500); phi.setLocation(200,100); phi.setBackground(Color.gray); pi = new Panel(); pi2 = new Panel(); pi3 = new Panel(); pi3a = new Panel(); pi3b = new Panel(); pi3c = new Panel(); pi4 = new Panel(); pi5 = new Panel(); pi.setLayout(null); pi2.setLayout(null); pi3.setLayout(null); pi3a.setLayout(null); pi3b.setLayout(null); pi3c.setLayout(null); pi4.setLayout(null); pi5.setLayout(null); pi.setBackground(Color.blue); pi2.setBackground(Color.red); pi3.setBackground(Color.yellow); pi3a.setBackground(Color.yellow); pi3b.setBackground(Color.yellow); pi3c.setBackground(Color.yellow); pi4.setBackground(Color.magenta); pi5.setBackground(Color.green); beta = new Button("Calculate Fare"); tau = new Choice(); alpha1 = new TextField("",10); alpha2 = new TextField("",10); alpha3 = new TextField("",10); theta1 = new Label("Agent's Name"); theta2 = new Label("Travelling Method"); theta3 = new Label("Number of Tourists"); theta3a = new Label("Age Below 2"); theta3b = new Label("Age 2-16"); theta3c = new Label("Age Above 16"); theta4 = new Label("Total Fare:"); nu = new TextField("",30); chi = new TextField("",20); tau.add("Bus"); tau.add("Cruise"); tau.add("Airplane"); beta = new Button("Calculate Fare"); tau = new Choice(); pi.add(theta1); pi.add(nu); pi2.add(theta2); pi2.add(tau); pi3.add(theta3); pi3a.add(theta3a); pi3a.add(alpha1); pi3b.add(theta3b); pi3b.add(alpha2); pi3c.add(theta3c); pi3c.add(alpha3); pi4.add(beta); pi5.add(theta4); pi5.add(chi); phi.add(pi); phi.add(pi2); phi.add(pi3); phi.add(pi3a); phi.add(pi3b); phi.add(pi3c); phi.add(pi4); phi.add(pi5); phi.pack(); phi.setVisible(true); } }
-
Read the Sun layout tutorial. You should avoid using null layout and direct placement of components. Instead use nested containers with appropriate layout managers for the best way to show your components.
Again, though you should change to Swing for better applications and for better chance of getting advice here.
- 04-02-2009, 05:01 AM #12
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
if i nulled the phi frame, it would show absolutely nothing but a gray frame. however, what's the best layout?
-
There is no one best layout, but I usually use a combination of layouts. My favorites are the BorderLayout, the GridLayout, BoxLayout and FlowLayout. I'll use the GridBagLayout if I have to but try to avoid using this.
- 04-02-2009, 05:29 AM #14
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
use setPreferredSize instead of setSize
- 04-03-2009, 06:03 AM #15
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
but i want a fixed size, but setPreferredSize does not work.
- 04-03-2009, 06:18 AM #16
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
use setPreferredSize with setResizable...
phi.setSize(800,500); seems fail to set the size to 800*500
- 04-03-2009, 07:26 AM #17
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Java Code:import java.io.*; import java.awt.*; import java.awt.event.*; public class MainProgram { String name0x1; TravellingPackage prog[] = new TravellingPackage[10]; Frame phi; Button beta; Choice tau; TextField alpha1, alpha2, alpha3, nu, chi; Label theta1, theta2, theta3, theta3a, theta3b, theta3c, theta4; Panel pi, pi2, pi3, pi3a, pi3b, pi3c, pi4, pi5; public static void main (String args[]) throws IOException { MainProgram main = new MainProgram(); main.addComponents(); } public MainProgram() { phi = new Frame("MKDL Travel Agency System Ver. I"); } public void addComponents() { File data = new File ("report.txt"); int q = 0; phi = new Frame("MKDL Travel Agency System Ver. I"); phi.setLayout(new FlowLayout()); phi.setPreferredSize(800,500);//still does not work!!! phi.setResizable(false); phi.setLocation(200,100); phi.setBackground(Color.gray); pi = new Panel(); pi2 = new Panel(); pi3 = new Panel(); pi3a = new Panel(); pi3b = new Panel(); pi3c = new Panel(); pi4 = new Panel(); pi5 = new Panel(); pi.setBackground(Color.blue); pi2.setBackground(Color.red); pi3.setBackground(Color.yellow); pi3a.setBackground(Color.yellow); pi3b.setBackground(Color.yellow); pi3c.setBackground(Color.yellow); pi4.setBackground(Color.magenta); pi5.setBackground(Color.green); beta = new Button("Calculate Fare"); tau = new Choice(); alpha1 = new TextField("",10); alpha2 = new TextField("",10); alpha3 = new TextField("",10); theta1 = new Label("Agent's Name"); theta2 = new Label("Travelling Method"); theta3 = new Label("Number of Tourists"); theta3a = new Label("Age Below 2"); theta3b = new Label("Age 2-16"); theta3c = new Label("Age Above 16"); theta4 = new Label("Total Fare:"); nu = new TextField("",30); chi = new TextField("",20); tau.add("Bus"); tau.add("Cruise"); tau.add("Airplane"); beta = new Button("Calculate Fare"); tau = new Choice(); pi.add(theta1); pi.add(nu); pi2.add(theta2); pi2.add(tau); pi3.add(theta3); pi3a.add(theta3a); pi3a.add(alpha1); pi3b.add(theta3b); pi3b.add(alpha2); pi3c.add(theta3c); pi3c.add(alpha3); pi4.add(beta); pi5.add(theta4); pi5.add(chi); phi.add(pi); phi.add(pi2); phi.add(pi3); phi.add(pi3a); phi.add(pi3b); phi.add(pi3c); phi.add(pi4); phi.add(pi5); phi.pack(); phi.setVisible(true); } }
- 04-03-2009, 07:50 AM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Does this compile? If not, and you can't understand the compiler message, post it.phi.setPreferredSize(800,500);//still does not work!!!
- 04-03-2009, 07:51 AM #19
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Message:
--------------------Configuration: <Default>--------------------
E:\mkdl\archive1\jp219\MainProgram.java:30: cannot resolve symbol
symbol : method setPreferredSize (int,int)
location: class java.awt.Frame
phi.setPreferredSize(800,500);
^
1 error
Process completed.
- 04-03-2009, 08:00 AM #20
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
setPreferredSize take Dimension object
should be
phi.setPreferredSize(new Dimension(800,500));
read JAVA API to know what input parameter needed


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks