Skip to content

Commit e5433fc

Browse files
committed
SwingBasics-Typo
1 parent b281bbc commit e5433fc

File tree

2 files changed

+147
-5
lines changed

2 files changed

+147
-5
lines changed

Swing/GUIEventHandling.java

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import javax.swing.*;
2+
3+
import com.sun.glass.events.KeyEvent;
4+
import com.sun.glass.events.MouseEvent;
5+
import com.sun.javafx.stage.WindowEventDispatcher;
6+
7+
import java.awt.Event;
8+
import java.awt.event.ActionEvent;
9+
import java.awt.event.ActionListener;
10+
import java.awt.event.KeyListener;
11+
import java.awt.event.MouseListener;
12+
import java.awt.event.WindowListener;
13+
14+
public class GUIEventHandling extends JFrame{
15+
16+
JButton button1;
17+
JTextField textField1;
18+
JTextArea textArea1;
19+
int buttonClicked;
20+
21+
public static void main(String[] args){
22+
new GUIEventHandling();
23+
}
24+
25+
public GUIEventHandling(){
26+
/** Size of the Frame */
27+
this.setSize(400,400);
28+
29+
/**Position of the Frame 'centered' */
30+
this.setLocationRelativeTo(null);
31+
32+
/**Allow resizing of Window. 'true' by default */
33+
this.setResizable(false);
34+
35+
/**Closes the frame when you click the cross icon */
36+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37+
38+
/**set title of the frame */
39+
setTitle("Swing GUI");
40+
41+
/**Create a panel area */
42+
JPanel panel = new JPanel();
43+
44+
/**Label is just plain text */
45+
JLabel label1 = new JLabel("Tell me somthing");
46+
47+
/** Shows a pop-up when you hover your mouse in the label. */
48+
label1.setToolTipText("Write something...anything!!!");
49+
50+
/**Create a button */
51+
JButton button1 = new JButton("Send");
52+
button1.setToolTipText("Press it to find out!");
53+
ListenforButton lforButton = new ListenforButton();
54+
button1.addActionListener(lforButton);
55+
56+
/**Create Textfield */
57+
JTextField textfield1 = new JTextField("",15);
58+
textfield1.setToolTipText("Type something");
59+
60+
ListenforKeys lforKeys = new ListenforKeys();
61+
textfield1.addKeyListener(lforKeys);
62+
63+
/**Create Text Area */
64+
JTextArea textArea1 = new JTextArea(15,20);
65+
66+
textArea1.setText("Tracking events");
67+
68+
/**Whenever you reach the end of the line, you go to another line */
69+
textArea1.setLineWrap(true);
70+
71+
/**Makes sure words doesn't get broken up when line breaking. */
72+
textArea1.setWrapStyleWord(true);
73+
74+
/**Show number of lines in writeen in textArea1. */
75+
int numOfLines = textArea1.getLineCount();
76+
textArea1.append("Number of Lines: " + numOfLines);
77+
78+
/**Set focus to textArea1 */
79+
textArea1.requestFocus();
80+
81+
/**Add Scroll Bar to the scrollPane1 */
82+
JScrollPane scrollPane1 = new JScrollPane(textArea1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
83+
84+
ListenforWindow lForWindow = new ListenforWindow();
85+
this.addWindowListener(lForWindow);
86+
87+
ListenforMouse lForMouse = new ListenforMouse();
88+
panel.addMouseListener(lForMouse);
89+
90+
/**Add containers to panel */
91+
panel.add(label1);
92+
panel.add(button1);
93+
panel.add(textfield1);
94+
panel.add(scrollPane1);
95+
/**Add panel to frame */
96+
this.add(panel);
97+
98+
/**Make the Frame Visible */
99+
this.setVisible(true);
100+
101+
/**Set Highlight to textArea1 */
102+
textArea1.requestFocus();
103+
104+
}
105+
106+
/**Listeners */
107+
108+
private class ListenforButton implements ActionListener{
109+
public void actionPerformed(ActionEvent e){
110+
if(e.getSource() == button1){
111+
buttonClicked++;
112+
textArea1.append("Button Click Count: " + buttonClicked + " times\n");
113+
}
114+
}
115+
}
116+
117+
private class ListenforKeys implements KeyListener{
118+
public void keyPressed(KeyEvent e){
119+
textArea1.append("Key Hit: " + e.getKeyChar() + "\n");
120+
}
121+
}
122+
123+
private class ListenforWindow implements WindowListener{
124+
public void windowActivated(WindowEvent e){
125+
textArea1.append("Window is active");
126+
}
127+
public void windowDeactivated(WindowEvent e){
128+
textArea1.append("Window is not active");
129+
}
130+
}
131+
132+
private class ListenforMouse implements MouseListener{
133+
public void mouseClicked(MouseEvent e){
134+
textArea1.append("Mouse Panel pos: " + e.getX()+ "" + e.getY()+"\n");
135+
textArea1.append("Mouse Screen pos: " + e.getXOnScreen()+ "" + e.getYOnScreen()+"\n");
136+
textArea1.append("Mouse Button Clicked: " + e.getButton()+"\n");
137+
textArea1.append("Mouse Button Clicked: " + e.getClickCount()+"\n");
138+
139+
140+
141+
}
142+
}
143+
}

Swing/readme.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
* Supports Pluggable Looks and Feel (PLAF).
77

88
### Notes
9-
* Swing Classes always satrt from letter J.
10-
* Swing uses modified version of [MVC](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) that combines the **View** and the **Controller** in to a single logical entitiy called **UI delegate.** So it is also called Model Delegate Architecture.
9+
* Swing Classes always start from letter J.
10+
* Swing uses modified version of [MVC](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) that combines the **View** and the **Controller** in to a single logical entitiy called **UI delegate.** So it is also called **Model Delegate Architecture.**
1111
* JWindow, JFrame, JDialog and JApplet are top level containers.
12-
* [Java This Keyword](https://www.youtube.com/watch?v=hUZ4jQmgwi4)
13-
12+
* [Java 'this' Keyword](https://www.youtube.com/watch?v=hUZ4jQmgwi4)
1413

1514
### Basic Steps for Using Swing
1615
1. `import javax.swing.*;`
@@ -20,7 +19,7 @@
2019
5. Add needed components in the Container.
2120
6. Make the Container object visible.
2221

23-
![Basic Swing Components](https://imgur.com/a/NipyNcG)
22+
![Basic Swing Components](https://i.imgur.com/jH4zLoj.jpg)
2423

2524
**Example 1:** Basics Panel, Label, Button, Text Area.
2625
```Java

0 commit comments

Comments
 (0)