Download presentation
Presentation is loading. Please wait.
Published byMargery Manning Modified over 8 years ago
1
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming
2
Visual Applications Exist in own window (JFrame ) main constructs an instance of the application –Constructs and adds own visual components –Displays itself on screen –Handles events JVM starts application by calling its main method main method main method constructs an instance of that class Instance of class OS calls code when visual components activated Constructor adds visual components to its surface
3
Visual Application Structure public class AppName extends JFrame … { declare visual components as member variables public AppName() { construct visual components add to surface of application … setSize(width, height); setVisible(true); } … public static void main(String[] args) { AppName a = new AppName(); } } 1) main called 2) Which in turn constructs an object of this class 3) Which constructs and adds visual components to itself, and makes itself visible
4
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)
5
Visual Component Classes JButton –A constructor: JButton(String label) label to display –Some methods: void setText(String label) change label void setEnabled(boolean b) can user press? (default: true)
6
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)
7
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
8
Visual Component Classes JLabel (cannot be entered into by user) –A constructor: JLabel(String label) displays this –Some methods: void setText(String s)
9
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
10
Adding Components to Surface ContentPane –Member variable representing visible surface of app –Accessed with getContentPan e method –Add JPanel to surface and add components to it Application ContentPane getContentPane() JPanel add
11
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)
12
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!"); }
13
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
14
Event-driven Programming Application Construct button Have it notify me about events by passing my own address User presses helloButton.addActionListener(this); Button notifies application actionPerformed(ActionEvent e) location 37A4 37A4
15
Clock Object as Member Variable ClockProject package Main.java visual application Stores current hour and minute Allows hour and minute to be set Increments to next second or minute Clock.java Clock.java business logic clock
16
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());
17
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); }
18
Parsing Note that a JTextField contains a string Must parse to a numeric value before manipulating getText() String parsing numeric value for math Key: Each simple type has corresponding class int Integer double Double boolean Boolean char Char Built-in tools to manipulate simple type values
19
Parsing Parsing syntax: type = Classname.parseType(String); Examples: int h = Integer.parseInt(hourField.getText()); double area = Double.parseDouble(areaString);
20
Popups Static methods in JOptionPane class Simple message : JOptionPane.showMessageDialog( location, String); displayed Example: JOptionPane.showMessageDialog(null, "The radius is " + radiusString); Usual values: null center of screen this same location as app
21
Popups Prompting for values: String reply = JOptionPane. showInputDialog(location,String); Returns string entered by user Example: String areaString = JOptionPane.showInputDialog(null, "Enter the area of a circle"); Note: returns null if Cancel button pressed instead
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.