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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| public class CardLayoutChangeByMethodByInspection {
private static final Color[] COLORS = {Color.ORANGE, Color.GREEN, Color.YELLOW, Color.CYAN};
private static final String TIME_LABEL_COMPONENT = "timelabel";
public static void main(String[] args) {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardLayout cardLayout = new CardLayout();
JPanel mainPanel = new JPanel(cardLayout);
Model model = new Model();
ComponentAdapter componentAdapter = new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent arg0) {
model.updatePanelTime(getName(arg0.getComponent().getBackground()));
}
};
for(Color color : COLORS) {
JPanel cardPanel = createPanel(color, model);
cardPanel.addComponentListener(componentAdapter);
mainPanel.add(cardPanel);
}
JButton button = new JButton("Change");
button.addActionListener(e-> cardLayout.next(mainPanel));
JPanel southPanel = new JPanel();
southPanel.add(button);
frame.add(mainPanel);
frame.add(southPanel, BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
model.updatePanelTime(getName(getVisible(mainPanel).getBackground()));
}
public void windowClosing(java.awt.event.WindowEvent e) {
for(Map.Entry<String, String> times : model.getTimes()) {
String time = times.getValue();
if ( time.isEmpty() ) {
time = "jamais affiché";
}
System.out.println("Heure du panneau " + times.getKey().toLowerCase() + " : " + time);
}
}
});
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected static JPanel getVisible(JPanel panel) {
for(Component component : panel.getComponents()) {
if ( component instanceof JPanel && component.isVisible() ) {
return (JPanel)component;
}
}
return null;
}
private static String getName(Color color) {
for(Field field : Color.class.getFields()) {
try {
if ( color==field.get(null) ) {
return field.getName();
}
} catch (IllegalArgumentException | IllegalAccessException e) {
}
}
return "?";
}
private static String getTime() {
return DateTimeFormatter.ofPattern("hh:mm:ss").format(LocalDateTime.now());
}
public static JPanel createPanel(Color color, Model description) {
String id = getName(color);
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(color);
JLabel timeLabel = new JLabel(); // le label qui va afficher l'heure
description.add(id, e-> timeLabel.setText((String)(e.getNewValue())));
timeLabel.setHorizontalAlignment(JLabel.CENTER);
timeLabel.setName(TIME_LABEL_COMPONENT);
panel.add(timeLabel, BorderLayout.NORTH);
JLabel label2 = new JLabel(id); // un autre label
label2.setHorizontalAlignment(JLabel.CENTER);
panel.add(label2, BorderLayout.SOUTH);
return panel;
}
public static class Model {
private final SwingPropertyChangeSupport propertyChangeSupport;
private Map<String, String> times;
public Model() {
times=new HashMap<>();
propertyChangeSupport=new SwingPropertyChangeSupport(this);
}
public Set<Map.Entry<String,String>> getTimes() {
return Collections.unmodifiableSet(times.entrySet());
}
public void add(String id, PropertyChangeListener listener) {
if ( !times.containsKey(id) ) {
times.put(id,"");
propertyChangeSupport.addPropertyChangeListener(id, listener);
}
}
public void updatePanelTime(String id) {
if ( times.containsKey(id) ) {
String newtime=getTime();
String old=times.put(id, newtime);
propertyChangeSupport.firePropertyChange(id, old, newtime);
}
}
public String getPanelTime(String id) {
return times.getOrDefault(id, "");
}
}
} |
Partager