Skip to content

Commit 7e073fc

Browse files
committed
Added the ability to toggle between light and dark mode
1 parent 6464123 commit 7e073fc

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/MainFrame.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818
import javax.swing.event.ChangeEvent;
1919
import javax.swing.event.ChangeListener;
2020

21+
import com.formdev.flatlaf.FlatDarkLaf;
22+
import com.formdev.flatlaf.FlatLaf;
23+
import com.formdev.flatlaf.FlatLightLaf;
24+
2125
public class MainFrame extends JFrame implements ActionListener, ComponentListener, ChangeListener, KeyListener {
2226
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
2327

2428
JSpinner widthField, heightField;
2529

26-
JCheckBox resizable;
30+
JCheckBox resizable, darkMode;
2731

2832
JPanel buttonPanel;
2933
JButton resizeButton, exitButton;
3034

31-
int widthSize = 325, heightSize = 180;
35+
int widthSize = 325, heightSize = 212;
3236

3337
public MainFrame() {
3438
// Width panel
@@ -45,8 +49,12 @@ public MainFrame() {
4549
resizable = setUpCheckBox("Resize with mouse and maximize/restore button", true);
4650
setUpPanel(2).add(resizable);
4751

52+
// Toggle between light and dark mode
53+
darkMode = setUpCheckBox("Dark mode", true);
54+
setUpPanel(3).add(darkMode);
55+
4856
// Buttons
49-
buttonPanel = setUpPanel(3);
57+
buttonPanel = setUpPanel(4);
5058
resizeButton = setUpButton("Resize");
5159
exitButton = setUpButton("Exit");
5260

@@ -109,6 +117,14 @@ public void actionPerformed(ActionEvent e) {
109117
changeWindowSize();
110118
} else if (source == resizable) {
111119
this.setResizable(!this.isResizable());
120+
} else if (source == darkMode) {
121+
if (!FlatLaf.isLafDark()) {
122+
FlatDarkLaf.setup();
123+
FlatLaf.updateUI();
124+
} else {
125+
FlatLightLaf.setup();
126+
FlatLaf.updateUI();
127+
}
112128
} else if (source == exitButton) {
113129
System.exit(0);
114130
}

0 commit comments

Comments
 (0)