2016-12-20

posted Dec 20, 2016, 6:29 AM by Samuel Konstantinovich   [ updated Dec 20, 2016, 6:33 AM ]
Homework:
1. Write static methods CtoF(double t) and FtoC(double t) that converts the given temperature from/to the indicated scale.
CtoF(-40.0) -> -40.0
CtoF(100.0) -> 212.0
FtoC(212.0) -> 100.0

We will use these tomorrow.

2. Review some java GUI (graphical user interface) code so that we can start tomorrow with how to make it do things!

note : These java programs must be run on a local terminal, not a remote terminal session (ssh) or tty session.

Basic Window:
import javax.swing.*;
public class Window extends JFrame {
    public Window() {
        this.setTitle("My first GUI");
        this.setSize(600,400);
        this.setLocation(100,100);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Window g = new Window();
        g.setVisible(true);
    }
}


Window With some stuff added:
Basically you m ake some "things" and then add them to the content pane. 


import javax.swing.*;
import java.awt.*;
public class Window2 extends JFrame {
private Container pane;

private JButton b;
private JLabel l;
private JTextField t;
private JCheckBox c;
 
  //CONSTRUCTOR SETS EVERYTHING UP
  public Window2() {
     this.setTitle("My first GUI");
     this.setSize(600,400);
     this.setLocation(100,100);
     this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
     pane = this.getContentPane();
     pane.setLayout(new FlowLayout());  //NOTE!! THIS CAN BE CHANGED (see below)
    
     b = new JButton("Do Nothing");
     l = new JLabel("This is AWESOME! (lies)",null,JLabel.CENTER);
     t = new JTextField(12);
     c = new JCheckBox("Overclock!!!");
     pane.add(l);
     pane.add(b);
     pane.add(t);
     pane.add(c);
}

  //MAIN JUST INSTANTIATES + MAKE VISIBLE
  public static void main(String[] args) {
     Window2 g = new Window2();
     g.setVisible(true);
  }
}

LAYOUTS CAN MAKE A HUGE DIFFENCE:
Comments