Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
-
Upload
edith-norris -
Category
Documents
-
view
248 -
download
1
Transcript of Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
// helloworldbutton.java
import java.awt.*;import javax.swing.*;
class HelloButton{ public static void main (String[] args){
JFrame frame = new JFrame("HelloButton");Container pane = frame.getContentPane();JButton hello = new JButton("HelloWorld");pane.add(hello);frame.pack();frame.show();
}}
The Java Button
frame.setSize(300,200);
The Swing library
• Any program that uses Swing to create a window enters a separate thread of execution that enters an infinite loop, looking for events such as mouse movements, button clicks or key presses.
• This is like having to workers executing the program:– One for the main()
– One for watching for events
• This results in an event driven program
• When main() is finished, the program still is executing the “listening” part of the library object.
The Swing library
• All Swing components are event sources that can be observed or listened to.
• To use the event, we need to tell the source which object to notify when an event occurs.
• By default the JButton reacts to a mouse click by changing its appearance.
• JButtons are also the source of java.awt.event.ActionEvent objects. An ActionEvent is generated by a button when you click the button with a mouse.
• Objects that can receive events are called listeners.
• Different type of events have different listeners.
// HelloGoodBye.java
import java.awt.*;import javax.swing.*;
class HelloGoodBye{ public static void main (String[] args){
JFrame frame = new JFrame("HelloGoodBye");Container pane = frame.getContentPane();JButton hello = new JButton("Hello world!!");
GoodBye listener = new GoodBye(); hello.addActionListener(listener);
pane.add(hello);frame.setSize(200,75);frame.show();
}}
Now we have a button, but no action for the object named listener from the GoodBye class
// GoodBye.javaimport java.awt.event.*;
class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println(“Goodbye !”);
System.exit(0); }}
The steps• Create a button with new Jbutton (“some label”)
• Get the Container for the Jframe using getContentPane()
• Add the button to the content pane of the Jframe with add()
• Create an ActionEventListener class by
– Adding implements ActionEventListener to the lcass declaration and
– Defining an actionPerformed method
• Add the listener object to the list of listeners for the button by calling button.addActionListener(listener), where the button is the name we gave our button, and listener is an instance of the class we created to be executed when the button was clicked (the GoodBye class).
Many Buttons// ManyButtons.java
import java.awt.*;import javax.swing.*;
class ManyButtons{ public static void main (String[] args){
JFrame frame = new JFrame("myCalculator");Container pane = frame.getContentPane();
JButton exit = new JButton("Exit"); GoodBye listener = new GoodBye(); exit.addActionListener(listener);
JButton myAddition = new JButton("2+2"); Addition listener1 = new Addition(); myAddition.addActionListener(listener1);
JButton mySubtraction = new JButton("123-12"); Subtraction listener2 = new Subtraction(); mySubtraction.addActionListener(listener2);
pane.add(exit);pane.add(myAddition);pane.add(mySubtraction);
frame.pack();frame.show();
}}
How not to engineer the application
// Addition.javaimport java.awt.event.*;
class Addition implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("The answer is 4");
System.exit(0); }}
// Subtraction.javaimport java.awt.event.*;
class Subtraction implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("The answer is 111");
System.exit(0); }}
// GoodBye.javaimport java.awt.event.*;
class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("Goodbye!");
System.exit(0); }}
How not to engineer the application
// MiniCalc.java - demo gridlayoutimport java.awt.*;import javax.swing.*;
class MiniCalc{ public static void main (String[] args){
JFrame frame = new JFrame("MiniCalc");Container pane = frame.getContentPane();
//Creating the major componentsJTextField firstNumber= new JTextField(20);JTextField secondNumber= new JTextField(20);JTextField result= new JTextField(20);
JButton addButton = new JButton("Add");JButton subButton = new JButton("Subtract");
pane.setLayout(new GridLayout(4,2));
pane.add(new JLabel("Enter a number"));pane.add(firstNumber);pane.add(new JLabel("Enter a number"));pane.add(secondNumber);pane.add(new JLabel("Result"));pane.add(result);pane.add(addButton);pane.add(subButton);
DoMath listener = new DoMath(firstNumber, secondNumber, result);subButton.addActionListener(listener);addButton.addActionListener(listener);frame.pack();frame.show();
}}
Dividing the Container in a grid with 4 rows and 2 columns
// DoMath.java
import javax.swing.*;import java.awt.event.*;
class DoMath implements ActionListener{
DoMath(JTextField first, JTextField second, JTextField result){inputOne = first;inputTwo = second;output = result;
}
public void actionPerformed(ActionEvent e){double first, second;first = Double.parseDouble(inputOne.getText().trim());second = Double.parseDouble(inputTwo.getText().trim());
if (e.getActionCommand().equals("Add")) output.setText(String.valueOf(first+second));else
output.setText(String.valueOf(first-second)); }
private JTextField inputOne, inputTwo, output;}
Simple if…else statement
The LEFT layout
// FlowLayoutTest.java - demo flowlayout
import java.awt.*;import javax.swing.*;
class FlowLayoutTest{ public static void main (String[] args){
JFrame frame = new JFrame("FlowLayoutTest.LEFT");Container pane = frame.getContentPane();pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.add(new JButton("Button 1"));pane.add(new JLabel("Label 2"));pane.add(new JButton("Button 3"));pane.add(new JLabel("Label 4"));pane.add(new JButton("Button 5"));frame.pack();frame.show();
}}
CENTER
LEFT
Adding a button to StartTest
// StarTestQuit.java - add a quit button to StartTestimport java.awt.*;import javax.swing.*;
class StarTestQuit{ public static void main (String[] args){
JFrame frame = new JFrame("StartTest");Container pane = frame.getContentPane();
Star star = new Star();
JButton Quit = new JButton("Quit");pane.setLayout(new FlowLayout());Quit.addActionListener(new GoodBye());
pane.add(Quit);pane.add(star);frame.pack();frame.show();
}}
Adding an actionlistener to the button based on the GoodBye class (from earlier class example)
Adding the button and the star to the container
Showing the frame
Packing the frame to smallest size
Drawing in a frame// StarTest.java - display a starburst
import java.awt.*;import javax.swing.*;
class StarTest{ public static void main (String[] args){
JFrame frame = new JFrame("StartTest");Container pane = frame.getContentPane();
Star star = new Star();pane.add(star);frame.pack();frame.show();
}}
The Star Class// Star.java - draws a star
import javax.swing.*;import java.awt.*;
class Star extends JComponent{ public void paint (Graphics g){
double x1, x2, y1, y2;for (double angle=0; angle<Math.PI; angle = angle + Math.PI / 16) {
x1=Math.cos(angle) * RADIUS + RADIUS;y1=Math.sin(angle) * RADIUS + RADIUS;x2=Math.cos(angle + Math.PI) * RADIUS + RADIUS;y2=Math.sin(angle + Math.PI) * RADIUS + RADIUS;g.drawLine((int)x1, (int)y1,(int)x2, (int)y2);}
} public Dimension getMinimumSize(){
return new Dimension(2 * RADIUS, 2 * RADIUS); } public Dimension getPreferredSize(){
return new Dimension(2 * RADIUS, 2 * RADIUS); }
private static final int RADIUS = 100;}
// SimplePaint.java - drawing with a mouse
import java.awt.*;import javax.swing.*;
class SimplePaint{ public static void main (String[] args){
JFrame frame = new JFrame("SimplePaint");Container pane = frame.getContentPane();
DrawingCanvas canvas = new DrawingCanvas();PaintListener listener=new PaintListener();canvas.addMouseMotionListener(listener);
pane.add(canvas);frame.pack();frame.show();
}}
// DrawingCanvas.java - a blank canvas
import javax.swing.*;import java.awt.*;
class DrawingCanvas extends JComponent{
public Dimension getMinimumSize(){return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// PaintListener.java
import java.awt.*;import java.awt.event.*;
class PaintListener implements MouseMotionListener{ public void mouseDragged(MouseEvent e) {
DrawingCanvas canvas = (DrawingCanvas)e.getSource();Graphics g = canvas.getGraphics();g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){} private int radius =3; private int diameter = radius*2;
}
This class implements the interface MouseMotionListener
The interface MouseMotionListener requires that two methods are implemented:
1) mouseDragged (button clicked and held down)2) mouseMoved (no button clicked and held down)
Everytime a window is placed over the drawing, the text that was overlapped is removed when the window becomes active again
// PaintListener2.java - paints on a DrwaingCanvas2// and it is associated with an offscreenImage.
import java.awt.*;import java.awt.event.*;class PaintListener2 implements MouseMotionListener{ public void mouseDragged(MouseEvent e) {
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();Graphics g = canvas.getGraphics();g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
// duplicating the drawing on the offscreenImageImage image= canvas.getOffscreenImage();g = image.getGraphics();g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
} public void mouseMoved(MouseEvent e){} protected int radius =3; protected int diameter = radius*2;}
// SimplePaint2.java - drawing with a mouse
import java.awt.*;import javax.swing.*;
class SimplePaint2{ public static void main (String[] args){
JFrame frame = new JFrame("SimplePaint");Container pane = frame.getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();PaintListener2 listener=new PaintListener2();canvas.addMouseMotionListener(listener);
pane.add(canvas);frame.pack();frame.show();
}}
// DrawingCanvas2.java - a blank canvas that remembers// drawing operations using an offscreen image
import javax.swing.*;import java.awt.*;
class DrawingCanvas2 extends JComponent{ public void paint(Graphics g){
if (offscreenImage!=null) g.drawImage(offscreenImage,0,0,SIZE,SIZE,null);
} public Image getOffscreenImage(){
if (offscreenImage==null) offscreenImage=createImage(SIZE,SIZE); return offscreenImage;
} public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE); } public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE); } private static final int SIZE=500; private Image offscreenImage;}
Everytime a window is placed over the drawing, the offscreenimage is called when the window becomes active again