https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

https://www.cs.indiana.edu/classes/c212-dgerman/spr2012/classNotes/chapter10.html


import javax.swing.*; 

class Two {
  public static void main(String[] args) {
    JFrame frame = new JFrame(); 

    JButton b1 = new JButton("Click me!"); 
    JButton b2 = new JButton("Click me!"); 
    JButton b3 = new JButton("Click me, me, me, I am the most important one!"); 
    JButton b4 = new JButton("Click me!"); 
    JLabel label = new JLabel("Hello, World!"); 
 
    JPanel panel = new JPanel();  
    panel.add(b1); 
    panel.add(b2); 
    panel.add(label); 
    panel.add(b3); 
    panel.add(b4); 

    frame.add(panel); 

    frame.setSize(300, 100); 
    frame.setTitle("An empty frame"); 
    // frame.setDefaultCloseOperation(3); // better: JFrame.EXIT_ON_CLOSE
    frame.setVisible(true); 
  }
}

There is a default layout associated with every container. 

JPanel is a Container and so is JFrame. 

JPanel's default layout is FlowLayout. 

Each layout has a layout manager associated with it. 

In the summary for this lab the link in this sentence is relevant:

  If you want to place components in specific places you need to 
  work with the layout manager, perhaps disabling it, etc.

We also ran this but for no reason:

import javax.swing.*; 
import java.awt.event.*;
import java.awt.*; 

class Six extends JFrame {
  Seven component; 
  Six(String title, int width) {
    this.component = new Seven(); 
    this.add( this.component );
    this.setSize(width, width); 
    this.setTitle(title); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    this.setVisible(true); 
  }
  public static void main(String[] args) {
    Six frame = new Six("Finding PI", 600); 
    frame.component.calculate(); 
  }
}

class Seven extends JComponent {
  int times, size, inside, total; 
  Point center; 
  public void paintComponent(Graphics g) {
    this.size = this.getHeight(); 
    this.center = new Point(size/2, size/2);
    this.total = this.inside = 0; 
    g.setColor(Color.BLUE); 
    g.drawOval(0, 0, this.size, this.size);
    if (this.times > 0) { 
      for (int i = 0; i < this.times; i = i + 1) {
        Point p = new Point(size); 
        if (p.distanceTo(this.center) < size/2) { 
          this.inside += 1; 
          g.setColor(Color.RED); 
        } else 
          g.setColor(Color.BLUE); 
        g.drawLine(p.x, p.y, p.x, p.y); 
        this.total += 1;       
      } 
      System.out.println( "In " + this.times + " simulation steps PI is estimated to " + 4.0 * this.inside / this.total ); 
    } 
  } 
  void calculate() {
    String input = JOptionPane.showInputDialog("How many times?"); 
    if (input == null || input.equals("bye")) 
      System.exit(0); 
    else {
      try{ 
        this.times = Integer.parseInt( input ); 
        this.repaint(); 
      } catch (Exception e) {
        System.out.println("Please enter a number or bye."); 
      } finally {
        this.calculate(); 
      }
    } 
  } 
}

class Point { 
  int x, y; 
  Point(int size) {
    this((int)(Math.random() * size), (int)(Math.random() * size)); 
  }
  Point(int x, int y) {
    this.x = x; 
    this.y = y; 
  } 
  double distanceTo(Point other) {
    return Math.sqrt( Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2) ); 
  } 
}

--

import java.awt.event.*;
import javax.swing.*;

class Two extends JFrame implements ActionListener, KeyListener {
  public void keyPressed (KeyEvent e) { }
  public void keyReleased(KeyEvent e) { }
  public void keyTyped   (KeyEvent e) {
    System.out.println(" kieo;wry34rt ");
    totalLabel.setText("");
  }
  public void actionPerformed(ActionEvent e) {
    // System.out.println( e );
    totalLabel.setText( "" + Math.sqrt( Double.parseDouble( numberTextField.getText() )));
  }
  JTextField numberTextField;
  JLabel numberLabel, totalLabel;
  Two() {
    JButton action;
    JPanel panel = new JPanel();
    panel.setLayout( null );
    numberLabel = new JLabel("Number: ");
    numberLabel.setBounds(30, 30, 120, 20);
    panel.add(numberLabel);
    numberTextField = new JTextField();
    //  numberTextField.setPreferredSize( new Dimension(60,20));
    numberTextField.setBounds(70, 70, 60, 20);
    numberTextField.addKeyListener( this );
    panel.add(numberTextField);
    action = new JButton( "Calculate" );
    action.setBounds(100, 100, 100, 20);
    panel.add( action );
    action.addActionListener( this );
    totalLabel = new JLabel( "..." );
    totalLabel.setBounds(150, 150, 160, 20);
    panel.add(totalLabel);
    this.add(panel);
    this.setSize(400, 200);
    this.setVisible(true);
    this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  }
  public static void main(String[] args) {
    Two frame = new Two();
  }
}

Minute paper: name (for attendance)
              feedback for your lab instructors (confidential)
                       name of ui/ai and comment/suggestion (just one)


--

Data Structures

Trees 

--

Streams

Generic Classes