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