challengee

This commit is contained in:
Josh Ashton
2023-11-15 10:48:03 -07:00
parent edce1d4ff9
commit 2feee0f8e8
11 changed files with 39 additions and 23 deletions
+19 -14
View File
@@ -1,43 +1,44 @@
import javax.swing.JButton; import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color; import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Button extends JButton { public class Button extends JButton {
private boolean selected; private boolean selected;
private Color bg; private Color bg;
private Color fg; private Color fg;
/**
* @param String buttonLabel
* @param Color fg
* @param Color bg
*/
public Button(String buttonLabel, Color bg, Color fg) { public Button(String buttonLabel, Color bg, Color fg) {
super(buttonLabel); super(buttonLabel);
this.bg = bg; this.bg = bg;
this.fg = fg; this.fg = fg;
applyCommon();
deselect(); deselect();
} }
// Apply default appearance to the button upon exiting the bounds.
public void deselect() { public void deselect() {
selected = false; selected = false;
applyDefaultStyle();
}
public void select() {
selected = true;
applyFocusedStyle();
}
private void applyDefaultStyle() {
setBackground(bg); setBackground(bg);
setForeground(fg); setForeground(fg);
} }
private void applyFocusedStyle() { // Apply focused appearance to the button upon hover.
public void select() {
selected = true;
setBackground(fg); setBackground(fg);
setForeground(bg); setForeground(bg);
} }
// Apply hover functionality to all buttons, as well as removing certain default stylings.
private void applyCommon() { private void applyCommon() {
addMouseListener( addMouseListener(
new MouseListener() { new MouseListener() {
@@ -53,11 +54,15 @@ public class Button extends JButton {
@Override @Override
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
select(); select();
repaint();
revalidate();
} }
@Override @Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
deselect(); deselect();
repaint();
revalidate();
} }
}); });
+2 -5
View File
@@ -1,9 +1,6 @@
import javax.swing.JFrame; import javax.swing.*;
import javax.swing.UIManager;
import java.awt.Color; import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
public class Gui extends JFrame { public class Gui extends JFrame {
public Gui() { public Gui() {
+18 -4
View File
@@ -1,7 +1,6 @@
import javax.swing.JPanel; import javax.swing.*;
import java.awt.BorderLayout; import java.awt.*;
import java.awt.Color;
public class Panel extends JPanel { public class Panel extends JPanel {
private Color bg; private Color bg;
@@ -19,6 +18,21 @@ public class Panel extends JPanel {
setBackground(bg); setBackground(bg);
setForeground(fg); setForeground(fg);
add(new Button("Example text", new Color(160, 32, 240), Color.WHITE)); Button b = new Button("Example text", new Color(160, 32, 240), Color.WHITE);
b.addActionListener(
e -> {
System.out.println("Button action.");
}
);
Button b2 = new Button("Example 2 text", new Color(160, 32, 240), Color.WHITE);
b2.addActionListener(
e -> {
System.out.println("Button 2 action.");
}
);
add(b);
add(b2);
} }
} }
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.