Re: Generating GUI for text based interface?
anon wrote:
Following on from my previous post, I needa little more help then I
think i should be good to run with it.
I have a program that simulates a call center.
Now I need to add a GUI to it but I don't really know how to use swing.
<http://java.sun.com/docs/books/tutorial/ui/index.html>
As far as I can tell a GUI program has 3 main sections, the section
itself that declares the app, adds the buttons, code etc, then the main
section which just calls the app code, then the action handler part.
Here's my code from the non-gui prog:
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class Main
{
public static void main(String[] args) //main section. reads in data
file and loads into vector. then displays menu
Don't do everything in main(), or indeed in a static method. Use
purpose-built instance methods.
{
Calendar Cal = new GregorianCalendar();
By ironclad convention in Java, variable names should start with a lower-case
letter.
//creates a new calendar
Cal.set(Calendar.YEAR, 2008); //sets
the calendar year
Your lines are too wide for readability on Usenet. Also, you don't need to
comment a line that says "set( Calendar.YEAR)" with "sets calendar year" - it
is rather redundant.
Cal.set(Calendar.MONTH, Calendar.NOVEMBER);
Part of the reason your lines are too wide for Usenet is that your indentation
is too wide for Usenet. Use indentation of no more than four spaces, and keep
line width to 80 characters.
//sets the calendar month
....
SimpleDateFormat sdf = new SimpleDateFormat(); //sets
the date format to be read back
Many of these variables could be instance variables.
....
Vector waitlist = new Vector();
Don't use Vector. Use List and ArrayList, to whit,
List <Caller> waiters = new ArrayList <Caller> ();
....
do //display menu
{
Brace placement should follow one of two conventions for opening brace -
either on the same line as the control structure, or aligned with it on the
next line (not indented).
System.out.println(""); // prints out a blank line.
Obviously you won't use 'System.out.println()' in a GUI.
You really need to narrow your indentation, and regularize it. It's all over
the place.
....
Here's what I have so far for the GUI prog:
....
public class CallGui extends JFrame implements ActionListener {
....
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
CallGui application = new CallGui();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
*All* GUI action must occur on the Event Dispatch Thread (EDT), or you might
get very weird bugs.
}
....
What I can't figure out is where to put what in order to start merging
the two together to achieve my goal. I've been hunting around on the web
and through my books but the answer escapes me.
No matter where I declare my variables etc other parts of the program
wont compile because they can't see them.
You need to use the pattern of creating objects with instance-specific
attributes, and expose those attributes publicly, ideally via getX() and
setX() methods (accessors and mutators, respectively).
package eg;
public class Foo
{
private Bar bar;
public final Bar getBar( void )
{
return this.bar;
}
public final void setBar( Bar val )
{
this.bar = val;
}
}
Then another class's instance can instantiate such an object and interact with
its attributes.
package ex;
import eg.Foo;
public class Baz
{
public static void main( String [] args )
{
Foo foo = new Foo();
Bar bar = makeBar( args ); // just for example
foo.setBar( bar );
// and so on
}
}
--
Lew