September 21st, 2022

OOP for GUI

  • To terminate program when window closes: setDefaultCloseOperation(EXIT_ON_CLOSE) (method on JFrame)

This somehow runs code as thread safe (avoid race conditions?):

public static void main(String [] args) {
	public void run() {
		new Main();
	}
}

MyRun myRun = new MyRun();
java.awt.EventQueue.invokeLater(myRun);

or:

java.awt.EventQueue.invokeLater(new Runnable() {
	public void run() {
		new Main();
	}
});
  • BorderLayout: like Finder, where there’s 1-4 fixed-width border areas and a variable-width content area.
    • add(new Border(), BorderLayout.WEST)
  • Nest JPanel as much as you’d like (like <div>)

Java

  • public final int = 3;
    • You can never change this value, it’s a constant
  • public static int = 3;
    • This applies to all items of the class, and it’s shared and only stored once.
    • Can be called on the class itself without having an instance (Rect.printCount()).
  • To compare string contents, we do s1.equals(s2).