Application - YSU Computer Science & Information Systems

Download Report

Transcript Application - YSU Computer Science & Information Systems

Java Visual Applications
CSIS 3701: Advanced Object Oriented Programming
Applications in Java
• Must contain a main method:
public static void main(String[] args)
{ …
}
Used to pass any
command line arguments
Simple “hello world” application
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello world!”);
}
}
Writes string to “standard output”
-- DOS window
-- Status window in NetBeans
Visual Applications
• Exist in own window (JFrame)
• main constructs an instance of the application
– Constructs and adds own visual components
– Displays itself on screen
Visual Application Structure
public class AppName extends JFrame … {
declare visual components as member variables
2) Which
in turn
constructs
an object
of this
class
1)
main
called
public AppName() {
construct visual components
3) Which
add to surface of application
constructs and
…
adds visual
setSize(width, height);
components to
setVisible(true);
itself, and makes
itself visible
}
…
public static void main(String[] args) {
AppName a = new AppName();
}
}
Visual Component Classes
• Objects in Java
– Must construct and add to surface of application
– Must import javax.swing.* package
• Basic types:
– JButton
– JTextField
– JPanel
(surface other components attached to for display)
Visual Component Classes
• JButton
– A constructor:
JButton(String label)
label to display
– Some methods:
void setText(String label)
void setEnabled(boolean b)
change label
can user press?
(default: true)
Visual Component Classes
• JTextField
– A constructor:
JTextField(int width)
width in columns
– Some methods:
void setText(String s) display this string
String getText()
get current value
(what user entered)
void setEditable(boolean b) can user type?
(default: true)
Visual Component Classes
• JTextArea (multi-line text area)
– A constructor:
JTextArea(int rows,
int cols)
sets width and height
– Some methods:
void setText(String s) display this string
String getText()
get current value
void append(String s)
add to current text
rather than replacing
Visual Component Classes
• JLabel (cannot be entered into by user)
– A constructor:
JLabel(String label)
– Some methods:
void setText(String s)
displays this
Adding Components to Surface
• JPanel
– A constructor: JPanel()
Default constructor adds other components left-to-right
across suface
– Some methods:
void add(Component c) Add this component to
surface of panel
Adding Components to Surface
• ContentPane
– Member variable representing visible surface of app
– Accessed with getContentPane method
– Add JPanel to surface and add components to it
Application
ContentPane
JPanel
add
getContentPane()
add
Adding Components to Surface
// Create a new visual panel and add it to the
// surface (the ContentPane) of the application.
JPanel mainPanel = new JPanel();
getContentPane().add(mainPanel);
// Add the button and textfield to
// panel to make them visible.
mainPanel.add(helloButton);
mainPanel.add(responseField);
Side point: Unlike other
components, declared
mainPanel as local variable
since not used outside
constructor (more efficient)
Event-driven Programming
• Code executed at startup to initialize
– Code implemented in constructor
• Additional code executed on demand when user
causes events (button press, etc.)
– Usually done in actionPerformed method of the
application
public void actionPerformed(ActionEvent e) {
responseField.setText("Welcome to CSIS 3701!");
}
Event-driven Programming
• Application registers as an “ActionListener”
– Can handle “action events” created by components
public class Main extends JFrame
implements ActionListener {
• Application tells button to notify it when pressed
– Adds itself as an ActionListener to the button
helloButton.addActionListener(this);
Reference to the application itself
Event-driven Programming
location 37A4
Construct button
Application
Have it notify me about events
by passing my own address
helloButton.addActionListener(this);
37A4
Button notifies application
actionPerformed(ActionEvent e)
User
presses
Event Objects
• Java events are objects with properties/methods
– Event object passed to application as parameter
public void actionPerformed(ActionEvent e)
• Contain useful information about event
– Example: which component caused event
public Object getSource()
– Can use to execute different code for different events
if (e.getSource() == helloButton) {…
Event-driven Programming
location 7981
helloButton: 7981
Application
Button notifies application
actionPerformed(ActionEvent e)
ActionEvent object
Source: 7981
User
presses
Event Objects
• Example: application to move text either left or right
based on which button pressed
private JButton leftButton;
private JButton rightButton;
public Main() {
leftButton = new JButton("MOVE LEFT");
rightButton = new JButton("MOVE RIGHT");
leftButton.addActionListener(this);
actionPerformed
rightButton.addActionListener(this);
called if either pressed
Event Objects
• Use getSource to find whether to move text left or right
public void actionPerformed(ActionEvent e) {
String temp;
if (e.getSource() == leftButton) {
temp = rightField.getText();
leftField.setText(temp);
rightField.setText("");
}
if (e.getSource() == rightButton) {
temp = leftField.getText();
rightField.setText(temp);
leftField.setText("");
}
}
Clock Object as Member Variable
ClockProject package
• Stores current hour and
clock
minute
• Allows hour and minute
to be set
• Increments to next
second or minute
Main.java
Clock.java
visual application
business logic
Clock Object as Member Variable
• Main application contains a Clock object:
– Stores as member variable
– Constructs and initializes in constructor
– Manipulates it in response to events
public class Main extends JFrame
implements ActionListener {
private Clock clock;
…
public Main() {
…
clock = new Clock();
timeField.setText(clock.toString());
Clock Object as Member Variable
public void actionPerformed(ActionEvent actionObject) {
if (actionObject.getSource() == tickButton) {
clock.nextMinute();
}
if (actionObject.getSource() == setButton) {
int h = Integer.parseInt(hourField.getText());
int m = Integer.parseInt(minuteField.getText());
clock.setHour(h);
clock.setMinute(m);
}
String timeString = “” + clock.getHour() +
”:” + clock.getMinute();
timeField.setText(timeString);
}