Write an application that displays a solid square com- posed of a character input by the user (Fig. 12.29). The user also should input the size of the side of the square.
a) Copying the template to your working directory. Copy the C:Examples Tutorial12ExercisesDisplaySquare directory to your C:SimplyJava direc- tory.
b) Opening the template file. Open the DisplaySquare.java file in your text editor.
c) Adding a method to your application that displays a square of characters. On line
141, add a comment indicating that the method will display a square in a JTextArea. On line 142, add the method header for this method. The method will be called dis- playSquare. This method contains two parameters–the first, of type int, should be called size, the second, of type String, should be called character. This method does not return a value, but simply performs a task (displaying a square). For such methods, the return type is specified as void. Specify the return type for display- Square as void. On line 143, add a left brace to begin the body of the method. On line 145, add the right brace to end the body of the method. Follow the brace with a comment i
```
1 // DisplaySquare.java
2 // Displays a square of characters where the size of the square
3 // and the characters used in the square are specified by the user.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.text.*;
7 import javax.swing.*;
8
9 public class DisplaySquare extends JFrame
10 {
11 // JLabel and JTextField for size of square sides
12 private JLabel squareSizeJLabel;
13 private JTextField squareSizeJTextField;
14
15 // JLabel and JTextField for fill character
16 private JLabel fillCharacterJLabel;
17 private JTextField fillCharacterJTextField;
18
19 // JTextArea and JScrollPane for displaying square
20 private JTextArea outputJTextArea;
21 private JScrollPane outputJScrollPane;
22
23 // JButton initiates creation and display of square
24 private JButton displaySquareJButton;
25
26 // no-argument constructor
27 public DisplaySquare()
28 {
29 createUserInterface();
30 }
31
32 // create and position GUI components; register event handlers
33 private void createUserInterface()
34 {
35 // get content pane for attaching GUI components
36 Container contentPane = getContentPane();
37
38 // enable explicit positioning of GUI components
39 contentPane.setLayout( null );
40
41 // set up squareSizeJLabel
42 squareSizeJLabel = new JLabel();
43 squareSizeJLabel.setBounds( 16, 16, 80, 21 );
44 squareSizeJLabel.setText( "Square size:" );
45 contentPane.add( squareSizeJLabel );
46
47 // set up squareSizeJTextField
48 squareSizeJTextField = new JTextField();
49 squareSizeJTextField.setBounds( 104, 16, 24, 21 );
50 squareSizeJTextField.setHorizontalAlignment(
51 JTextField.RIGHT );
52 contentPane.add( squareSizeJTextField );
53
54 // set up fillCharacterJLabel
55 fillCharacterJLabel = new JLabel();
56 fillCharacterJLabel.setBounds( 16, 56, 80, 21 );
57 fillCharacterJLabel.setText( "Fill character:" );
58 contentPane.add( fillCharacterJLabel );
59
60 // set up fillCharacterJTextField
61 fillCharacterJTextField = new JTextField();
62 fillCharacterJTextField.setBounds( 104, 56, 24, 21 );
63 fillCharacterJTextField.setHorizontalAlignment(
64 JTextField.RIGHT );
65 contentPane.add( fillCharacterJTextField );
66
67 // set up outputJTextArea
68 outputJTextArea = new JTextArea();
69 outputJTextArea.setBackground( Color.LIGHT_GRAY );
70 outputJTextArea.setEditable( false );
71 contentPane.add( outputJTextArea );
72
73 // set up outputJScrollPane
74 outputJScrollPane = new JScrollPane( outputJTextArea );
75 outputJScrollPane.setBounds( 16, 96, 248, 136 );
76 contentPane.add( outputJScrollPane );
77
78 // set up displaySquareJButton
79 displaySquareJButton = new JButton();
80 displaySquareJButton.setBounds( 144, 16, 120, 23 );
81 displaySquareJButton.setText( "Display Square" );
82 contentPane.add( displaySquareJButton );
83 displaySquareJButton.addActionListener(
84
85 new ActionListener() // anonymous inner class
86 {
87 // event handler called when
88 // displaySquareJButton is clicked
89 public void actionPerformed( ActionEvent event )
90 {
91 displaySquareJButtonActionPerformed( event );
92 }
93
94 } // end anonymous inner class
95
96 ); // end call to addActionListener
97
98 // set properties of application's window
99 setTitle( "Display Square" ); // set title bar string
100 setSize( 288, 276 ); // set window size
101 setVisible( true ); // display window
102
103 } // end method createUserInterface
104
105 // display a square of characters
106 private void displaySquareJButtonActionPerformed(
107 ActionEvent event )
108 {
109 // if valid input is entered
110 if ( ( squareSizeJTextField.getText() != "" ) &&
111 ( fillCharacterJTextField.getText() != "" ) )
112 {
113 // retrieve user input
114 int squareSize = Integer.parseInt(
115 squareSizeJTextField.getText() );
116 String fillCharacter = fillCharacterJTextField.getText();
117
118 displaySquare( squareSize, fillCharacter );
119 }
displaySquare( squareSize, fillCharacter );
120 else
121 {
122 JOptionPane.showMessageDialog( null,
123 "Square size and fill character needed",
124 "Incorrect Input", JOptionPane.ERROR_MESSAGE );
125 }
126
127 } // end method displaySquareJButtonActionPerformed
128
129 // display square in outputJTextArea
130 private void displaySquare( int size, String character )
131 {
132 outputJTextArea.setText( "" ); // clear output
133
134 // loop until row reaches value of first argument (size)
135 for ( int row = 1; row <= size; row++ )
136 {
137 // loop until column reaches value of size
138 for ( int column = 1; column <= size; column++ )
139 {
140 outputJTextArea.append( character );
141 }
142
143 outputJTextArea.append( "\n" ); // add line to output
144 }
145
146 } // end method displaySquare
147
148 // main method
149 public static void main( String[] args )
150 {
151 DisplaySquare application = new DisplaySquare();
152 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
153
154 } // end method main
155
156 } // end class DisplaySquare
```
You might also like to view...
After a tag is created, it is easily added to other files without having to type the entire tag name
Indicate whether the statement is true or false
MC Function__________ , which receives one required argument and two optional arguments, creates a stream object.
a) create. b) open. c) write. d) buffer.