GUI Examples //Example 1 ******************************************************** /*********** Creates a simple frame ***********/ import java.awt.Color; import javax.swing.JFrame; public class GUIFrameProject { public static void main(String[] args) { JFrame frame = new JFrame("My Frame"); frame.setSize(500, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } //Example 2 ******************************************************** /****** Creates a simple frame with a few components ******/ import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class GUISimpleStartWithPanel { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; public static void main(String[] args) { JFrame frame = new JFrame("My Frame"); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); JButton button = new JButton("Press me"); JButton button2 = new JButton("Press me also"); JLabel label = new JLabel("I'm a Label"); JPanel panel = new JPanel(); panel.add(button); panel.add(button2); panel.add(label); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } //Example 3 ******************************************************** /***** Shows how to extend a JFrame *******/ import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class FilledFrame extends JFrame { JButton button = new JButton("Press me"); private JLabel label = new JLabel("I'm a Label"); private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; public FilledFrame() { super("My Frame"); createComponents(); this.setSize(FRAME_WIDTH, FRAME_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void createComponents() { JPanel panel = new JPanel(); panel.setBackground(Color.red); panel.add(button); panel.add(label); this.add(panel); } public static void main(String[] args) { FilledFrame frame = new FilledFrame(); } } //Example 4 ******************************************************** /* ***** Adding functionality to button press: We create a new action listener * class and add an instance of it to our button. When the button is clicked * "I was clicked" is printed on the console. */ import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class FilledFrame extends JFrame { JButton button; private JLabel label; private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; public FilledFrame() { super("My Frame"); createComponents(); this.setSize(FRAME_WIDTH, FRAME_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void createComponents() { button = new JButton("Press me"); label = new JLabel("I'm a Label"); button.addActionListener(new ClickListener()); JPanel panel = new JPanel(); panel.setBackground(new Color(150,150,255)); panel.add(button); panel.add(label); this.add(panel); } public static void main(String[] args) { FilledFrame frame = new FilledFrame(); } } /** The Button Listener class */ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("I was clicked"); } } //Example 5 ******************************************************** /* Shows how to create a local inner class: * Create a new action listener class inside of the FilledFrame class and * add an instance of it to our button. Note: Methods of inner classss can * access the instance variables and methods of the surrounding class. */ import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class FilledFrame extends JFrame { JButton button; private JLabel label; private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { label.setText("I was clicked"); } } public FilledFrame() { super("My Frame"); createComponents(); this.setSize(FRAME_WIDTH, FRAME_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void createComponents() { button = new JButton("Press me"); label = new JLabel("I'm a Label"); button.addActionListener(new ClickListener()); JPanel panel = new JPanel(); panel.setBackground(Color.red); panel.add(button); panel.add(label); this.add(panel); } public static void main(String[] args) { FilledFrame frame = new FilledFrame(); } } //Example 6 ******************************************************** /* Same as Part 5 except it uses an anonymous inner class. */ import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class FilledFrame extends JFrame { JButton button; private JLabel label; private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; public FilledFrame() { super("My Frame"); createComponents(); this.setSize(FRAME_WIDTH, FRAME_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void createComponents() { button = new JButton("Press me"); label = new JLabel("I'm a Label"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { label.setText("I was clicked"); } }); JPanel panel = new JPanel(); panel.setBackground(Color.red); panel.add(button); panel.add(label); this.add(panel); } public static void main(String[] args) { FilledFrame frame = new FilledFrame(); } } //Example 7 ******************************************************** /** Shows how to create a program with a drawable area */ import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.JFrame; public class DrawingProject { JFrame myFrame; DrawArea myDrawArea; final int FRAME_WIDTH = 256; final int FRAME_HEIGHT = 512; public DrawingProject() { myFrame = new JFrame("Simple Drawing Program)"); myFrame.setSize(FRAME_WIDTH, FRAME_WIDTH); myDrawArea = new DrawArea(); myFrame.add(myDrawArea); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setVisible(true); } public static void main(String[] args) { DrawingProject myFrame = new DrawingProject(); } } /** A component class for drawing */ import java.awt.Color; import java.awt.Graphics; import javax.swing.JComponent; public class DrawArea extends JComponent { public void paintComponent(Graphics g) { for (int row = 0; row< this.getHeight(); row++) { for (int col = 0 ; col < this.getWidth(); col++) { g.setColor(new Color(row % 256, 0, 255)); g.drawLine(col,row, col, row+1); } } g.drawRect(10, 20, 40, 60); } }