Java Tutorial Building GUIs Java with Added Swing
Java Tutorial – Building GUIs Java with Added Swing Daniel Bryant csm 1 db@surrey. ac. uk
Get the files n Create a directory on your home drive called Java. Tutorial n Visit my website and download today’s files into the new directory. *Right click each file and select “Save as” ¡ n http: //www. danielbryant. co. uk/java These files will be used as a reference only. Accordingly, the files should be opened in Notepad and the contents cut and paste into Net. Beans when necessary (i. e. do not mount the new directory or files within Net. Beans)
Mounting the File System n Create a directory named “Gui. Tutorial” in you home directory. Please note the case of the letters and the absence of spaces – Java can be very picky with file names!! n Mount the empty “Gui. Tutorial” directory in Net. Beans ¡ n Create a package called “samplegui” ¡ ¡ n File >> Mount Filesystem Right-click on newly mounted directory New >> Java Package N. B. A package is essentially a unique named collection of classes and allows you to import (or use) other peoples code easily.
Packaging your classes n We have created a package here to keep all of our classes grouped together n The package name also refers to the directory structure in which your java files will be stored n i. e. creating a package named samplegui will create a directory named samplegui in your Gui. Tutorial directory n Please be careful with the case of letters. In addition package names must not contain spaces or you will get strange errors!
Compiling the Files n Create a “Main Class” ¡ ¡ ¡ n The “Main Class” will be the entry point into your Java application and accordingly it will contain an important method that is executed whenever you run your class ¡ n Right-click on newly created package New >> Java Main Class Call this class “My. Main” public static void main(String[] args) We will use this class to instantiate our other objects and determine the flow of execution within the application, or in other words, utilise our other classes
Compiling the Files n Create a Java Class ¡ ¡ ¡ Right-click on samplegui package New >> Java Class Call this class “Main. Frame” (Note case) n Cut and paste the code from the first file you downloaded over any existing code that Net. Beans has generated for you in the Main. Frame class. It is essential that you delete any automatically generated code in this file only or your application will not compile n Click on the “Build” pull down menu and select “Compile” ¡ If all goes well you will see a message informing you the Main. Frame. java class was compiled successfully
Compiling the Files n Add appropriate code to the My. Main class to instantiate a Main. Frame object and make it visible on the screen Public static void main (String[ ] args) { Main. Frame frame = new Main. Frame("Window Title"); frame. set. Visible(true); } n Run the code by clicking on the green triangle on the menu (the play button) n You should see a window on the screen (quite a simple window and perhaps boring, but a GUI nonetheless!)
Compiling the Files n Have a look at the code (and the comments I’ve provided) n Look at and compile the other files you have downloaded ¡ n With the second file (Main. Frame 2. java) I want you to modify the code to create another button that, when pressed, opens a dialog box Don’t forget when you modify the code in the Main. Frame class to compile your changes ¡ Build >> Compile (F 9)
The Files n Main. Frame. java ¡ n An implementation of a very simple GUI. Have a look at the code and ask any questions Main. Frame 2. java ¡ ¡ The same GUI with buttons and Event handlers to launch additional code I want you to add an additional button yourself that displays a dialogue box on screen
The Files n Main. Frame 3. java ¡ n I’ve added a JLabel to the GUI so you can include text in your GUI application Main. Frame 4. java ¡ ¡ I’ve created a GUI with a Layout Manager to specify how buttons are set out on the screen I’ve also included an example of how to use a JFile. Chooser to load a file into you application
- Slides: 10