Blame view

src/tp2/getSource/MultiButt.java 1.43 KB
3c9d20ad   shaggy42089   nearly finished tp2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  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);
  
5d736557   shaggy42089   tp2 done
19
          this.label = new JLabel(" ");
3c9d20ad   shaggy42089   nearly finished tp2
20
21
22
23
24
25
          label.setFont(new Font("SansSerif", Font.PLAIN, 25));
  
          JPanel topPanel = new JPanel();
          topPanel.add(label);
          this.add(topPanel);
  
5d736557   shaggy42089   tp2 done
26
          ActionListener listener = new ClicListener();
3c9d20ad   shaggy42089   nearly finished tp2
27
28
29
  
          for (int i = 0;i < nbButtons; ++i){
              JPanel tmp = new JPanel();
5d736557   shaggy42089   tp2 done
30
              JButton butt = new JButton("Bouton "+ i);
3c9d20ad   shaggy42089   nearly finished tp2
31
32
33
34
35
36
37
38
39
40
41
42
              butt.addActionListener(listener);
              tmp.add(butt);
              this.add(tmp);
          }
  
          this.setResizable(false);
          this.pack();
  
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setVisible(true);
      }
  
5d736557   shaggy42089   tp2 done
43
      private class ClicListener implements ActionListener {
3c9d20ad   shaggy42089   nearly finished tp2
44
  
5d736557   shaggy42089   tp2 done
45
          public ClicListener() {
3c9d20ad   shaggy42089   nearly finished tp2
46
47
48
49
50
51
52
53
54
55
56
57
          }
  
          @Override
          public void actionPerformed(ActionEvent e) {
              label.setText(((JButton) e.getSource()).getText());
          }
      }
  
      public static void main(String[] args) {
          new MultiButt();
      }
  }