Solved

gui clean up

Posted on 2004-09-11
5
193 Views
Last Modified: 2010-03-31
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
Comment
Question by:jayrod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Assisted Solution

by:aviadbd
aviadbd earned 100 total points
ID: 12033707

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
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12034004
Yep Layout managers check them out from the Sun site as aviadbd says :)

pjcrooks2000
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 400 total points
ID: 12035084
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
 
LVL 3

Author Comment

by:jayrod
ID: 12036818
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
 
LVL 3

Expert Comment

by:aviadbd
ID: 12037082

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 264
java imports not found 4 71
maximize the sum of fractions 33 74
How to find out if all executorservice threads have finished 2 12
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question