Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 import java.awt.Container; 2 import java.awt.FlowLayout; 3 import java.awt.eve

ID: 3535418 • Letter: 1

Question

1 import java.awt.Container;
2 import java.awt.FlowLayout;
3 import java.awt.event.ActionListener;
4 import java.awt.event.*;
5 import javax.swing.JButton;
6 import javax.swing.JFrame;
7 import javax.swing.JLabel;
8 import javax.swing.JPanel;
9 import javax.swing.JTextField;
10
11
//@SuppressWarnings("serial")
12
13
public class WindowWithComponents extends JFrame implements ActionListener
14 {
15 final static int WINDOW_WIDTH =350;
16 final static int WINDOW_HEIGHT = 250;
17 private JPanel panel;
18 private JTextField JtField1;
19 private JTextField JtField2;
20 private JTextField JtSum;
21 private JLabel label1;
22 private JLabel label2;
23 private JLabel labelSum;
24 private JButton jbSum;
25 private JButton JbDialog;
26
27 public WindowWithComponents()
28 {
29 // call the Jframe constructor passing a window title.
30 super("Window with Components");
31 }
32 private void buildPanel(Container contentPane)
33 {
34 // instantiate the window's components
35 label1 = new JLabel("Enter number 1:");
36 label2 = new JLabel("Enter number 2:");
37 labelSum = new JLabel("Sum of two numbers.");
38
39 // create the textfields with 20 columns
40 JtField1 = new JTextField(20);
41 JtField2 = new JTextField(20);
42 JtSum = new JTextField(20);
43
44 // initialize the buttons with text
45 jbSum = new JButton("Sum");
46 JbDialog = new JButton("Open Dialog");
47
48 jbSum.addActionListener(this);
49 jbSum.addActionListener(this);
50
51 // build the panel
52 panel = new JPanel(new FlowLayout());
53
54 // add the components to the panel
55 panel.add(label1);
56 panel.add(JtField1);
57 panel.add(label2);
58 panel.add(JtField2);
59 panel.add(labelSum);
60 panel.add(JtSum);
61 panel.add(jbSum);
62 panel.add(JbDialog);
63 // add the panel to the contentPane
64 contentPane.add(panel);
65
66 }
67 private static void createAndShowGui()
68 {
69 WindowWithComponents frame = new WindowWithComponents();
70
71 frame.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
72 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
73 //build panel
74 frame.buildPanel(frame.getContentPane());
75 // the pack method sizes the frame so all of the components
76 // at or above their prefered.
77
78 frame.pack();
79 frame.setVisible(true);
80
81 }
82 public static void main(String[] args)
83 {
84 // schedule a job for the event-dispatching thread.
85 // creating and showing this application's gui
86 javax.swing.SwingUtilities.invokeLater(new Runnable()
87 {
88 public void run()
89 {
90 createAndShowGui();
91 }
92 });
93 }
94 public void actionPerformed(ActionEvent e)
95 {
96 Object source = e.getSource();
97
98 // find out what object initiated the event
99 if(source == jbSum)
100 {
101 this.sum();
102 }
103 else if (source == JbDialog)
104 {
105 this.openDialog();
106 }
107 }
108 private void openDialog()
109 {
110 //toto auto generated method stub
111 }
112 private void sum()
113 {
114 int num1 = -1;
115 int num2 = -1;
116 int sum = -1;
117
118 // get the numbers from the gui and convert to integers
119 num1 = Integer.parseInt(JtField1.getText());
120 num2 = Integer.parseInt(JtField2.getText());
121
122 sum = num1 + num2;
123
124 JtSum.setText(String.valueOf(sum));
125 }
126
127 }


-can anyone explain me what is "javax.swing.SwingUtilities.invokeLater(new Runnable)"

-and what is "this" keyword do in this code, i learned this keyword to use to call from a super class but here only one class.

-why num1,num2,and sum are declared with -1. when i run the program -1 is not there.

thanks a lot..


Explanation / Answer

Swing is a single-threaded GUI toolkit. There is a special thread called the Event Dispatch Thread (EDT) that is dedicated to displaying and updating Swing components.public javax.swing.SwingUtilities.invokeLater(new Runnable) cause anonymous thread object method rum() which has already been defined in your program to be executed asynchronously on the event dispatching thread. This will happen after all pending AWT events have been processed. This method are generally used when an application thread needs to update the GUI.



this is a keyword defined in the java ,which is used to reference the current object,we can refer to any member of the current object from within an instance method or a constructor by using this.

for example:--

public class Rectangle {

private int x, y;

private int radius

public Rectangle() {

this(0, 0,radius);

}

public Rectangle(int radius) {

this(0, 0, radius);

}

public Rectangle(int x, int y,int radius) {

this.x = x;

this.y = y;

this.width = radius.

}

}


num1 ,num2 and sum are just initialized with -1 , you can set any value to these variables during initialization ,or you also have a option to not initialize variable, these things doesn't matter,because ultimatiley you are changing the value of these variables at line

num1 = Integer.parseInt(JtField1.getText());

num2 = Integer.parseInt(JtField2.getText());

sum = num1 + num2;


generally we initialize variables to some legal values,so that they don't take garbage values due to some progamming errors. for example


int a;

int b;

int sum=a+b;


this code will create error beacuse we are not any legal integer values to variables a and b , so when we add these variable, what we are actually doing,we arde adding garbage value stored int variables, which will result in a wrong result. the remedy is to initialize the variables a and b.


int a=2;

int b=3;

int sum=a+b;