FenetreIncrementer.java
1.72 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
package tp2.evenements;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FenetreIncrementer extends JFrame{
private final JLabel label;
public FenetreIncrementer() throws HeadlessException {
super();
this.setLocationRelativeTo(null);
BoxLayout layout = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
this.setLayout(layout);
this.label = new JLabel("0");
label.setFont(new Font("SansSerif", Font.PLAIN, 25));
JPanel topPanel = new JPanel();
topPanel.add(label);
JButton incbutt = new JButton("incrémenter");
//question 4
//utilisation des sous-classes internes
//incbutt.addActionListener(new ReponseAuClic());
incbutt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String old = label.getText();
label.setText(Integer.toString(Integer.parseInt(old)+1));
}
});
JPanel botPanel = new JPanel();
botPanel.add(incbutt);
this.add(topPanel);
this.add(botPanel);
this.setResizable(false);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private class ReponseAuClic implements ActionListener {
public ReponseAuClic() {}
@Override
public void actionPerformed(ActionEvent e) {
String old = label.getText();
label.setText(Integer.toString(Integer.parseInt(old)+1));
}
}
public static void main(String[] args) {
new FenetreIncrementer();
}
}