1 package finestraconpannelli;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 public class FinestraConPannelli {
7 private static Color coloreCerchio = Color.RED;
8
9 public static void main(String[] args) {
10 SwingUtilities.invokeLater(() -> {
11 JFrame frame = new JFrame("Finestra con Pannelli");
12 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13
14 frame.setSize(400, 400);
15
16
17 JPanel panelCentrale = new JPanel() {
18 @Override
19 protected void paintComponent(Graphics g) {
20 super.paintComponent(g);
21 g.setColor(coloreCerchio);
22 int centerX = getWidth() / 2;
23 int centerY = getHeight() / 2;
24 int radius = 15;
25
26 g.fillOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
27
28
29 g.setColor(Color.BLACK);
30 ((Graphics2D) g).setStroke(new BasicStroke(2));
31 g.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
32 }
33 };
34
35
36 JPanel panelBasso = new JPanel();
37 JButton pulsante = new JButton("Cliccami");
38 pulsante.setPreferredSize(new Dimension(100, 30));
39 panelBasso.add(pulsante);
40
41
42 pulsante.addActionListener(e -> {
43
44 if (coloreCerchio == Color.RED) {
45 coloreCerchio = Color.ORANGE;
46 } else {
47 coloreCerchio = Color.RED;
48 }
49 panelCentrale.repaint();
50 });
51
52
53
54
55
56
57
58 frame.setLocationRelativeTo(null);
59
60
61
62 frame.add(panelCentrale, BorderLayout.CENTER);
63 frame.add(panelBasso, BorderLayout.SOUTH);
64 frame.setVisible(true);
65 });
66 }
67 } |