Examples class, Week 9
In this exercise we will be gradually building up a GUI to perform arithmetic:
The user will add an integer, character and integer respectively in to the first three textboxes. When they click on the button (marked ¡°=¡±) the result of the arithmetic operation will appear in the textbox on the right. Note that the only component that generates an event is the (equals) button.
1. Here is some code implementing a first version of the program, with no event handling. The line numbers are just for reference.
Copyright By PowCoder代写 加微信 powcoder
//import statements omitted
1. public class Arithmetic0 extends JFrame{
2. private JTextField number1Text;
3. private JTextField operatorText;
4. private JTextField number2Text;
5. private JTextField resultText;
6. private JButton equalsButton;
7. public Arithmetic0() {
8. //JFrame title and size – omitted
9. this.setLayout(new FlowLayout());
10. number1Text = new JTextField(8);
11. operatorText = new JTextField(3);
12. number2Text = new JTextField(8);
13. resultText = new JTextField(15);
14. equalsButton = new JButton(“=”);
15. number1Text.setText(“” + 0);
16. operatorText.setText(“” + ‘+’);
17. number2Text.setText(“” + 0);
18. resultText.setText(“” + 0);
19. this.add(number1Text);
20. this.add(operatorText);
21. this.add(number2Text);
22. this.add(equalsButton);
23. this.add(resultText);
25. // main method omitted
(a) Read the code carefully. With a pen indicate next to each of the following categories which
lines of code are associated with the following:
– instance variable declaration
– contentPane layout
– Instance variable initialization
– Setting of the initial values of the textFields
– Adding of the components to the underlying contentPane
Look at lines 15 and 16. Why can¡¯t we just replace these with:
number1Text.setText(0);
operatorText.setText(‘+’);
2. We are going to add event handling to the code. To do this we need to do three things. What are they – and where in the code do we do them?
3. To help with the implementation of an ActionPerformed method, I will use a helper method to calculate the result of applying the operator stored in a character c1 to two integers a1 and b1. Fill in the method below:
public static int calculate(int a1, int b1, char c1){ // FILL THIS IN!
4. Where could we add the helper method in our code?
5. I will also need to be able to extract integer and character values from the textFields. Since the operation getText() returns a string value, how can I do this? Complete the following:
int a = . . . number1Text.getText() . . . ; int b = . . . number2Text.getText() . . . ; char c = . . . operatorText.getText() . . . ;
6. I will now demonstrate how to add event handling to our code (see demo). The resulting file will be the similar to the solutions file Arithmetic2.java which you can access after the examples class.
7. I will now create a new class, ExampleComponent.java which will perform most of the work, but will call Examples2.java as an ActionListener. Examples2.java is the main JFrame object and now looks like this: Note that ExampleComponent.java and Examples2.java are similar to solution files ArithComponent.java and Arithmetic3.java which can access after the examples class. Examples2.java and an outline of ExampleComponent.java are shown on the next page.
//import statements omitted
public class Examples2 extends JFrame implements ActionListener { ExampleComponent comp;
public Examples2() {
// JFrame title and size omitted
this.setLayout(new FlowLayout());
comp = new ExampleComponent(this); // create new component add(comp);
public void actionPerformed(ActionEvent e) {
comp.updateResult(); //component reads values and updates result
* Main method — ommitted
public class ExampleComponent extends JPanel { // to complete
// add instance variables
public ExampleComponent(ActionListener parent){ // to complete
public static void updateResult(){ //to complete
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com