Blame view

src/tp2/getSource/MultiButt.java 1.46 KB
3c9d20ad   shaggy42089   nearly finished tp2
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
  package tp2.getSource;
  
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  public class MultiButt extends JFrame {
  
      private final JLabel label;
      private final int nbButtons = 3;
  
      public MultiButt() throws HeadlessException {
          super();
          this.setLocationRelativeTo(null);
          BoxLayout layout = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
          this.setLayout(layout);
  
          this.label = new JLabel("Bouton 0");
          label.setFont(new Font("SansSerif", Font.PLAIN, 25));
  
          JPanel topPanel = new JPanel();
          topPanel.add(label);
          this.add(topPanel);
  
          ActionListener listener = new ReponseAuClic2();
  
          for (int i = 0;i < nbButtons; ++i){
              JPanel tmp = new JPanel();
              JButton butt = new JButton("Bouton "+Integer.toString(i));
              butt.addActionListener(listener);
              tmp.add(butt);
              this.add(tmp);
          }
  
          this.setResizable(false);
          this.pack();
  
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setVisible(true);
      }
  
      private class ReponseAuClic2 implements ActionListener {
  
          public ReponseAuClic2() {
          }
  
          @Override
          public void actionPerformed(ActionEvent e) {
              label.setText(((JButton) e.getSource()).getText());
          }
      }
  
      public static void main(String[] args) {
          new MultiButt();
      }
  }