FirstSwingFrame.java Page 1 of 1
|
|
- Horace McKenzie
- 6 years ago
- Views:
Transcription
1 FirstSwingFrame.java Page 1 of 1 2: * A first example of using Swing. A JFrame is created with 3: * a label and buttons (which don t yet respond to events). 4: * 5: Andrew Vardy 6: */ 7: import javax.swing.*; 8: import java.awt.flowlayout; 9: 10: public class FirstSwingFrame extends JFrame { 11: public FirstSwingFrame() { 12: super("firstswingframe"); 13: setdefaultcloseoperation(jframe.exit_on_close); 14: setlayout(new FlowLayout()); 15: 16: // You can any Component to the JFrame 17: add(new JLabel("Label")); 18: add(new JButton("Button1")); 19: add(new JButton("Button2")); 20: 21: // Set the size of the JFrame and make it visible 22: setsize(300, 100); 23: setvisible(true); 24: } 25: 26: public static void main(string[] args) throws Exception { 27: // An anonymous inner class created to run on the event- 28: // dispatching thread. 29: SwingUtilities.invokeLater(new Runnable() { 30: public void run() { 31: new FirstSwingFrame(); 32: } 33: }); 34: } 35: }
2 SecondSwingFrame.java Page 1 of 1 2: * Just like FirstSwingFrame only we do not extend 3: * JFrame, we have a JFrame attribute instead. 4: * 5: Andrew Vardy 6: */ 7: 8: import javax.swing.*; 9: import java.awt.flowlayout; 10: 11: public class SecondSwingFrame { 12: 13: JFrame frame = new JFrame("SecondSwingFrame"); 14: 15: public SecondSwingFrame() { 16: frame.setdefaultcloseoperation(jframe.exit_on_close); 17: frame.setlayout(new FlowLayout()); 18: 19: frame.add(new JLabel("Label")); 20: frame.add(new JButton("Button1")); 21: frame.add(new JButton("Button2")); 22: 23: frame.setsize(300, 100); 24: frame.setvisible(true); 25: } 26: 27: public static void main(string[] args) throws Exception { 28: SwingUtilities.invokeLater(new Runnable() { 29: public void run() { 30: new SecondSwingFrame(); 31: } 32: }); 33: } 34: }
3 LayoutPlay.java Page 1 of 2 2: * Demonstrates Swing s component layout features using three different 3: * layout managers: FlowLayout, GridLayout, and BoxLayout. 4: * 5: * The suggested layout manager is GridBagLayout but it is rather complicated. 6: * Alternatively, you can use a GUI-builder such as NetBeans. 7: * 8: Andrew Vardy 9: */ 10: import javax.swing.*; 11: import java.awt.*; 12: 13: public class LayoutPlay extends JFrame { 14: 15: // RedPanel uses the default layout for JPanels --- FlowLayout 16: class RedPanel extends JPanel { 17: public RedPanel() { 18: setbackground(color.red); 19: setpreferredsize(new Dimension(200, 200)); 20: add(new JLabel("x")); add(new JButton("Set")); 21: add(new JLabel("y")); add(new JButton("Set")); 22: add(new JLabel("z")); add(new JButton("Set")); 23: } 24: } 25: 26: // GreenPanel uses GridLayout 27: class GreenPanel extends JPanel { 28: public GreenPanel() { 29: setbackground(color.green); 30: setpreferredsize(new Dimension(200, 200)); 31: setlayout(new GridLayout(3, 2)); 32: add(new JLabel("x")); add(new JButton("Set")); 33: add(new JLabel("y")); add(new JButton("Set")); 34: add(new JLabel("z")); add(new JButton("Set")); 35: } 36: } 37: 38: // BluePanel uses BoxLayout to layout three individual rows of 39: // (label, button) pairs. 40: class BluePanel extends JPanel { 41: public BluePanel() { 42: setbackground(color.blue); 43: setpreferredsize(new Dimension(200, 200)); 44: 45: // Layout the BluePanel vertically. 46: setlayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 47: 48: // Create three rows, each one a horizontally laid out 49: // JPanel with a label and a button. 50: JPanel row1 = new JPanel(), 51: row2 = new JPanel(), 52: row3 = new JPanel(); 53: row1.setlayout(new BoxLayout(row1, BoxLayout.X_AXIS)); 54: row2.setlayout(new BoxLayout(row2, BoxLayout.X_AXIS)); 55: row3.setlayout(new BoxLayout(row3, BoxLayout.X_AXIS)); 56: row1.add(new JLabel("x")); row1.add(new JButton("Set")); 57: row2.add(new JLabel("y")); row2.add(new JButton("Set")); 58: row3.add(new JLabel("z")); row3.add(new JButton("Set")); 59: 60: add(row1); 61: add(row2); 62: add(row3); 63: } 64: } 65: 66: public LayoutPlay() { 67: super("layoutplay"); 68: setdefaultcloseoperation(jframe.exit_on_close); 69: 70: // At the highest level we will use FlowLayout. 71: setlayout(new FlowLayout()); 72: 73: // Add three different panels with differing 74: // layouts, but the same set of labels + buttons 75: add(new RedPanel()); 76: add(new GreenPanel()); 77: add(new BluePanel());
4 LayoutPlay.java Page 2 of 2 78: 79: // The JFrame will choose its initial size 80: // based on its content. 81: pack(); 82: setvisible(true); 83: } 84: 85: public static void main(string[] args) throws Exception { 86: SwingUtilities.invokeLater(new Runnable() { 87: public void run() { 88: new LayoutPlay(); 89: } 90: }); 91: } 92: }
5 Paint1.java Page 1 of 2 2: * First cut at creating a paint program with Swing. This program works 3: * but does not separate the model from the GUI. This will be fixed in 4: * subsequent versions of PaintX. 5: * 6: Andrew Vardy 7: */ 8: import java.awt.*; 9: import java.awt.event.actionevent; 10: import java.awt.event.actionlistener; 11: import java.awt.event.mouseadapter; 12: import java.awt.event.mouseevent; 13: 14: import javax.swing.*; 15: 16: public class Paint1 extends MouseAdapter implements ActionListener { 17: // The underlying model is a 2-D array of booleans. 18: // This grid represents the contents of the canvas. 19: boolean grid[][] = new boolean[400][400]; 20: 21: JFrame frame = new JFrame("Paint1"); 22: PaintPanel paintpanel = new PaintPanel(); 23: JButton clearbutton = new JButton("Clear"); 24: 25: public Paint1() { 26: // Initialize frame and add the paintpanel in the center 27: frame.setdefaultcloseoperation(jframe.exit_on_close); 28: frame.setlayout(new BorderLayout()); 29: frame.add(paintpanel); 30: 31: // Create a panel on the left for buttons and add 32: // the button to it 33: JPanel buttonpanel = new JPanel(); 34: buttonpanel.setlayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); 35: buttonpanel.add(clearbutton); 36: frame.add(buttonpanel, BorderLayout.WEST); 37: 38: // Setup event listeners. In this case, Paint1 is the 39: // listener for all component events. 40: clearbutton.addactionlistener(this); 41: paintpanel.addmouselistener(this); 42: paintpanel.addmousemotionlistener(this); 43: 44: frame.pack(); 45: frame.setresizable(false); // Must not be resizable because we aren t 46: frame.setvisible(true); // handling changes in size. 47: } 48: 50: public void actionperformed(actionevent e) { 51: for (int i=0; i<grid.length; i++) 52: for (int j=0; j<grid[0].length; j++) 53: grid[i][j] = false; 54: 55: // The panel must be informed that a repaint is now required. 56: paintpanel.repaint(); 57: } 58: 59: public void mousepressed(mouseevent e) { 60: int cellx = e.getx() / paintpanel.cw; 61: int celly = e.gety() / paintpanel.ch; 62: 63: if (cellx >= 0 && cellx < grid.length && 64: celly >= 0 && celly < grid[0].length) { 65: // Set the cell content to true. 66: grid[cellx][celly] = true; 67: paintpanel.repaint(); 68: } 69: } 70: 71: public void mousedragged(mouseevent e) { 72: mousepressed(e); 73: } 74: 75: class PaintPanel extends JPanel { 76: // Width and height of an individual cell.
6 Paint1.java Page 2 of 2 77: int cw, ch; 78: 79: PaintPanel() { 80: int pw = 400, ph = 400; 81: setpreferredsize(new Dimension(pw, ph)); 82: 83: cw = pw / grid.length; 84: ch = ph / grid[0].length; 85: } 86: 87: public void paintcomponent(graphics g) { 88: // First fill the whole panel with white. 89: g.setcolor(color.white); 90: g.fillrect(0, 0, getwidth(), getheight()); 91: 92: // Now fill in all true grid entries with blue. 93: g.setcolor(color.blue); 94: for (int i=0; i<grid.length; i++) 95: for (int j=0; j<grid[0].length; j++) 96: if (grid[i][j]) 97: g.fillrect(i*cw, j*ch, cw, ch); 98: } 99: } 100: 101: public static void main(string[] args) throws Exception { 102: SwingUtilities.invokeLater(new Runnable() { 103: public void run() { 104: new Paint1(); 105: } 106: }); 107: } 108: }
7 Paint2.java Page 1 of 1 2: * Here we re-factor Paint1 to isolate the model as a separate 3: * class---gridmodel. Also we factor out PaintPanel and call it 4: * GridPanel and place it in a separate top-level class. 5: * 6: Andrew Vardy 7: */ 8: import java.awt.*; 9: import java.awt.event.actionevent; 10: import java.awt.event.actionlistener; 11: import java.awt.event.mouseadapter; 12: import java.awt.event.mouseevent; 13: 14: import javax.swing.*; 15: 16: public class Paint2 extends MouseAdapter implements ActionListener { 17: 18: GridModel model = new GridModel(100, 100); 19: JFrame frame = new JFrame("Paint2"); 20: GridPanel gridpanel = new GridPanel(400, 400, model); 21: JButton clearbutton = new JButton("Clear"); 22: 23: public Paint2() { 24: // Initialize frame and add the paintpanel in the center 25: frame.setdefaultcloseoperation(jframe.exit_on_close); 26: frame.setlayout(new BorderLayout()); 27: frame.add(gridpanel); 28: 29: // Create a panel on the left for buttons and add 30: // the button to it 31: JPanel buttonpanel = new JPanel(); 32: buttonpanel.setlayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); 33: buttonpanel.add(clearbutton); 34: frame.add(buttonpanel, BorderLayout.WEST); 35: 36: // Setup event listeners. In this case, Paint2 is the 37: // listener for all component events. 38: clearbutton.addactionlistener(this); 39: gridpanel.addmouselistener(this); 40: gridpanel.addmousemotionlistener(this); 41: 42: frame.pack(); 43: frame.setresizable(false); // Must not be resizable because we aren t 44: frame.setvisible(true); // handling changes in size. 45: } 46: 48: public void actionperformed(actionevent e) { 49: model.clearall(); 50: gridpanel.repaint(); 51: } 52: 53: public void mousepressed(mouseevent e) { 54: int cellx = e.getx() / gridpanel.getcellwidth(); 55: int celly = e.gety() / gridpanel.getcellheight(); 56: 57: if (cellx >= 0 && cellx < model.getwidth() && 58: celly >= 0 && celly < model.getheight()) { 59: model.setvalue(cellx, celly, true); 60: gridpanel.repaint(); 61: } 62: } 63: 64: public void mousedragged(mouseevent e) { 65: mousepressed(e); 66: } 67: 68: public static void main(string[] args) throws Exception { 69: SwingUtilities.invokeLater(new Runnable() { 70: public void run() { 71: new Paint2(); 72: } 73: }); 74: } 75: } 76:
8 GridModel.java Page 1 of 1 2: * Represents a 2-D array of boolean values. 3: * 4: Andrew Vardy 5: */ 6: public class GridModel { 7: protected int width; 8: protected int height; 9: protected boolean grid[][]; 10: 11: GridModel(int width, int height) { 12: this.width = width; 13: this.height = height; 14: grid = new boolean[width][height]; 15: } 16: 17: void clearall() { 18: for (int i=0; i<width; i++) 19: for (int j=0; j<height; j++) 20: grid[i][j] = false; 21: } 22: 23: boolean getvalue(int i, int j) { 24: return grid[i][j]; 25: } 26: 27: void setvalue(int i, int j, boolean value) { 28: grid[i][j] = value; 29: } 30: 31: int getwidth() { 32: return width; 33: } 34: 35: int getheight() { 36: return height; 37: } 38: }
9 GridPanel.java Page 1 of 1 2: * A JPanel that provides a view of a GridModel. 3: */ 4: import java.awt.color; 5: import java.awt.dimension; 6: import java.awt.graphics; 7: import javax.swing.jpanel; 8: 9: public class GridPanel extends JPanel { 10: // Width and height of an individual cell. 11: int cw, ch; 12: GridModel model; 13: 14: GridPanel(int width, int height, GridModel model) { 15: setpreferredsize(new Dimension(width, height)); 16: this.model = model; 17: 18: cw = width / model.getwidth(); 19: ch = height / model.getheight(); 20: } 21: 22: public void paintcomponent(graphics g) { 23: super.paintcomponent(g); 24: 25: // First fill the whole panel with white. 26: g.setcolor(color.white); 27: g.fillrect(0, 0, getwidth(), getheight()); 28: 29: // Now fill in all true grid entries with blue. 30: g.setcolor(color.blue); 31: for (int i=0; i<model.getwidth(); i++) 32: for (int j=0; j<model.getheight(); j++) 33: if (model.getvalue(i,j)) 34: g.fillrect(i*cw, j*ch, cw, ch); 35: } 36: 37: public int getcellwidth() { 38: return cw; 39: } 40: 41: public int getcellheight() { 42: return ch; 43: } 44: }
10 GameOfLife.java Page 1 of 3 2: * Based on Paint2, this application implements Conway s Game of Life 3: * Cellular Automaton. For event listeners we use anonymous inner 4: * class which are defined directly in the constructor for 5: * GameOfLife. 6: * 7: Andrew Vardy 8: */ 9: import java.awt.*; 10: import java.awt.event.actionevent; 11: import java.awt.event.actionlistener; 12: import java.awt.event.mouseadapter; 13: import java.awt.event.mouseevent; 14: 15: import javax.swing.*; 16: 17: public class GameOfLife { 18: 19: // The underlying model is an extension of GridModel 20: // and implements the rules for the Game of Life. 21: LifeModel model = new LifeModel(100, 100); 22: 23: JFrame frame = new JFrame("GameOfLife"); 24: GridPanel paintpanel = new GridPanel(800, 800, model); 25: JButton clearbutton = new JButton("Clear"), 26: startbutton = new JButton("Start"), 27: stopbutton = new JButton("Stop"); 28: 29: public GameOfLife() { 30: // Initialize frame and add the paintpanel in the center 31: frame.setdefaultcloseoperation(jframe.exit_on_close); 32: frame.setlayout(new BorderLayout()); 33: frame.add(paintpanel); 34: 35: // Create a panel on the left for buttons and add 36: // the button to it 37: JPanel buttonpanel = new JPanel(); 38: buttonpanel.setlayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); 39: buttonpanel.add(clearbutton); 40: buttonpanel.add(startbutton); 41: buttonpanel.add(stopbutton); 42: frame.add(buttonpanel, BorderLayout.WEST); 43: 44: // Setup event listeners. This time we will add the 45: // listeners as anonymous inner classes. 46: clearbutton.addactionlistener(new ActionListener() { 47: public void actionperformed(actionevent e) { 48: model.clearall(); 49: paintpanel.repaint(); 50: } 51: }); 52: startbutton.addactionlistener(new ActionListener() { 53: public void actionperformed(actionevent e) { 54: model.start(); 55: } 56: }); 57: stopbutton.addactionlistener(new ActionListener() { 58: public void actionperformed(actionevent e) { 59: model.stop(); 60: } 61: }); 62: 63: // For mouse events we also use an anonymous inner class 64: // only here we retain a reference to it so that it can 65: // be added as both a MouseListener and a MouseMotionListener 66: MouseAdapter mymouseadapter = new MouseAdapter() { 67: public void mousepressed(mouseevent e) { 68: int cellx = e.getx() / paintpanel.cw; 69: int celly = e.gety() / paintpanel.ch; 70: 71: if (cellx >= 0 && cellx < model.getwidth() && 72: celly >= 0 && celly < model.getheight()) { 73: model.setvalue(cellx, celly, true); 74: paintpanel.repaint(); 75: } 76: }
11 GameOfLife.java Page 2 of 3 77: 78: public void mousedragged(mouseevent e) { 79: mousepressed(e); 80: } 81: }; 82: paintpanel.addmouselistener(mymouseadapter); 83: paintpanel.addmousemotionlistener(mymouseadapter); 84: 85: // Create a timer object with a specific interval in 86: // milliseconds. Create a listener to update the model. 87: // Note that timer events require an ActionListener, just 88: // like JButton events. 89: Timer timer = new Timer(100, new ActionListener() { 90: public void actionperformed(actionevent e) { 91: model.step(); 92: paintpanel.repaint(); 93: } 94: }); 95: timer.start(); 96: 97: frame.pack(); 98: frame.setresizable(false); // Must not be resizable because we aren t 99: frame.setvisible(true); // handling changes in size. 100: } 101: 102: public static void main(string[] args) throws Exception { 103: SwingUtilities.invokeLater(new Runnable() { 104: public void run() { 105: new GameOfLife(); 106: } 107: }); 108: } 109: } 110: 111: class LifeModel extends GridModel { 112: // Used to store the new grid created on each call to step. 113: private boolean nextgrid[][]; 114: private boolean started = false; 115: 116: LifeModel(int width, int height) { 117: super(width, height); 118: nextgrid = new boolean[width][height]; 119: } 120: 121: // Allow the simulation to proceed. 122: void start() { 123: started = true; 124: } 125: 126: // Prevent the simulation from proceeding. 127: void stop() { 128: started = false; 129: } 130: 131: // Execute one iteration, updating grid according 132: // to the Game of Life rules. 133: void step() { 134: if (!started) 135: return; 136: 137: // Implement rules for Conway s Game of Life, for all grid 138: // cells not on the outside border. 139: for (int i=1; i<width-1; i++) 140: for (int j=1; j<height-1; j++) { 141: // Count the number of live neighbours. 142: int count = 0; 143: if (grid[i-1][j-1]) count++; 144: if (grid[i ][j-1]) count++; 145: if (grid[i+1][j-1]) count++; 146: if (grid[i-1][j ]) count++; 147: if (grid[i+1][j ]) count++; 148: if (grid[i-1][j+1]) count++; 149: if (grid[i ][j+1]) count++; 150: if (grid[i+1][j+1]) count++; 151: 152: if (grid[i][j]) 153: // A living cell will stays alive only if it
12 GameOfLife.java Page 3 of 3 154: // has exactly 2 or 3 living neighbours. 155: nextgrid[i][j] = count==2 count==3; 156: else 157: // A dead cell will only come back to life 158: // if it has exactly 3 living neighbours. 159: nextgrid[i][j] = count==3; 160: } 161: 162: // Swap the two grid references 163: boolean tmp[][] = grid; 164: grid = nextgrid; 165: nextgrid = tmp; 166: } 167: }
Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008
Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Announcements A3 is up, due Friday, Oct 10 Prelim 1 scheduled for Oct 16 if you have a conflict, let us know now 2 Interactive
More informationGraphic User Interfaces. - GUI concepts - Swing - AWT
Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations
More informationH212 Introduction to Software Systems Honors
Introduction to Software Systems Honors Lecture #19: November 4, 2015 1/14 Third Exam The third, Checkpoint Exam, will be on: Wednesday, November 11, 2:30 to 3:45 pm You will have 3 questions, out of 9,
More informationAnimatedImage.java. Page 1
1 import javax.swing.japplet; 2 import javax.swing.jbutton; 3 import javax.swing.jpanel; 4 import javax.swing.jcombobox; 5 import javax.swing.jlabel; 6 import javax.swing.imageicon; 7 import javax.swing.swingutilities;
More informationPreviously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components
CS112-Section2 Hakan Guldas Burcin Ozcan Meltem Kaya Muge Celiktas Notes of 6-8 May Graphics Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how
More informationAP CS Unit 11: Graphics and Events
AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two
More informationGraphical User Interface (GUI)
Graphical User Interface (GUI) Layout Managment 1 Hello World Often have a static method: createandshowgui() Invoked by main calling invokelater private static void createandshowgui() { } JFrame frame
More informationAPPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }
APPENDIX CLASS NODE AS TREE OBJECT public class Node public int data; public Node left; public Node right; public Node parent; public Node(int i) data=i; PROCEDURE BUILDING TREE public class Tree public
More information// autor igre Ivan Programerska sekcija package mine;
// autor igre Ivan Bauk @ Programerska sekcija package mine; import java.awt.color; import java.awt.flowlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener;
More informationencompass a group of features for building Graphical User Interfaces (GUI).
Java GUI (intro) JFC Java Foundation Classes encompass a group of features for building Graphical User Interfaces (GUI). javax.swing.* used for building GUIs. Some basic functionality is already there
More information1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2
1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring 2010 - Quiz 2 Name: MIT Email: TA: Section: You have 80 minutes to complete this exam. For coding questions, you do not need
More informationUser interfaces and Swing
User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1 2 Applets public class Simple extends
More informationG51PRG: Introduction to Programming Second semester Applets and graphics
G51PRG: Introduction to Programming Second semester Applets and graphics Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Previous two lectures AWT and Swing Creating components and putting
More informationHeavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.
Java GUI Windows Events Drawing 1 Java GUI Toolkits Toolkit AWT Description Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.
More informationINTRODUCTION TO (GUIS)
INTRODUCTION TO GRAPHICAL USER INTERFACES (GUIS) Lecture 10 CS2110 Fall 2009 Announcements 2 A3 will be posted shortly, please start early Prelim 1: Thursday October 14, Uris Hall G01 We do NOT have any
More informationLecture 5: Java Graphics
Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,
More informationOOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:
OOP Assignment V If we don t use multithreading, or a timer, and update the contents of the applet continuously by calling the repaint() method, the processor has to update frames at a blinding rate. Too
More informationCSCI 201L Midterm Written Fall % of course grade
CSCI 201L Midterm Written Fall 2015 10% of course grade 1. Inheritance Answer the following questions about inheritance. a. Does Java allow overloading, overriding, and redefining of methods? (0.5%) b.
More informationGraphical User Interfaces 2
Graphical User Interfaces 2 CSCI 136: Fundamentals CSCI 136: Fundamentals of Computer of Science Computer II Science Keith II Vertanen Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Overview
More informationCSCI 201L Midterm Written SOLUTION Fall % of course grade
CSCI 201L Midterm Written SOLUTION Fall 2015 10% of course grade 1. Inheritance Answer the following questions about inheritance. a. Does Java allow overloading, overriding, and redefining of methods?
More informationIntroduction to the JAVA UI classes Advanced HCI IAT351
Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing
More informationOutline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics
Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming
More informationPart I: Learn Common Graphics Components
OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect
More informationFrames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling
Frames, GUI and events Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Introduction to Swing The Java AWT (Abstract Window Toolkit)
More informationBuilding a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver
Building a GUI in Java with Swing CITS1001 extension notes Rachel Cardell-Oliver Lecture Outline 1. Swing components 2. Building a GUI 3. Animating the GUI 2 Swing A collection of classes of GUI components
More informationLecture 3: Java Graphics & Events
Lecture 3: Java Graphics & Events CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki Text Input Scanner class Constructor: myscanner = new Scanner(System.in); can use file instead of System.in new Scanner(new
More informationG51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread
G51PGP Programming Paradigms Lecture 008 Inner classes, anonymous classes, Swing worker thread 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals
More informationWindow Interfaces Using Swing Objects
Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs
More informationUsing Several Components
Ch. 16 pt 2 GUIs Using Several Components How do we arrange the GUI components? Using layout managers. How do we respond to event from several sources? Create separate listeners, or determine the source
More informationGoals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords
Goals By the end of this lesson, you should: Lecture 7 More GUI programming 1. Be able to write Java s with JTextField, JList, JCheckBox and JRadioButton components 2. Be able to implement a ButtonGroup
More informationDr. Hikmat A. M. AbdelJaber
Dr. Hikmat A. M. AbdelJaber JWindow: is a window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. It optionally has a parent JFrame.
More informationWindow Interfaces Using Swing Objects
Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs
More informationChapter 9 Designing Graphical User Interfaces (GUIs)
Chapter 9 Designing Graphical User Interfaces (GUIs) Overview The basics of GUIs in Java A tour of Java GUI libraries Containers and components Swing: the full picture Layout managers Understanding events
More informationGraphical User Interface (GUI)
Graphical User Interface (GUI) Layout Managment 1 Hello World Often have a run method to create and show a GUI Invoked by main calling invokelater private void run() { } JFrame frame = new JFrame("HelloWorldSwing");
More informationAdvanced Java Unit 6: Review of Graphics and Events
Advanced Java Unit 6: Review of Graphics and Events This is a review of the basics of writing a java program that has a graphical interface. To keep things simple, all of the graphics programs will follow
More informationCSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT
CSE 143 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/
More informationFinal Examination Semester 2 / Year 2010
Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2010 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student
More informationJFrame In Swing, a JFrame is similar to a window in your operating system
JFrame In Swing, a JFrame is similar to a window in your operating system All components will appear inside the JFrame window Buttons, text labels, text fields, etc. 5 JFrame Your GUI program must inherit
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 32 April 9, 2018 Swing I: Drawing and Event Handling Chapter 29 HW8: Spellchecker Available on the web site Due: Tuesday! Announcements Parsing, working
More informationJava: Graphical User Interfaces (GUI)
Chair of Software Engineering Carlo A. Furia, Marco Piccioni, and Bertrand Meyer Java: Graphical User Interfaces (GUI) With material from Christoph Angerer The essence of the Java Graphics API Application
More informationCS 251 Intermediate Programming GUIs: Components and Layout
CS 251 Intermediate Programming GUIs: Components and Layout Brooke Chenoweth University of New Mexico Fall 2017 import javax. swing.*; Hello GUI public class HelloGUI extends JFrame { public HelloGUI ()
More informationProctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.
QUEEN'S UNIVERSITY SCHOOL OF COMPUTING HAND IN Answers Are Recorded on Question Paper CMPE212, FALL TERM, 2012 FINAL EXAMINATION 18 December 2012, 2pm Instructor: Alan McLeod If the instructor is unavailable
More informationGraphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)
Graphics programming COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Overview Aims To provide an overview of Swing and the AWT To show how to build
More informationThe JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class
CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical
More informationTopic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more
Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 5: Will be an open-ended Swing project. "Programming Contest"
More informationTopic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!
Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Why are we studying Swing? 1. Useful & fun! 2. Good application of OOP techniques
More informationAP CS Unit 12: Drawing and Mouse Events
AP CS Unit 12: Drawing and Mouse Events A JPanel object can be used as a container for other objects. It can also be used as an object that we can draw on. The first example demonstrates how to do that.
More informationCSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction
CSC 160 LAB 8-1 DIGITAL PICTURE FRAME PROFESSOR GODFREY MUGANDA DEPARTMENT OF COMPUTER SCIENCE 1. Introduction Download and unzip the images folder from the course website. The folder contains 28 images
More informationJava Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,
Java Mouse Actions C&G criteria: 5.2.1, 5.4.1, 5.4.2, 5.6.2. The events so far have depended on creating Objects and detecting when they receive the event. The position of the mouse on the screen can also
More informationBuilding Java Programs Bonus Slides
Building Java Programs Bonus Slides Graphical User Interfaces Copyright (c) Pearson 2013. All rights reserved. Graphical input and output with JOptionPane JOptionPane An option pane is a simple dialog
More informationJava. GUI building with the AWT
Java GUI building with the AWT AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses the controls defined by your OS therefore
More informationBuilding Graphical User Interfaces. GUI Principles
Building Graphical User Interfaces 4.1 GUI Principles Components: GUI building blocks Buttons, menus, sliders, etc. Layout: arranging components to form a usable GUI Using layout managers. Events: reacting
More informationCSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University
Events and Listeners CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides
More informationComputer Science 210: Data Structures. Intro to Java Graphics
Computer Science 210: Data Structures Intro to Java Graphics Summary Today GUIs in Java using Swing in-class: a Scribbler program READING: browse Java online Docs, Swing tutorials GUIs in Java Java comes
More informationDatenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1
Datenbank-Praktikum Universität zu Lübeck Sommersemester 2006 Lecture: Swing Ho Ngoc Duc 1 Learning objectives GUI applications Font, Color, Image Running Applets as applications Swing Components q q Text
More informationProctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.
HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC124, WINTER TERM, 2009 FINAL EXAMINATION 7pm to 10pm, 18 APRIL 2009, Dunning Hall Instructor: Alan McLeod If the
More informationTop-Level Containers
1. Swing Containers Swing containers can be classified into three main categories: Top-level containers: JFrame, JWindow, and JDialog General-purpose containers: JPanel, JScrollPane,JToolBar,JSplitPane,
More informationExample: Building a Java GUI
Steven Zeil October 25, 2013 Contents 1 Develop the Model 2 2 Develop the layout of those elements 3 3 Add listeners to the elements 9 4 Implement custom drawing 12 1 The StringArt Program To illustrate
More informationExample: Building a Java GUI
Steven Zeil October 25, 2013 Contents 1 Develop the Model 3 2 Develop the layout of those elements 4 3 Add listeners to the elements 12 4 Implement custom drawing 15 1 The StringArt Program To illustrate
More informationGUI in Java TalentHome Solutions
GUI in Java TalentHome Solutions AWT Stands for Abstract Window Toolkit API to develop GUI in java Has some predefined components Platform Dependent Heavy weight To use AWT, import java.awt.* Calculator
More informationJava Coordinate System
Java Graphics Drawing shapes in Java such as lines, rectangles, 3-D rectangles, a bar chart, or a clock utilize the Graphics class Drawing Strings Drawing Lines Drawing Rectangles Drawing Ovals Drawing
More informationFinal Examination Semester 2 / Year 2011
Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2011 COURSE COURSE CODE TIME DEPARTMENT LECTURER : JAVA PROGRAMMING : PROG1114 : 2 1/2 HOURS : COMPUTER SCIENCE : LIM PEI GEOK Student
More informationLab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in
Lab 4 D0010E Object-Oriented Programming and Design Lecture 9 Lab 4: You will implement a game that can be played over the Internet. The networking part has already been written. Among other things, the
More informationQueen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb
Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb HAND IN Answers recorded on Examination paper This examination is THREE HOURS
More informationCIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011
CIS 120 Programming Languages and Techniques Final Exam, May 3, 2011 Name: Pennkey: My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in
More informationKF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation
KF5008 Program Design & Development Lecture 1 Usability GUI Design and Implementation Types of Requirements Functional Requirements What the system does or is expected to do Non-functional Requirements
More informationDr. Hikmat A. M. AbdelJaber
Dr. Hikmat A. M. AbdelJaber Portion of the Java class hierarchy that include basic graphics classes and Java 2D API classes and interfaces. java.lang.object Java.awt.Color Java.awt.Component Java.awt.Container
More informationBuilding Graphical User Interfaces. Overview
Building Graphical User Interfaces 4.1 Overview Constructing GUIs Interface components GUI layout Event handling 2 1 GUI Principles Components: GUI building blocks. Buttons, menus, sliders, etc. Layout:
More informationGUI DYNAMICS Lecture July 26 CS2110 Summer 2011
GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI Statics and GUI Dynamics 2 Statics: what s drawn on the screen Components buttons, labels, lists, sliders, menus,... Containers: components that contain
More informationPackages: Putting Classes Together
Packages: Putting Classes Together 1 Introduction 2 The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic
More informationJava Never Ends CHAPTER MULTITHREADING 1100 Example: A Nonresponsive GUI 1101
CHAPTER 20 Java Never Ends 20.1 MULTITHREADING 1100 Example: A Nonresponsive GUI 1101 Thread.sleep 1101 The getgraphics Method 1105 Fixing a Nonresponsive Program Using Threads 1106 Example: A Multithreaded
More informationSE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts
SE1021 Exam 2 Name: You may use a note-sheet for this exam. But all answers should be your own, not from slides or text. Review all questions before you get started. The exam is printed single-sided. Write
More informationGraphical User Interfaces 2
Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Ge?ng user input Overview Displaying message or error Listening for
More informationGraphical User Interfaces (GUIs)
CMSC 132: Object-Oriented Programming II Graphical User Interfaces (GUIs) Department of Computer Science University of Maryland, College Park Model-View-Controller (MVC) Model for GUI programming (Xerox
More informationJava Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959
CHAPTER 19 Java Never Ends 19.1 MULTITHREADING 958 Example: A Nonresponsive GUI 959 Thread.sleep 959 The getgraphics Method 963 Fixing a Nonresponsive Program Using Threads 964 Example: A Multithreaded
More information(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI
51 6!! GUI(Graphical User Interface) java.awt javax.swing (component) GUI... (container) (listener)... MouseListener, ActionLister (adapter)... MouseAdapter, ActionAdapter 6.1 GUI(Graphics User Interface
More informationGraphical User Interfaces
Graphical User Interfaces CSCI 136: Fundamentals CSCI 136: Fundamentals of Computer of Science Computer II Science Keith II Vertanen Keith Vertanen Copyright 2011 Overview Command line versus GUI apps
More informationGraphical User Interfaces 2
Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014 2011 Extending JFrame Dialog boxes Overview Ge
More informationAdvanced Java Programming
Advanced Java Programming Shmulik London Lecture #5 GUI Programming Part I AWT & Basics Advanced Java Programming / Shmulik London 2006 Interdisciplinary Center Herzeliza Israel 1 Agenda AWT & Swing AWT
More informationCSIS 10A Assignment 7 SOLUTIONS
CSIS 10A Assignment 7 SOLUTIONS Read: Chapter 7 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment,
More information2IS45 Programming
Course Website Assignment Goals 2IS45 Programming http://www.win.tue.nl/~wsinswan/programmeren_2is45/ Rectangles Learn to use existing Abstract Data Types based on their contract (class Rectangle in Rectangle.
More informationCSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT
CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/
More informationTheory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use
Please fill in your Student Number and, optionally, Name. For Official Use Student Number : Mark : Name : Marker : University of Cape Town ~ Department of Computer Science Computer Science 1016S ~ 2007
More informationGUI and its COmponent Textfield, Button & Label. By Iqtidar Ali
GUI and its COmponent Textfield, Button & Label By Iqtidar Ali GUI (Graphical User Interface) GUI is a visual interface to a program. GUI are built from GUI components. A GUI component is an object with
More informationGUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners
GUI (Graphic User Interface) Programming Part 2 (Chapter 8) Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons, text fields, and the
More informationAll the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.
Q1. If you add a component to the CENTER of a border layout, which directions will the component stretch? A1. The component will stretch both horizontally and vertically. It will occupy the whole space
More informationProgramming Mobile Devices J2SE GUI
Programming Mobile Devices J2SE GUI University of Innsbruck WS 2009/2010 thomas.strang@sti2.at Graphical User Interface (GUI) Why is there more than one Java GUI toolkit? AWT write once, test everywhere
More informationIT101. Graphical User Interface
IT101 Graphical User Interface Foundation Swing is a platform-independent set of Java classes used for user Graphical User Interface (GUI) programming. Abstract Window Toolkit (AWT) is an older Java GUI
More informationOverview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling
Overview Building Graphical User Interfaces Constructing GUIs Interface components GUI layout Event handling 4.1 GUI Principles AWT and Swing Components: GUI building blocks. Buttons, menus, sliders, etc.
More informationDM503 Programming B. Peter Schneider-Kamp.
DM503 Programming B Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm503/! ADVANCED OBJECT-ORIENTATION 2 Object-Oriented Design classes often do not exist in isolation from each
More informationMultiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)
M257 MTA Spring2010 Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) 1. If we need various objects that are similar in structure, but
More informationRobotPlanning.java Page 1
RobotPlanning.java Page 1 import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.swing.border.*; import java.util.*; * * RobotPlanning - 1030 GUI Demonstration.
More informationProctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.
HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2006 FINAL EXAMINATION 7pm to 10pm, 19 DECEMBER 2006, Jeffrey Hall 1 st Floor Instructor: Alan
More informationState Application Using MVC
State Application Using MVC 1. Getting ed: Stories and GUI Sketch This example illustrates how applications can be thought of as passing through different states. The code given shows a very useful way
More informationJLayeredPane. Depth Constants in JLayeredPane
JLayeredPane Continuing on Swing Components A layered pane is a Swing container that provides a third dimension for positioning components depth or Z order. The class for the layered pane is JLayeredPane.
More informationJava, Swing, and Eclipse: The Calculator Lab.
Java, Swing, and Eclipse: The Calculator Lab. ENGI 5895. Winter 2014 January 13, 2014 1 A very simple application (SomeimageswerepreparedwithanearlierversionofEclipseandmaynotlookexactlyasthey would with
More informationCSA 1019 Imperative and OO Programming
CSA 1019 Imperative and OO Programming GUI and Event Handling Mr. Charlie Abela Dept. of of Artificial Intelligence Objectives Getting familiar with UI UI features MVC view swing package layout managers
More informationGraphical User Interfaces in Java - SWING
Graphical User Interfaces in Java - SWING Graphical User Interfaces (GUI) Each graphical component that the user can see on the screen corresponds to an object of a class Component: Window Button Menu...
More information[module lab 2.2] GUI FRAMEWORKS & CONCURRENCY
v1.0 BETA Sistemi Concorrenti e di Rete LS II Facoltà di Ingegneria - Cesena a.a 2008/2009 [module lab 2.2] GUI FRAMEWORKS & CONCURRENCY 1 GUI FRAMEWORKS & CONCURRENCY Once upon a time GUI applications
More informationJFrame & JLabel. By Iqtidar Ali
JFrame & JLabel By Iqtidar Ali JFrame & its Features JFrame is a window with border, title and buttons. The component added to frame are referred to as its contents & are managed by the content pane. To
More information