• No results found

Tutorial 3 – Designing the

N/A
N/A
Protected

Academic year: 2021

Share "Tutorial 3 – Designing the"

Copied!
24
0
0

Loading.... (view fulltext now)

Full text

(1)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Outline

3.1 Test-Driving the Inventory Application

3.2 Customizing JLabels on the Inventory Application

3.3 Customizing JTextFields and a JButton in the Inventory Application

3.4 Wrap-Up

Application

Introducing JTextField s and JButton s

(2)

Objectives

• In this tutorial, you will learn to:

– Use graphical user interface design guidelines to create a useful GUI.

– Customize JLabel s, JTextField s and a JButton in an application window.

– Align text horizontally in a JTextField .

– Specify that a JTextField is uneditable.

(3)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application Requirements

A college bookstore receives cartons of textbooks. In each shipment, each carton contains the same number of textbooks. The inventory manager wants to use a computer to calculate the total number of textbooks arriving at the bookstore for each shipment. The inventory manager will enter the number of cartons received and the fixed number of textbooks in each carton of the shipment; the application should then calculate and display the total number of textbooks in the shipment.

(4)

3.1 Test-Driving the Inventory Application (Cont.)

Figure 3.1 Inventory application JFrame with default data displayed in the input JTextFields.

Input JTextFields Output JTextField

JButton

• JTextField s

– Enables a user to input data from the keyboard – Display data as output to the user

• JButton s

– Component the user can click to cause the application to perform

(5)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

(Cont.)

Figure 3.2 Inventory application after you enter new quantities.

Descriptive JLabels

• Input data into application

– Enter

3

in the

Cartons per shipment: JTextField

– Enter

15

in the

Items per carton: JTextField

(6)

3.1 Test-Driving the Inventory Application (Cont.)

Figure 3.3 Calculating the total number of items received.

Result of calculation displayed in an uneditable JTextField

• Click the Calculate Total JButton

– Result of calculation displays in an uneditable

JTextField

(7)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application

Figure 3.4 JLabels in the Inventory application.

Descriptive JLabels

(8)

3.2 Customizing JLabel s in the Inventory Application (Cont.)

Figure 3.5 Setting the Cartons per shipment: JLabel’s text and bounds.

Set the text and bounds of the

cartonsJLabel

• Setting the text displayed in the JLabel

– Use the

setText

method to set the text property – Use sentence-style capitalization

• Setting the bounds of the JLabel

– Use the

setBounds

method to set the bounds property

(9)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 3.6 Updated application with Cartons per shipment:

JLabel.

Upper-left corner of

JLabel appears at 16, 16

(10)

3.2 Customizing JLabel s in the Inventory Application (Cont.)

Figure 3.7 Setting the Items per carton: JLabel’s text and bounds.

Set the text and bounds of the itemsJLabel

(11)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 3.8 Setting the Total: JLabel’s text and bounds.

Set the text and bounds of the totalJLabel

(12)

3.2 Customizing JLabel s in the Inventory Application (Cont.)

Figure 3.9 Running Inventory Application with three customized JLabels.

(13)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

JButton in the Inventory Application

Figure 3.10 Setting the Cartons per shipment: JTextField’s properties.

Set the text, bounds and alignment of the

cartonsJTextField

• Setting the text displayed in the JTextField

– Use the

setText

method to set the text property

• Setting the bounds of the JTextField

– Use the

setBounds

method to set the bounds property

(14)

3.3 Customizing JTextField s and a

JButton in the Inventory Application (Cont.)

Figure 3.11 Setting the Items per carton: JTextField’s properties.

Set the text, bounds and alignment of the

itemsJTextField

• Setting the horizontal alignment of the JLabel

– Use the

setHorizontalAlignment

method to set the horizontal alignment property

• JTextField.RIGHT

• JTextField.LEFT

(15)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

JButton in the Inventory Application (Cont.)

Figure 3.12 Setting the Total: JTextField’s bounds and horizontal alignment.

Set the bounds and alignment of the

totalResultJTextField

(16)

3.3 Customizing JTextField s and a

JButton in the Inventory Application (Cont.)

Figure 3.13 Changing the editable property of the Total: JTextField.

Set the editable property of the totalResultJTextField

to false to prevent the user from modifying it

• For an output JTextField

– Use the

setEditable

method to allow or prevent the user from

modifying the contents of the

JTextField

(17)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

JButton in the Inventory Application (Cont.)

Figure 3.14 Running Inventory Application with customized JTextFields.

(18)

3.3 Customizing JTextField s and a

JButton in the Inventory Application (Cont.)

Figure 3.15 Customizing the CalculateTotal JButton.

Set the text and bounds of the calculateJButton

• Adding a JButton

– Set text property

– Use book-title capitalization

– Set bounds property

(19)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

JButton in the Inventory Application (Cont.)

Figure 3.16 Running the application after customizing the Calculate Total JButton.

(20)

Outline

Inventory.java (1 of 5)

2 // Calculates the number of items in a shipment based on the number 3 // of cartons received and the number of items per carton.

