basic gui implemented for week11 challenge.

This commit is contained in:
Josh Ashton
2023-11-15 10:14:25 -07:00
parent 23054afaba
commit edce1d4ff9
5 changed files with 136 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
public class App {
public static void main(String[] args) {
Gui g = new Gui();
}
}
+70
View File
@@ -0,0 +1,70 @@
import javax.swing.JButton;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Button extends JButton {
private boolean selected;
private Color bg;
private Color fg;
public Button(String buttonLabel, Color bg, Color fg) {
super(buttonLabel);
this.bg = bg;
this.fg = fg;
deselect();
}
public void deselect() {
selected = false;
applyDefaultStyle();
}
public void select() {
selected = true;
applyFocusedStyle();
}
private void applyDefaultStyle() {
setBackground(bg);
setForeground(fg);
}
private void applyFocusedStyle() {
setBackground(fg);
setForeground(bg);
}
private void applyCommon() {
addMouseListener(
new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {
select();
}
@Override
public void mouseExited(MouseEvent e) {
deselect();
}
});
setFocusPainted(false);
setBorder(new EmptyBorder(3,3,3,3));
repaint();
revalidate();
}
}
+31
View File
@@ -0,0 +1,31 @@
import javax.swing.JFrame;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Dimension;
public class Gui extends JFrame {
public Gui() {
super("Java Swing Challenge");
init();
add(new Panel(Color.DARK_GRAY, Color.WHITE));
setVisible(true);
}
private void init() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(new Dimension(750, 550));
setResizable(false);
setLocationRelativeTo(null);
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Error with cross platform look and feel.");
}
}
}
+24
View File
@@ -0,0 +1,24 @@
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
public class Panel extends JPanel {
private Color bg;
private Color fg;
public Panel(Color bg, Color fg) {
super();
this.bg = bg;
this.fg = fg;
init();
}
private void init() {
setBackground(bg);
setForeground(fg);
add(new Button("Example text", new Color(160, 32, 240), Color.WHITE));
}
}
Executable
+6
View File
@@ -0,0 +1,6 @@
#!/bin/zsh
javac *.java
java App
rm *.class