Swing Basics

References

java.sun.com/docs/books/tutorial/uiswing/

http://java.sun.com/products/jfc/tsc/articles/architecture/

 

A basic application frame

import javax.swing.*;

public class AppFrame extends JFrame implements Runnable {

   private static final long serialVersionUID = 1L;

   public AppFrame() {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setDefaultLookAndFeelDecorated(true);
   }

   public void run() {
      pack();
      setVisible(true);
   }

   public void display() {
      SwingUtilities.invokeLater(this);
   }

   public static void main(String[] args) {
      AppFrame app = new AppFrame();
      app.setTitle("Test AppFrame");
      app.display();
   }
}

A basic content panel

 

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

public class GUI1 extends JPanel implements ActionListener {

   private static final long serialVersionUID = 1L;
   private Color color = Color.RED;
   private Color nextColor = Color.GREEN;

   public GUI1() {
      Button b = new Button("Change");
      b.addActionListener(this);
      add(b);
   }

   public void actionPerformed(ActionEvent ae) {
      Color temp = nextColor;
      nextColor = color;
      color = temp;
      repaint();
   }

   public void paintComponent(Graphics gc) {
      super.paintComponent(gc);
      Graphics2D gc2d = (Graphics2D) gc;
      gc2d.setColor(color);
      gc2d.fillOval(20, 20, 50, 50);

   }

   public static void main(String[] args) {
      AppFrame app = new AppFrame();
      app.setTitle("GUI 1");
      GUI1 gui = new GUI1();
      app.setContentPane(gui);
      app.display();
   }
}

Menus

public class GUI2 extends JPanel implements ActionListener {

   private static final long serialVersionUID = 1L;
   private Color color = Color.RED;
   private Color nextColor = Color.GREEN;

   public GUI2() {
      Button b = new Button("Change");
      b.addActionListener(this);
      add(b);
   }

   private JMenu makeMenu() {
      JMenu result = new JMenu("Commands");
      result.setMnemonic('c');
      JMenuItem item = new JMenuItem("Change", 'h');
      item.addActionListener(this);
      result.add(item);
      item = new JMenuItem("Exit", 'x');
      item.addActionListener(this);
      result.add(item);
      return result;
   }

   public void actionPerformed(ActionEvent ae) {
      String cmmd = ae.getActionCommand();
      if (cmmd.equals("Change")) {
         Color temp = nextColor;
         nextColor = color;
         color = temp;
         repaint();
      } else if (cmmd.equals("Exit")) {
         System.exit(0);
      }
   }

   public void paintComponent(Graphics gc) {
      super.paintComponent(gc);
      Graphics2D gc2d = (Graphics2D) gc;
      gc2d.setColor(color);
      gc2d.fillOval(20, 20, 50, 50);

   }

   public static void main(String[] args) {
      AppFrame app = new AppFrame();
      app.setTitle("GUI 2");
      GUI2 gui = new GUI2();
      JMenuBar menuBar = new JMenuBar();
      menuBar.add(gui.makeMenu());
      app.setJMenuBar(menuBar);
      app.setContentPane(gui);
      app.display();
   }
}