Lesson_33_Layout_management_Java_GUI
Download
Report
Transcript Lesson_33_Layout_management_Java_GUI
Lesson 33:
Layout management and drawing – Java GUI
Drawing in a frame
Recap
1.
We will use the graphics and the Jframe together with a simple
loop that creates references for a line.
2.
The line needs a starting x,y coordinate and an ending x,y
coordinate
3.
We will use the class Star for definition and the class StartTest for
executing our star
Drawing in a frame
Recap
// 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();
}
}
Drawing in a frame
Recap
The layout
manipulations
// FlowLayoutTest.java - demo flowlayout
import java.awt.*;
import javax.swing.*;
Importing the libraries
Creating a class called FlowLayoutTest
class FlowLayoutTest{
Creating a main section
public static void main (String[] args){
JFrame frame = new JFrame("FlowLayoutTest.CENTER");
Container pane = frame.getContentPane();
pane.setLayout(new FlowLayout(FlowLayout.CENTER));
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();
}
}
Creating a frame named
frame and labeled
Flowlayout.CENTER
// FlowLayoutTest.java - demo flowlayout
import java.awt.*;
import javax.swing.*;
Creating a container named
pane, and associating it with
our frame called frame.
class FlowLayoutTest{
public static void main (String[] args){
JFrame frame = new JFrame("FlowLayoutTest.CENTER");
Container pane = frame.getContentPane();
pane.setLayout(new FlowLayout(FlowLayout.CENTER));
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();
}
}
Formatting the container to
FlowLayout.Center
// FlowLayoutTest.java - demo flowlayout
import java.awt.*;
import javax.swing.*;
class FlowLayoutTest{
public static void main (String[] args){
JFrame frame = new JFrame("FlowLayoutTest.CENTER");
Container pane = frame.getContentPane();
pane.setLayout(new FlowLayout(FlowLayout.CENTER));
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();
Creating buttons and labels
and adding them to the
container
Packing the frame to
smallest size
}
}
Showing the frame
We can resize the window using standard features in the frame:
The output
Compile
Program
Result
The LEFT layout
// FlowLayoutTest.java - demo flowlayout
CENTER
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();
}
}
LEFT
Adding buttons to actions
and frames
-Another example
Adding a button to StartTest
Importing the libraries
// StarTestQuit.java - add a quit button to StartTest
import java.awt.*;
import javax.swing.*;
Creating a class called StartTestQuit
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();
}
}
Creating a main section
Creating a frame named
frame and labeled StarTest
Adding a button to StartTest
// StarTestQuit.java - add a quit button to StartTest
import java.awt.*;
Creating a container named pane, and
import javax.swing.*;
associating it with our frame called frame.
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();
}
}
Creating a star based on
the star class (as before)
Creating a button named
Quit and labeled Quit
Formatting the container to
FlowLayout.Center
Adding a button to StartTest
// StarTestQuit.java - add a quit button to StartTest
import 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
Packing the frame to smallest size
Showing the frame
Program
Compile
Run
Exit
A simple Drawing
program
// SimplePaint.java - drawing with a mouse
import java.awt.*;
import javax.swing.*;
Importing the libraries
Creating a class called SimplePaint
class SimplePaint{
Creating a main section
public static void main (String[] args){
JFrame frame = new JFrame("SimplePaint");
Container pane = frame.getContentPane();
Creating a frame
named frame and
DrawingCanvas canvas = new DrawingCanvas();
labeled SimplePaint
PaintListener listener=new PaintListener();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
frame.pack();
frame.show();
}
}
Creating a container named
pane, and associating it with
our frame called frame.
// SimplePaint.java - drawing with a mouse
Creating a new object named
canvas based on the
DrawingCanvas class (we will
look at it later)
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);
Creating a listener based on
the PaintListener class
pane.add(canvas);
frame.pack();
frame.show();
}
}
Associating the
listener to the canvas
// 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);
Adding the canvas object to
the container
pane.add(canvas);
frame.pack();
frame.show();
Packing the frame to
}
smallest size
}
Showing the frame
// DrawingCanvas.java - a blank canvas
Importing libraries
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);
}
This class is an extension of
the JComponent class from
the swing library
Setting our preferred and
minimum size to the container
manager
private static final int SIZE=500;
}
Private protected variable
// PaintListener.java
Importing
libraries
This class implements the
interface MouseMotionListener
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;
}
The interface MouseMotionListener requires that two
methods are implemented:
1) mouseDragged (button clicked and held down)
2) mouseMoved (no button clicked and held down)
// PaintListener.java
import java.awt.*;
import java.awt.event.*;
Each time the mouse is moved – while the position of the over
the component that is being listened to, the mouseDragged()
method is called.
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;
}
The calls e.getSource() returns a generic
reference to the component that generated
the event. In general the event could have
come from any component not just
DrawingCanvas. Here it is explicitly cast to
the reference a DrawingCanvas object
Each change in position detected by the
system generates another call to
mouseDragged(). This results in many
calls by a single dragging of the mouse.
Each call is passed a a MouseEvent object
that contains, among other things, a
reference to the component that generated
the event and the coordinates of the
mouse at that time the event was
generated.
// PaintListener.java
Simply filling an oval when
the mouseDragged event
occurs (as before)
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;
}
Returning nothing when the mouse is moved
over the component being listened to
The outcome