Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: • identify and use some of the common components of the Java Swing package; • program graphics components to handle mouse-click events; • describe the role of layout managers; • use the FlowLayout and BorderLayout managers; • make use of compound containers.
Graphics and event-driven programs. Learning objectives. By the end of this lecture you should be able to:. identify and use some of the common components of the Java Swing package; program graphics components to handle mouse-click events; describe the role of layout managers ; - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Graphics and event-driven programs
Learning objectives
By the end of this lecture you should be able to:
• identify and use some of the common components of the Java Swing package;
• program graphics components to handle mouse-click events;
• describe the role of layout managers;
• use the FlowLayout and BorderLayout managers;
• make use of compound containers.
Which printer [a] LPT1 or [b] LPT2?:
How many copies do you wish to print?:
[P]rint now or [C]ancel?:
Print all pages (y/n)?:
1
2
y
C
Swing and AWT
AWTSwing
Swing Components
The SmileyFace class
public class RunSmileyFace{ public static void main(String[] args) { new SmileyFace(); }}
public class SmileyFace
import java.awt.*;import javax.swing.*;
{ public SmileyFace() { // initialise the screen }
public void paint(Graphics g) { // draw onto the screen }}
class PushMe extends JFrame implements ActionListener{ private JTextField myTextField = new JTextField(15); private JButton myButton = new JButton("please push me"); private JLabel myLabel = new JLabel("Enter some text and push the button", JLabel.CENTER); public PushMe() { // code goes here }
public void actionPerformed(ActionEvent e) { // code goes here }}
public PushMe()
{
setTitle("Push Me");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(220,120);
setLocation(400, 300);
add(myTextField);
add(myButton);
add(myLabel);
myButton.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
String myText;myText = myTextField.getText();
myLabel.setText("You entered: " + myText);
myTextField
myLabel
A GUI for the Oblong class
JTextArea
JButton
JLabel
JTextField JLabel JTextField
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OblongGUI extends JFrame implements ActionListener
{
private Oblong myOblong = new Oblong(0,0);
private JLabel lengthLabel = new JLabel("Length");
private JTextField lengthField = new JTextField(5);
private JLabel heightLabel = new JLabel("Height");
private JTextField heightField = new JTextField(5);
private JButton calcButton = new JButton("Calculate");
private JTextArea displayArea = new JTextArea(2,20);
public void actionPerformed(ActionEvent e){ String lengthEntered = lengthField.getText(); String heightEntered = heightField.getText(); if(lengthEntered.length() == 0 || heightEntered.length() == 0) { displayArea.setText("Length and height must be entered"); } else { myOblong.setLength(Double.parseDouble(lengthEntered)); myOblong.setHeight(Double.parseDouble(heightEntered)); displayArea.setText("The area of the oblong is " + myOblong.calculateArea() + "\n" + "The perimeter of the oblong is " + myOblong.calculatePerimeter()); } }}