|
|
|
|
||
|
|
Developing a Graphical User Interface
(GUI) Introduction |
|
||
|
|
A basic feature of modern application programs is the graphical user interface of GUI. Java has many classes that facilitate the building of GUI's and the purpose of the exercise is to familiarize you with some of these elements. In this exercise you will develop a menu system using components of your choice (menu bar, menu list, buttons, radio buttons) to allow the user to input data into a data file. The screens should include a main menu, and at least three sub menus (input, edit and view). The data will be stored and accessed in a text file. The code for writing data to a text file is shown below as a as the class "WriteTestFile". Feel free to use other file access code should you desire. |
|
||
|
|
import java.io.*; class WriteTestFile { WriteTestFile(String s, String f) { // declare a file output object FileOutputStream
out; // declare a print stream object PrintStream p; try { //
Create a new file output stream out = new FileOutputStream(f); //
Connect print stream to the output stream p
= new PrintStream( out ); //write
string to file p.println(s); //close
the file when complete p.close(); } catch (Exception e) { System.err.println ("Error writing to file"); } }//end method }//end class |
|
||
|
|
To read data from a text file use the following class: |
|
||
|
|
import java.io.*; class ReadTestFile { ReadTestFile(String f) { try { //
Open the file that is the first //
command line parameter FileInputStream fstream = new FileInputStream(f); //
Convert our input stream to a //
DataInputStream DataInputStream in = new DataInputStream(fstream); //
Continue to read lines while //
there are still some left to read while
(in.available() !=0) { // Print file line to screen System.out.println
(in.readLine()); } in.close(); }//end try catch (Exception e) { System.err.println("File input error"); } }//end method }//end class |
|
||
|
|
To read data from a text file use the following class: |
|
||
|
|
import java.io.*; class ReadTestFile { ReadTestFile(String f) { try { //
Open the file that is the first //
command line parameter FileInputStream fstream = new FileInputStream(f); //
Convert our input stream to a //
DataInputStream DataInputStream in = new DataInputStream(fstream); //
Continue to read lines while //
there are still some left to read while
(in.available() !=0) { // Print file line to screen System.out.println
(in.readLine()); } in.close(); }//end try catch (Exception e) { System.err.println("File input error"); } }//end method }//end class |
|
||
|
|
|
|
||
|
|
MORE CODE TO BE ADDED HERE |
|
||
|
|
Programming Assignment Write an application program that has a main menu and three sub menu. The main menu will allow the user to access the subsequent menus. The main menu will also allow the user to exit the program. The three submenus will consist of an input menu, and edit menu and a view menu. The input menu will allow the user to append records to a text file. The edit menu will allow a user to edit the records in a text file. The view menu will display the records of the file and allow the user to scroll through the records. I would suggest that you implement this as an ArrayList. Have the ArrayList be copied to the text file for input, copied from the text file for edit and view. Turn in a program listing your source code. Source code should use proper formatting. |
|