4 import java.awt.*;

5 import java.awt.event.*;

6 import javax.swing.*;

7

8 public class Inventory extends JFrame 9 {

10 // JLabel and JTextField for cartons per shipment 11 private JLabel cartonsJLabel;

12 private JTextField cartonsJTextField;

13

14 // JLabel and JTextField for items per carton 15 private JLabel itemsJLabel;

16 private JTextField itemsJTextField;

17

18 // JLabel and JTextField for total items per shipment 19 private JLabel totalJLabel;

20 private JTextField totalResultJTextField;

21

22 // JButton to initiate calculation of total items per shipment 23 private JButton calculateJButton;

(21)

 2004 Prentice Hall, Inc.

All rights reserved.

Inventory.java (2 of 5)

28 createUserInterface();

29 } 30

31 // create and position GUI components; register event handlers 32 public void createUserInterface()

33 {

34 // get content pane and set layout to null 35 Container contentPane = getContentPane();

36 contentPane.setLayout( null );

37

38 // set up cartonsJLabel

39 cartonsJLabel = new JLabel();

40 cartonsJLabel.setText( "Cartons per shipment:" ); 

41 cartonsJLabel.setBounds( 16, 16, 130, 21 );

42 contentPane.add( cartonsJLabel );

43

44 // set up itemsJLabel

45 itemsJLabel = new JLabel();

46 itemsJLabel.setText( "Items per carton:"  

47 itemsJLabel.setBounds( 16, 48, 104, 21 );

48 contentPane.add( itemsJLabel );

49

Set the text and bounds of the

cartonsJLabel

Set the text and bounds of the

itemsJLabel

(22)

Outline

Inventory.java (3 of 5)

51 totalJLabel = new JLabel();

52 totalJLabel.setText( "Total:" ); 

53 totalJLabel.setBounds( 204, 16, 40, 21 );

54 contentPane.add( totalJLabel );

55

56 // set up cartonsJTextField

57 cartonsJTextField = new JTextField();

58 cartonsJTextField.setText( "0" );  

59 cartonsJTextField.setBounds( 148, 16, 40, 21 );

60 cartonsJTextField.setHorizontalAlignment( JTextField.RIGHT );

61 contentPane.add( cartonsJTextField );

62

63 // set up itemsJTextField

64 itemsJTextField = new JTextField();

65 itemsJTextField.setText( "0" );  

66 itemsJTextField.setBounds( 148, 48, 40, 21 );

67 itemsJTextField.setHorizontalAlignment( JTextField.RIGHT );

68 contentPane.add( itemsJTextField );

69

Set the text and bounds of the

totalJLabel

Set the text, bounds and alignment of the

cartonsJTextField

Set the text, bounds and alignment of the

itemsJTextField

(23)

 2004 Prentice Hall, Inc.

All rights reserved.

Inventory.java (4 of 5)

73 totalResultJTextField.setHorizontalAlignment(

74 JTextField.RIGHT );

75 totalResultJTextField.setEditable( false );

76 contentPane.add( totalResultJTextField );

77

78 // set up calculateJButton

79 calculateJButton = new JButton();

80 calculateJButton.setText( "Calculate Total" ); 

81 calculateJButton.setBounds( 204, 48, 126, 24 );

82 contentPane.add( calculateJButton );

83

84 // set properties of window

85 setSize( 354, 112 ); // set window size

86 setTitle( "Inventory" ); // set title bar string 87 setVisible( true ); // display window

88

89 } // end method createUserInterface 90

Set the bounds, alignment and editability of the

totalResultJText- Field

Set the text and bounds of the

calculateJButton

(24)

Outline

Inventory.java (5 of 5)

92 public static void main( String[] args ) 93 {

94 Inventory application = new Inventory();

95 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

96

97 } // end method main 98

99 } // end class Inventory

References

Related documents

The projected gains over the years 2000 to 2040 in life and active life expectancies, and expected years of dependency at age 65for males and females, for alternatives I, II, and

Marie Laure Suites (Self Catering) Self Catering 14 Mr. Richard Naya Mahe Belombre 2516591 [email protected] 61 Metcalfe Villas Self Catering 6 Ms Loulou Metcalfe

The corona radiata consists of one or more layers of follicular cells that surround the zona pellucida, the polar body, and the secondary oocyte.. The corona radiata is dispersed

Currently, National Instruments leads the 5G Test & Measurement market, being “responsible for making the hardware and software for testing and measuring … 5G, … carrier

• Follow up with your employer each reporting period to ensure your hours are reported on a regular basis?. • Discuss your progress with

Proprietary Schools are referred to as those classified nonpublic, which sell or offer for sale mostly post- secondary instruction which leads to an occupation..

4.1 The Select Committee is asked to consider the proposed development of the Customer Service Function, the recommended service delivery option and the investment required8. It

National Conference on Technical Vocational Education, Training and Skills Development: A Roadmap for Empowerment (Dec. 2008): Ministry of Human Resource Development, Department