An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Similar documents
Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 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!

AP CS Unit 11: Graphics and Events

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Agenda. Container and Component

Graphical User Interfaces. Swing. Jose Jesus García Rueda

DM503 Programming B. Peter Schneider-Kamp.

Systems Programming Graphical User Interfaces

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Lecture (06) Java Forms

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Introduction. Introduction

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

SINGLE EVENT HANDLING

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Programming Languages and Techniques (CIS120e)

MIT AITI Swing Event Model Lecture 17

Graphical User Interfaces. Comp 152

Part I: Learn Common Graphics Components

Graphic User Interfaces. - GUI concepts - Swing - AWT

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Java. GUI building with the AWT

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

Window Interfaces Using Swing Objects

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Java - Applets. public class Buttons extends Applet implements ActionListener

Window Interfaces Using Swing Objects

Dr. Hikmat A. M. AbdelJaber

Swing from A to Z Some Simple Components. Preface

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

CS Exam 1 Review Suggestions

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Java Programming Lecture 6

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Swing - JButton. Adding buttons to the main window

Swing/GUI Cheat Sheet

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CS 251 Intermediate Programming GUIs: Components and Layout

CMP 326 Midterm Fall 2015

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

CS Fall Homework 5 p. 1. CS Homework 5

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

To gain experience using GUI components and listeners.

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

GUI Forms and Events, Part II

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Class 16: The Swing Event Model

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

2.4 Input and Output. Input and Output. Standard Output. Standard Output Abstraction. Input devices. Output devices. Our approach.

CS 180 Fall 2006 Exam II

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

COMPSCI 230. Software Design and Construction. Swing

navlakhi.com / navlakhi.education / navlakhi.mobi / navlakhi.org 1

CSIS 10A Assignment 7 SOLUTIONS

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

University of Cape Town Department of Computer Science Computer Science CSC1017F

Graphical User Interface (GUI)

First Name: AITI 2004: Exam 2 July 19, 2004

CS 106A, Lecture 23 Interactors and GCanvas

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Window Interfaces Using Swing. Chapter 12

Programmierpraktikum

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Introduction to the JAVA UI classes Advanced HCI IAT351

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

Packages: Putting Classes Together

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Project 1. LibraryTest.java. Yuji Shimojo CMSC 335

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Part 3: Graphical User Interface (GUI) & Java Applets

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

First Name: AITI 2004: Exam 2 July 19, 2004

Advanced Java Unit 6: Review of Graphics and Events

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

Example 3-1. Password Validation

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Week 9. Abstract Classes

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Swing - JLabel. Adding a text (and HTML) labels to a GUI

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Chapter 6 Using Objects

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

User interfaces and Swing

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

CS12020 for CGVG. Practical 2. Jim Finnis

Information Technology for Industrial Engineers 15 November ISE 582: Information Technology for Industrial Engineers

Transcription:

Arrays An array is a type of variable that is able to hold more than one piece of information under a single variable name. Basically you are sub-dividing a memory box into many numbered slots that can be accessed and changes independently from one another. Example. Tic-Tac-Toe Without arrays the board needs to have 9 separate memory boxes to represent the game board. Int s1=0,s2=0,s3=0,s4=0 s9=0; With and array we can define a single variable and give it the number of slots that we require (in this case 9) Int [ ] board; board=new int [9]; Placing values into the slots I could do this board[0]=0; board[1]=0; board[8]=0; for comparison and much easier for (int x=0;x<9;x++) board[x]=0; *** The second example uses a counted loop to move through the slots and place a zero in each slot

Intro to Graphical User Interfaces GUIs allow our programs to be a lot more attractive and current looking. Ultimately it will allow us to combine text and graphics features to give our programs greater appeal and functionality. To implement a GUI Step 1: import javax.swing.*; import java.awt.*; import java.awt.event.*; These are outside libraries of code that allow us to build and use our GUI. Step 2: In the class creation line add the following extends JFrame Step 3: Panels are used to group objects or items used to build the GUI with (like labels and textfields) To create a panel JPanel panel1=new JPanel(); Step 4: Basic objects can now be created and added to the JPanel JLabel label1=new JLabel( Hello World ); JTextField text1=new JTextField ( Type Here,20);

Labels are used to display information TextFields are used to allow a person to type information into a program Step 5: Add the newly created objects onto the JPanel panel1.add(label1); panel1.add(text1); Step 6; Add the JPanel onto the JFrame this.add(panel1); Step 7; Set the size of the JFrame this.setsize(300,300); Step 8; Make the JFrame visible this.setvisible(true);

Setting Layouts in a JPanel or JFrame By default, the Layout used by Java is called FlowLayout. FL places objects on the JFrame or JPanel left to right (as much as it can) and then top to bottom (starting an new line as there is no more space on the previous line). This all depends on the size and/or shape of the JFrame or JPanel. A very common Layout used also is GridLayout GL as you might expect will allow you to divide a panel or frame into a set number of rows/columns and it will place objects into the cells created in the grid. How do you do this Panelname.setLayout(new GridLayout(4,2)); *** where Panelname is the name of your panel. If you want a GL on the Jframe itself use the word this

