-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistoryPanel.java
More file actions
75 lines (65 loc) · 2.68 KB
/
HistoryPanel.java
File metadata and controls
75 lines (65 loc) · 2.68 KB
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
import java.awt.*;
import javax.swing.*;
/**
* HistoryPanel class represents the history view of the calculator.
* It displays a list of previous calculations and provides functionality to clear the history.
*/
public class HistoryPanel extends JPanel {
private final JTextArea historyArea; // Area to display calculation history
private final CalculatorLogic calculator; // Reference to calculator logic
private final JButton clearButton; // Button to clear history
/**
* Constructor initializes the history panel and sets up the UI components
* @param calculator Reference to the calculator logic
*/
public HistoryPanel(CalculatorLogic calculator) {
this.calculator = calculator;
setLayout(new BorderLayout());
setBackground(Color.BLACK);
// Create history display area
historyArea = new JTextArea();
historyArea.setEditable(false);
historyArea.setFont(new Font("Digital-7", Font.PLAIN, 18));
historyArea.setLineWrap(true);
historyArea.setWrapStyleWord(true);
historyArea.setBackground(Color.BLACK);
historyArea.setForeground(Color.WHITE);
historyArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Create scroll pane for history area
JScrollPane scrollPane = new JScrollPane(historyArea);
scrollPane.setBackground(Color.BLACK);
scrollPane.setBorder(null);
add(scrollPane, BorderLayout.CENTER);
// Create panel for Clear History button
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(Color.BLACK);
clearButton = new JButton("Clear History");
clearButton.setFont(new Font("Arial", Font.BOLD, 14));
clearButton.setBackground(new Color(165, 165, 165));
clearButton.setForeground(Color.BLACK);
clearButton.addActionListener(e -> clearHistory());
clearButton.setFocusPainted(false);
buttonPanel.add(clearButton);
add(buttonPanel, BorderLayout.SOUTH);
// Set up timer for automatic history updates
Timer timer = new Timer(1000, e -> updateHistory());
timer.start();
}
/**
* Updates the history display area with current calculation history
*/
private void updateHistory() {
StringBuilder sb = new StringBuilder();
for (String calc : calculator.getHistory()) {
sb.append(calc).append("\n");
}
historyArea.setText(sb.toString());
}
/**
* Clears all calculation history
*/
private void clearHistory() {
calculator.getHistory().clear();
updateHistory();
}
}