Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: GUI problem

  1. #1
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default GUI problem

    Help! I'm making a graphical user interface and here's the first version of my code:

    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);
    	}
    	
    }
    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?

  2. #2
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    And also add an exit button function which uses the built-in exit button at the top right of the frame.

  3. #3
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    How come no one's responding to my question???

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by kevzspeare View Post
    How come no one's responding to my question???
    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.

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

    Default

    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.

  6. #6
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    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.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  8. #8
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default New version of code.

    Here's the new code:

    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);
    	}
    	
    }
    Output: It only shows part of the title bar. When I resize it, only the panels are shown (no components insisde each of them).

  9. #9
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    so what would be the corrected code?

  10. #10
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default New code

    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);
    	}
    	
    }
    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?

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

    Default

    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.

  12. #12
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    if i nulled the phi frame, it would show absolutely nothing but a gray frame. however, what's the best layout?

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

    Default

    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.

  14. #14
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  15. #15
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    but i want a fixed size, but setPreferredSize does not work.

  16. #16
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    use setPreferredSize with setResizable...

    phi.setSize(800,500); seems fail to set the size to 800*500

  17. #17
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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);
    	}
    	
    }

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    phi.setPreferredSize(800,500);//still does not work!!!
    Does this compile? If not, and you can't understand the compiler message, post it.

  19. #19
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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.

  20. #20
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    setPreferredSize take Dimension object
    should be

    phi.setPreferredSize(new Dimension(800,500));

    read JAVA API to know what input parameter needed

Page 1 of 2 12 LastLast

Posting Permissions

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