Creating a Button in Java Step 1: Add the words implements ActionListener at the end of the class creation line (after extends JFrame) Step 2: Add the actionperformed method to the bottom of your program. Again keep it inside the last curly bracket that finishes the Class. public void actionperformed(actionevent e) if (e.getsource ()==b1) *** this is where the codes for the button called b1 goes Note that b1 is the name of a button object. Your actual button may be named something else. Step 3: Create a button variable JButton b1,b2,b3; This line needs to be located inside the start of the class but before the main subroutine. Step 4: Inside the GUI creation subroutine you need to assign a JButton to your button variable declared in Step 3. b1=new JButton ( OK ); Step 5: Add the ActionListener to the button b1.addactionlistener(this);

Step 6: Add the button to a panel and then add to the frame (Just like with a label) panel1.add(b1); this.add(panel1);

Changing the Text on a JButton or JLabel or JTextField objectname.settext( text you want to display ); everything you display on screen needs to be String based (nonnumeric) so if it is numeric it needs to be converted to a String first. Getting information FROM an object in a GUI Information coming into a program should really only come from a JTextField. variablename=textfieldname.gettext(); ex. String MyName; MyName=txtName.getText(); Changing the content of a GUI object Objectname.setText( Hello World ); Ex. Label1.setText( Hello World ); Text1.setText( Please enter your name ); Label2.setText(name); *** where name is a memory box (variable)

Converting Strings (or text) into Values In order to convert text info into a value or number you need the following built-in functions double value = Double.parseDouble(string); int value = Integer.parseInt(string); where value is the name of the variable you are assigning the converted string to AND String is the name of a string variable containing a number OR a gettext statement. example int v1 = Integer.parseInt(text1.getText()); this example will take text info from a text object called text1 and store it in an integer variable called v1

GUIs Continued Changing Color and Fonts in GUI Objects Step 1 Define Font(s) and Color(s) to be used just inside the CLASS title line where we are currently defining our other variable names example Font f = new Font("Courier", Font.BOLD, 30); Color c1=new Color(100,50,30); These 2 lines define a Font called f and a Color called c1 just like variables they could really be called anything. Step 2 When it comes to actually applying a font or color to an object we do the following Identify the object we want to alter put a dot identify what you want to change (Foreground or Background) b1.setforeground(c1); b1.setbackground(color.green); or set the font b1.setfont(f); In order to change the background color of a JLabel you need to use the following command as well for each label affected. label1.setopaque(true); *** by default labels are transparent and in order to see the colour they need to be made opaque (or solid).

Creating Arrays of Objects An object array in Java allows you to create MANY occurrences of the same kind of object (ex. A JLabel), have all of the occurrences of the JLabel share the SAME name, but still differentiate those individual labels using and index value. Example Label[0]= A ; Label[1]= B ; Label[25]= Z ; Steps to creating an object array Step 1: Indicate that you intend to create an array of objects when you initially declare the variable name for those objects by using a set of square brackets JLabel [ ] letters; Steps 2: When you know how many objects you need in the array declare the size of the object array in the main class letters=new JLabel[15]; *** the 15 could be any value OR it could be a value generated by another function Step 3: Each individual object then needs to be created, formatted, added to a panel (possible attached to an ActionListener) in order to be functional. This usually involves a counted loop For (int a=0;a<15;a++)

letters[a]=new JLabel ( $ ); panel1.add(letters[a]); *** where dollar sign ($) could be anything that you wanted to appear on the object. If they were Jbuttons then adding the ActionListener would take place inside the loop Step 4 (for any object with an ActionListener) In step 3 when using a JButton (instead of JLabel) we also need to add each button in the array to the ActionListener Likewise, in doing this we need to determine which specific button in the "array of buttons" is pressed when the program jumps to the ActionPerformed subroutine. To do this we need a loop in the ActionPerformed sub for(int d=0;d<9;d++) if (e.getsource==buttonset1[d]) if (player==1) buttonset1[d].settext("x"); else buttonset1[d].settext("o");

JButton [ ] buttonset1; buttonset1=new JButton[9]; For (int a=0;a<9;a++) buttonset1[a]=new JButton ( TTT ); panel1.add(buttonset1[a]); buttonset1[a].addactionlistener(this);

Object Arrays An object array is an object (JButton, JLabel, JTextField, etc...) that is created under a singular name, but is duplicated by creating a numbered set of that object. All of the objects within the set of numbered objects operates independently of one another and can be given different functionality. The advantage of an object array (and even a data array) is that we can mass process/create array objects/data slots using a counted loop to assist with the process. To create an object array... Step 1 In the initial declaration indicate if the object being created is going to be an array of objects (ie more than one). JButton [ ] ButtonSet; Step 2 Indicate how many of the object will be part of the array ButtonSet=new JButton[100]; Steps 1 and 2 can be done together at the top of the program as follows... JButton [ ] ButtonSet=new JButton[100]; Step 3 Create each individual object in the array of objects.

for (int u=0;u<100;u++) ButtonSet[u]=new JButton( +(u+100)); ButtonSet[u].setBackground(new Color(0+(u*2),50+u,150-u)); ButtonSet[u].addActionListener(this); Step 4 From this point forward you will likely want to process the objects as part of a counted loop for (int u=0;u<100;u++) If (e.getsource()=buttonset[u]) ButtonSet[u].setBackground(Color.BLACK); u=100;