Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Tutorial – Building GUIs Java with Added Swing Daniel Bryant

Similar presentations


Presentation on theme: "Java Tutorial – Building GUIs Java with Added Swing Daniel Bryant"— Presentation transcript:

1 Java Tutorial – Building GUIs Java with Added Swing Daniel Bryant csm1db@surrey.ac.uk

2 Get the files Create a directory on your home drive called JavaTutorial Visit my website and download today’s files into the new directory. *Right click each file and select “Save as”  http://www.danielbryant.co.uk/java 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 NetBeans when necessary (i.e. do not mount the new directory or files within NetBeans)

3 Mounting the File System Create a directory named “GuiTutorial” in you home directory. Please note the case of the letters and the absence of spaces – Java can be very picky with file names!! Mount the empty “GuiTutorial” directory in NetBeans  File >> Mount Filesystem Create a package called “samplegui”  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.

4 Packaging your classes We have created a package here to keep all of our classes grouped together The package name also refers to the directory structure in which your java files will be stored i.e. creating a package named samplegui will create a directory named samplegui in your GuiTutorial directory Please be careful with the case of letters. In addition package names must not contain spaces or you will get strange errors!

5 Compiling the Files Create a “Main Class”  Right-click on newly created package  New >> Java Main Class  Call this class “MyMain” 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  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

6 Compiling the Files Create a Java Class  Right-click on samplegui package  New >> Java Class  Call this class “MainFrame” (Note case) Cut and paste the code from the first file you downloaded over any existing code that NetBeans has generated for you in the MainFrame class. It is essential that you delete any automatically generated code in this file only or your application will not compile Click on the “Build” pull down menu and select “Compile”  If all goes well you will see a message informing you the MainFrame.java class was compiled successfully

7 Compiling the Files Add appropriate code to the MyMain class to instantiate a MainFrame object and make it visible on the screen Public static void main (String[ ] args) { MainFrame frame = new MainFrame("Window Title"); frame.setVisible(true); } Run the code by clicking on the green triangle on the menu (the play button) You should see a window on the screen (quite a simple window and perhaps boring, but a GUI nonetheless!)

8 Compiling the Files Have a look at the code (and the comments I’ve provided) Look at and compile the other files you have downloaded  With the second file (MainFrame2.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 MainFrame class to compile your changes  Build >> Compile (F9)

9 The Files MainFrame.java  An implementation of a very simple GUI. Have a look at the code and ask any questions MainFrame2.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

10 The Files MainFrame3.java  I’ve added a JLabel to the GUI so you can include text in your GUI application MainFrame4.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 JFileChooser to load a file into you application


Download ppt "Java Tutorial – Building GUIs Java with Added Swing Daniel Bryant"

Similar presentations


Ads by Google