Download Week10

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming w/ Java – Week 10
Week 10 Agenda:
Introduction to GUI programming – Graphical User interfaces – i.e. making graphics!
Before we start, once again we will be importing pre-written classes from libraries to use. Two libraries we
will should understand are::
java.awt = Abstract Window Toolkit(AWT) – Java’s original windowing, graphics, and user-interface
toolkit. The tools in this package are used to create a GUI (Graphical User Interface) for a program.
Originally these classes were called the JFC – Java Foundation Classes – the set of commands needed to
do GUI programming in Java.
javax.swing = Java’s more sophisticated toolkit for creating GUIs (Graphical User Interfaces). Swing is
platform independent, while the original AWT had different code for different platforms (i.e. computers –
Mac vs. PC). Swing was originally an “extension” to the main java classes, hence we import it with
“javax” instead of just “java”.
STEP 1 - Messages
Let’s begin. First open Netbeans, then click on File and choose New Project and Java Application.
For our project name we will type “Week8”, and we will allow Java to create a Main method for us.
Feel free to delete all the standard comments that come up, and then type the program that follows.
----------------package week10;
import javax.swing.*;
public class Week10 {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Are you ready to play?", "Ben's JAVA Window",
JOptionPane.PLAIN_MESSAGE);
}
}
-----------------------The JOptionPane class has several methods that are pre-written that we can use to customize
messages. We can also use this class to get user input from a graphical window. Try this
example – add the text in bold:
----------------package week10;
import javax.swing.*;
public class Week10 {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Are you ready to play?", "Ben's JAVA Window",
JOptionPane.PLAIN_MESSAGE);
String inputText;
inputText = JOptionPane.showInputDialog("Enter your full name please.", "Bubba Yoloman");
JOptionPane.showMessageDialog(null, "I think your name is "+ inputText +", please click the"
+ " OK button if this is correct.\n If not I might cry..", "Ben's JAVA Window",
JOptionPane.QUESTION_MESSAGE);
}
}
-----------------------Notice that we have used two specific methods in the JOptionPane.showMessageDialog class:
PLAIN_MESSAGE, and QUESTION_MESSAGE.
Now try using some others – there are also:
- ERROR_MESSAGE
- WARNING_MESSAGE
- INFORMATION_MESSAGE
STEP 2 – Windows
Now let’s try more advanced windows programming. Now we will create our own Window from scratch
using more classes from the javax.swing package. You could keep writing in your current Week10 Class,
or you could create a new program. I will create a new class by going to the Projects window in Netbeans,
choosing the Week10 project, drilling down to the Source Packages folder, and then right clicking on the
week10 package and selecting “New… Java Main Class”. If you create a new class, feel free to delete all
the standard comments that come up, and then type the program that follows. Remember – your project
can only have one main method, so you’ll need to comment out the main method in your Week10 class.
In this program we will see two new things:
1. The concept of inheritance. Using the keyword extends will allow our new class Window to inherit all
the methods of the JFrame class – which is defined in the javax.swing package. Another way to say this is
that our Window class will be a “subclass” of JFrame, which is then called the “superclass”. Other
programmers will refer to the Window class as the “child” and JFrame as the “parent” class.
2. Several commands which allow us to manage the properties of our Window – all which are actually
methods defined in the JFrame class. These are things like:
- setsize()
- setVisible()
- setDefaultCloseOperation()
----------------package week10;
public class Week10window {
public static void main(String[] args) {
Window BenWindow;
BenWindow = new Window("Ben's Window", 800, 400);
}
}
-----------------------This first class contains the main method, which essentially just creates a “Window” object named
“BenWindow”. Notice we pass in some parameters for the Window title, width and the height.
----------------package week10;
import java.awt.*;
import javax.swing.*;
public class Window extends JFrame {
public Window (String Title, int width, int height) {
super (Title);
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container windowContainer = getContentPane();
windowContainer.setBackground(Color.green);
}
}
------------------------