-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwingControlDemo.java
More file actions
116 lines (92 loc) · 3.21 KB
/
SwingControlDemo.java
File metadata and controls
116 lines (92 loc) · 3.21 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo(){
prepareGUI();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(500,300);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
headerLabel.setOpaque(true);
headerLabel.setBackground(Color.green);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setForeground(Color.RED);
statusLabel.setOpaque(true);
statusLabel.setBackground(Color.YELLOW);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
//Creation dun Control Panel
controlPanel = new JPanel();
//Assignation d'un Layout (FlowLayout)
controlPanel.setLayout(new FlowLayout());
//Ajout des composants au JFrame
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
//Pour centrer le JFrame a lecran
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
mainFrame.setLocation(dim.width/2 - mainFrame.getWidth()/2, dim.height/2 - mainFrame.getHeight()/2);
//Rendre le JFrame visible (pas visible par défaut)
mainFrame.setVisible(true);
}
private void showEventDemo(){
headerLabel.setText("Control in action: Button");
JButton okButton = new JButton("OK");
JButton submitButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
controlPanel.addMouseListener(new CustomMouseListener());
mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals( "OK" )) {
statusLabel.setText("Ok Button clicked.");
}
else if( command.equals( "Submit" ) ) {
statusLabel.setText("Submit Button clicked.");
}
else {
statusLabel.setText("Cancel Button clicked.");
}
}
}
private class CustomMouseListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
statusLabel.setText("Mouse Clicked: ("+e.getX()+", "+e.getY() +")");
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showEventDemo();
}
}