forked from yesiamrajeev/Hacktoberfest2025-Portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle dark mode.java
More file actions
51 lines (43 loc) · 1.53 KB
/
toggle dark mode.java
File metadata and controls
51 lines (43 loc) · 1.53 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DarkModeExample extends JFrame {
private boolean darkMode = false;
private JPanel panel;
private JButton toggleButton;
private JLabel label;
public DarkModeExample() {
setTitle("Dark Mode Example");
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new BorderLayout());
label = new JLabel("Click the button to toggle Dark Mode", SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 16));
toggleButton = new JButton("Enable Dark Mode");
toggleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
darkMode = !darkMode;
updateMode();
}
});
panel.add(label, BorderLayout.CENTER);
panel.add(toggleButton, BorderLayout.SOUTH);
add(panel);
}
private void updateMode() {
if (darkMode) {
panel.setBackground(Color.DARK_GRAY);
label.setForeground(Color.WHITE);
toggleButton.setText("Disable Dark Mode");
} else {
panel.setBackground(Color.WHITE);
label.setForeground(Color.BLACK);
toggleButton.setText("Enable Dark Mode");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new DarkModeExample().setVisible(true));
}
}