Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

ComputerScience & Software Engineering
Software Engineering (CITS1220)

 

    | 

 

 

Week 5: Building a GUI with Java Swing


TheaimofthislabistointroducetheSwingAPIforJavaGUIprogramming. The Swing classes andcomponents are contained in the javax.swingpackage hierarchy.

 

At the endof the lab, you will be able to evaluate user interfaces, modify agraphical user interface for the Customer Details program and implementbasic Swing GUIs in Java.

 

Beginners: Lab Tasks

 

  1. Create anew Java Project called week5 in your workspace. See the week2 lab fordetails of how to do this if needed.
  2. Savethefiles , and in yourweek5/src directory, and import into the project by using EasyEclipse. (Click on the week5 package fromthe Package Explorer tab, select File > Import > File System, then selectthe java file or save on Desktop and drag the files to Ecliplse Navigator.)   DemoGUI is a simple GUI with comments, providedfor your reference.  In this lab we willwork with CustomerDetails and ThankYou.
  3. Turnon�showlinenumber�indicatorinEclipse.(Select Window> Preferences> General> Editors> Text Editors,tickonthe�showlinenumber�andclickOK.)
  4. RuntheGUIapplicationtoexploreitsfunctionality.(Select CustomerDetails.java> Right-click > Run As-> Java Application).Writeonpaperause-casetodescribeausefulinteractionscenario.
  5. Read CustomerDetails.java. You can useeclipse�s help (right-click on keywords in the program) or refer backto your lecture notes and answer these questions. Line number is shownin the examples below.
    • What is theeffect of setVisible(true) ?
          28  this.setVisible(true);
    • Why is JRadioButton used for male/female choice insteadof JButton?
          39   mButton = new JRadioButton();
      40 fButton = new JRadioButton();
    • What is theeffect of setEnabled(true) ?
          48   checkButton = new JButton("Check");
      49 checkButton.setEnabled(true);
    • What is thedifference between addMouseListener() and addItemListener()?
          103  fButton.addItemListener(new ItemListener()...
      109 fButton.addMouseListener(new MouseAdapter()...
    • What is theeffect of setActionCommand() ?
          123  mobileText.setActionCommand("mobile");
  6. WhenauserclicksCheck,allinformationintheframeisverified.  Checklinenumber165 to see the checkingprocess after Check is clicked
  7. ModifytheJavacodetoinclude2newlabelsandtextfieldsfor email addressand verify email address purposes.  Youwill need to declare 2 new JText and 2 new JLabel fields (TODO - 1a), initializethem (TODO - 1b), set action handlers (TODO - 1c) and put them in theGUI layout (TODO - 1d).

Intermediate

 

  1. Verify thatthe 2 email addresses are the same. Use string matching and equalsmethod from String class. Refer to (TODO-2)toplaceyournewcode.
  2. Learnhowtocreateerrormessagesusingdifferenttechnique.First is to use JOptionPane.showMessageDialog. See examples in the checkButton_mouseClickedmethod for usage. Include this for your email checking process.
  3. Addanothercheckingforthecustomername�stextfield.Make sure that the inputname contains only alphabet, chars and not digit or punctuation marks(except ���). Hint: Use matches method from String class.
  4. Modify the ThankYou frame by adding navigation buttons. Inthis case, add a Quit button to the ThankYouframe.
  5. Thismustbecompletedpriortothenextweek�slab to do the advanced exercise) Create aJAR file of your project.

    Right-click on your project in the Navigator windowand select

    Export > Java > JAR File > Next > name your JAR file >Next > Next >
    Enter CustomerDetails in the MainClass text box  then click Finish
    Choose the name CustomerDetails.jar foryour jar file and take note on the path for your new jar file.


Advanced

  1. Createadatefield, which consists of date, month and year. You may use JComboBox for each of the three fields
  2. Ensurethatonlyvalid dates can be entered. One way is to use the SimpleDateFormat class. See the sample code provided.
  3. Improve thedesign of your Swing GUI (refer to this excellent tutorial on )
    • Colors,fonts and borders add attractiveness.
    • Look-and-feelimprovescontrol rendering.
    • Use LayoutManagers to control size. Position and alignment of components.
  4. Morewidgetstotry (what are their uses?)
    • JDialog
    • JOptionPane, JScrollPane, JLayeredPane, JSplitPane, JTabbedPane
  5. Check that the format of email address entered by useris a valid format of an email address.


 

CRICOSProvider Code: 00126G