Creating a GUI Program in Java

bravesnailsΛογισμικό & κατασκευή λογ/κού

7 Ιουν 2012 (πριν από 6 χρόνια και 1 μήνα)

447 εμφανίσεις

Creating a GUI Program in Java

You will use the NetBeans GUI program creator to build a simple GUI program for your birthday
converter. Your GUI program will have three input fields, an output field, and a show button and an exit

1. Create a new Java project (start NetBeans, click File, choose New Project, Select Java, and
select Java Applications and click next).
2. Name this project JavaGUIDate.

You will next create a simple GUI Class using the NetBeans GUI Generator:
3. Right click your Package name in the Source Package folder (JavaGUIDate) name (in the
Projects panel) and choose New and then choose Jpanel Form:

4. You see a New Jpanel Form. Name this Panel class JPanelDate:

5. Click Finish

6. You see a blank window with a set of tools under the Palette:

7. Scroll down until you see the Label control under the pallet area.
8. Drag the label control onto the empty box and position it where you want the title.

Under the Properties box (below the Pallet area) are a list of properties for the selected control. These
include the label Font and the label text. Clicking the small button next to the property allows you to set
the property. Those properties you can set are in Bold.

9. Make sure the label is selected. Locate the Font property in the properties window and click the
small button to the right of the Font property.

10. Choose a new font and font size.

11. Locate the Text property and click the button.

12. Type the new text line “Show Birthday Day” and click OK. You may have to widen the label to
see all the text.

13. Locate the Text Field under the Palette area and drag a text field into the blank window.
Onscreen guides will help you position the component.

14. Repeat this step and drag two more text fields into the window.

15. Drag three small labels until they are next to the text fields. The screen should look something

You will next change the text for the labels.

16. Click each label, locate the Text property, and change the label text to Year, Month, Day.

17. Drag a label under the three text fields and size it so it extends across the text area. Change the
text in this label to “Day of Week”. Set the horizontal alignment of this label to Center.

18. Drag two buttons from the Palette window onto the form and place them below the Day of
Week label.

19. Select each text label and click the Text property. Delete the text inside this text field. The field
will compress. You will have to expand the text field by dragging it wider.

20. Set the text of one button to Show and the other to Exit. The finished form should look like:

The controls that you will modify in your program must be given a name. You will next set the control
code name so you can access the control when you run the program.

21. For each of the text fields, click the field and then click the Code button under the Properties
window. Locate the variable name and set the name to what the field will contain (jYear,
jMonth, jDay).

22. Click the large label with the text Day of Week and click the Code button. You will have to give
this field a name as well. Name this field jDayOutput.

23. Name the two buttons jShowButton and jExitButton.

You will next set the Code for this program

1. Click the Source tab in the window. This displays the source code being created for your form.

2. Scroll to the top of the source code and, just below the package name javaguidate enter:

import java.util.*;

3. Switch to the Design View (the second tab button at the top of the window) and double click the
Show Button. This opens the code window and places the cursor at the location where code can
be entered. The method should be named jShowButtonActionPerformed(…).

4. Type the following lines of code:

// TODO add your handling code here:
String strdays[] = {"Sunday","Monday","Tuesday",

int yearval = Integer.parseInt(jYear.getText());
int monthval = Integer.parseInt(jMonth.getText() )-1;
int dayval = Integer.parseInt(jDay.getText());
GregorianCalendar cal = new
int dayofweek = cal.get(GregorianCalendar.DAY_OF_WEEK);

5. Save the Jpanel.

6. Switch back to the Main program.

7. At the top of the Main class, just below the package statement include the following:

import javax.swing.*;

8. In the main() method type the following:

// TODO code application logic here
JFrame f1 = new JFrame("Date");
JPanelDate p1 = new JPanelDate();


9. Run this program. You should see a GUI window.