• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

gui clean up

Can anyone tell me how to place my gui components on this win form in some sort of order?


package com.softwareConstruction;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.*;

public class AssociateApplet extends Applet
   implements ActionListener {
   
    //Variable declarations
    private Frame win;
    private TextField mapInputText, domainInputText;
    private Label domainContent, mapContent;
    private static final String submitInput = "Submit input";
    private static final String bye  = " Exit ";
    private static final String addDomainValue  = " Add Value to Domain ";    
    private HashSet hashSet = new HashSet();
    private HashMap hashMap = new HashMap();
   
   public void actionPerformed ( ActionEvent e) {
      String cmd = e.getActionCommand();
      if ( cmd.equals( submitInput ) ) {
         
          //Use the associate class to add the object and key to the map
      }
      else if ( cmd.equals( addDomainValue ) ) {
          //Add the value to the domain
         
       }
      else if ( cmd.equals( bye ) ) {
         win.setVisible(false);
      }
   }

   public void init() {

      win = new Frame ( "Associative Application" );
     
      //Define a new TextField to handle the map value input
      this.mapInputText = new TextField("", 20);
      this.domainInputText = new TextField("", 20);
     
      //Add the text Field
      win.add(mapInputText);
      win.add(domainInputText);
     
      //Create a new window
      win.setLayout ( new FlowLayout() );
     
      //Add the submitInput and exit buttons to the window
      addButton ( submitInput ); addButton ( bye ); addButton(addDomainValue);
     
      //Add the label for the domain content
      domainContent = new Label( "Domain Content" );
      domainContent.setFont (new Font ( "Dialog", Font.BOLD, 14 ) );
      domainContent.setBackground ( Color.green );
      domainContent.setAlignment ( Label.CENTER );
     
      //Add the window that will show us what is in the domain
      win.add ( domainContent );
     
     
      //Add the label for the domain content
      mapContent = new Label( "Map Content" );
      mapContent.setFont (new Font ( "Dialog", Font.BOLD, 14 ) );
      mapContent.setBackground ( Color.green );
      mapContent.setAlignment ( Label.CENTER );
     
      //Add the window that will show us what is in the domain
      win.add ( mapContent );
     
     
      win.setSize ( 600, 500 );
   }

   public void start() {
         win.setVisible(true);
   }

   public void stop() {
         win.setVisible(false);
   }

   private void addButton ( String name ) {
      Button b = new Button ( name );
      b.setFont (new Font ( "Helvetica", Font.BOLD, 14 ) );
      b.addActionListener ( this );
      win.add ( b );
   }

   private void display(){
       
       
   }

}

0
jayrod
Asked:
jayrod
2 Solutions
 
aviadbdCommented:

The order of your gui components is determined by your LayoutManager. I see you're using the FlowLayout, so just read its docs here: http://java.sun.com/j2se/1.4.2/docs/api/index.html

You can also use a visual editor to place the gui components on your layout. If you use Eclipse (www.eclipse.org) you could use the VE plugin: http://www.eclipse.org/vep/

Good luck!
AviadBD.
0
 
pjcrooks2000Commented:
Yep Layout managers check them out from the Sun site as aviadbd says :)

pjcrooks2000
0
 
suprapto45Commented:
Dear jayrod,

Mmmm....so if I am not mistaken, you want to have some mechanism where you can place your GUI nicely. To do that, as aviadbd and pjcrooks2000 said, yes, you need to use Layout Manager. Let me explain you the concept.

Your win form (the applet that you have created) is a container where it can be divided freely by us. Well, the first easy and default LayoutManager is FlowLayout manager which is actually you used on your applet. LayoutManager has the use to organize your GUI components in the container (either your win form or Panel...we talked Panel later on). If you do not specify any layout manager then it takes FlowLayout as the default layout manager. FlowLayout will alwasy place the components (such as Button, TextField or etc) from the left to the right.

There are many types of LayoutManager such as GridLayout (row and column), BorderLayout (north, east, west and etc) and etc. Now, to create a sophisticated GUI arrangements, you need to have Panel which behaves exactly the same as your container (win form). Each Panel can have its own LayoutManager, it also can store the components such as buttons and etc. Now, what you need to do is to apply your win form with the LayoutManager and place each Panel (contains the GUI components) to your win form.

In below example, I show you how to use BorderLayout to modify your applet. Some components are placed on North, West and South of your win form.

/* Start here */
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.*;

public class MeowTest extends Applet
   implements ActionListener {
   
    //Variable declarations
    private Frame win;
    private Panel panel;
    private Panel panel2;
    private TextField mapInputText, domainInputText;
    private Label domainContent, mapContent;
    private static final String submitInput = "Submit input";
    private static final String bye  = " Exit ";
    private static final String addDomainValue  = " Add Value to Domain ";    
    private HashSet hashSet = new HashSet();
    private HashMap hashMap = new HashMap();
   
   public void actionPerformed ( ActionEvent e) {
      String cmd = e.getActionCommand();
      if ( cmd.equals( submitInput ) ) {
         
          //Use the associate class to add the object and key to the map
      }
      else if ( cmd.equals( addDomainValue ) ) {
          //Add the value to the domain
         
       }
      else if ( cmd.equals( bye ) ) {
         win.setVisible(false);
      }
   }

   public void init() {

      win = new Frame ( "Associative Application" );
      panel = new Panel();
      panel2 = new Panel();

      //Define a new TextField to handle the map value input
      this.mapInputText = new TextField("", 20);
      this.domainInputText = new TextField("", 20);
     
      //Add the text Field
      //win.add(mapInputText);
      //win.add(domainInputText);

      panel2.add (domainInputText);
      panel2.add (mapInputText);

      //Create a new window

      win.setLayout ( new BorderLayout() );
     
      //Add the submitInput and exit buttons to the window
      addButton ( submitInput );
      addButton ( bye );
      addButton(addDomainValue);
     
      win.add (panel, BorderLayout.WEST);
      win.add (panel2, BorderLayout.CENTER);
      //Add the label for the domain content
      domainContent = new Label( "Domain Content" );
      domainContent.setFont (new Font ( "Dialog", Font.BOLD, 14 ) );
      domainContent.setBackground ( Color.green );
      domainContent.setAlignment ( Label.CENTER );
     
      //Add the window that will show us what is in the domain
      win.add ( domainContent , BorderLayout.NORTH);
     
     
      //Add the label for the domain content
      mapContent = new Label( "Map Content" );
      mapContent.setFont (new Font ( "Dialog", Font.BOLD, 14 ) );
      mapContent.setBackground ( Color.green );
      mapContent.setAlignment ( Label.CENTER );
     
      //Add the window that will show us what is in the domain
      win.add ( mapContent , BorderLayout.SOUTH);
     
     
      win.setSize ( 600, 500 );
   }

   public void start() {
         win.setVisible(true);
   }

   public void stop() {
         win.setVisible(false);
   }

   private void addButton ( String name ) {
      Button b = new Button ( name );
      b.setFont (new Font ( "Helvetica", Font.BOLD, 14 ) );
      b.addActionListener ( this );
      panel.add ( b );
   }

   private void display(){
       
       
   }

}

/* End here */
0
 
jayrodAuthor Commented:
I've downloaded the vep and i'm using eclipse 3.0 and I can't get the stinking thing to work for me... Once I have it installed.. how do I know it's working? VEP that is?
0
 
aviadbdCommented:

You should have a new perspective called VE, and you should have new objects under the "New" window (Where you have "Class", "Interface", "Project" etc, you should have GUI items now).

If you don't have those, you probably didn't install it right.

AviadBD.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now