1 import java awt container 2 import java awt flowlayout 3 import java awt event act 5346666

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..

"Get 15% discount on your first 3 orders with us"
Use the following coupon
FIRST15

Order Now