FenetreIncrementer.java 1.72 KB
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();
    }
}