Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.

Similar presentations


Presentation on theme: "Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction."— Presentation transcript:

1 Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction with the user. GUI’s accept input from: the keyboard a mouse. A window in a GUI consists of components that: present data to the user allow interaction with the application.

2 Introduction Some common GUI components are:
buttons, labels, text fields, check boxes, radio buttons, combo boxes, and sliders.

3 Text Fields provide a line of user-updatable text need to
- import javax.swing.*; - declare a variable private JTextField input; - construct input = new JTextField( "Enter text here " ); - place in a window - use. For example, String userInput = input.getText();

4 import java. awt. ; import javax. swing
import java.awt.*; import javax.swing.*; public class GUIDemo extends JApplet { private JTextField input; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); Container contentPane = getContentPane(); contentPane.add( input, BorderLayout.NORTH ); contentPane.validate(); }

5 Labels JLabel class provided by Swing Single line of read-only text
A passive component - no event listener required! Useful for providing labels for sliders, text fields, etc.

6 Using JLabels Two ways to construct Modifiable with setText method
Providing text only JLabel speedLabel = new JLabel("Speed is "); Providing text and justification JLabel speedLabel = new JLabel("Speed is ", JLabel.RIGHT); Modifiable with setText method speedLabel.setText("Speed is " + speed);

7 import java. awt. ; import javax. swing
import java.awt.*; import javax.swing.*; public class GUIDemo2 extends JApplet { private JTextField input; private JLabel label; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); Container contentPane = getContentPane(); contentPane.add( input, BorderLayout.NORTH ); contentPane.add( label, BorderLayout.SOUTH ); contentPane.validate(); }

8 Buttons Let’s add a button to our previous program.
Want clicks on the button to trigger a program action Requires slightly more programming effort than text fields

9 Checklist for using JButtons
Construct the JButton Add it to the content pane of the JApplet and validate So that the JApplet responds to events generated by the JButton Add this as a listener Make sure your JApplet implements ActionListener requires to add an actionPerformed method

10 Action Events When a JButton is clicked an ActionEvent is generated
actionPerformed is executed Just as onMouseClick is executed whenever a user clicks on the canvas actionPerformed is provided with a parameter Contains information about the object that triggered the event

11 import java. awt. ; import java. awt. event. ; import javax. swing
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo3 extends JApplet implements ActionListener { private JTextField input; private JLabel label; private JButton button; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("click"); button.addActionListener(this); Container contentPane = getContentPane(); contentPane.add( input, BorderLayout.NORTH ); contentPane.add( label, BorderLayout.SOUTH ); contentPane.add( button, BorderLayout.CENTER); contentPane.validate(); }

12 public void actionPerformed(ActionEvent e){ label. setText(input
public void actionPerformed(ActionEvent e){ label.setText(input.getText()); }

13

14 Layout of Components in a Container
Many layout options One example: BorderLayout (default JApplet contentPane) BorderLayout.NORTH BorderLayout.EAST BorderLayout.SOUTH BorderLayout.WEST BorderLayout.CENTER Sizes everything to fill space

15 FlowLayout Alternate layout that sequences components left to right, allows to keep natural size

16 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo4 extends JApplet implements ActionListener { private JTextField input; private JLabel label; private JButton button; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("click"); button.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout( new FlowLayout()); contentPane.add( input); // removed NORTH, SOUTH, etc contentPane.add( label ); contentPane.add( button); contentPane.validate(); } public void actionPerformed(ActionEvent e){ label.setText(input.getText());

17

18 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo5 extends JApplet implements ActionListener { private JTextField input; private JLabel label; private JButton button, button2; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("update"); button.addActionListener(this); button2 = new JButton("font"); button2.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout( new FlowLayout()); contentPane.add( input); contentPane.add( label ); contentPane.add( button); contentPane.add( button2); contentPane.validate(); }

19 Processing multiple buttons
Use e.getSource() to tell which was clicked public void actionPerformed(ActionEvent e){ if (e.getSource()==button){ label.setText(input.getText()); } if (e.getSource()==button2){ label.setFont( new Font("Arial",Font.PLAIN,20));

20

21 Your Assignment Add more buttons, labels, and text fields buttons:
copy box1 to box2 copy box2 to box1 exchange boxes (use a temp string to hold one) google swap two string variables make all the fonts grow by 20 percent (*1.2)

22 Review Construct the GUI component
Add the component to a container (i.e., a panel or the content pane of a window) If a listener is needed Add this as a listener for the GUI component Add the event-handling methods promised by the listener interface


Download ppt "Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction."

Similar presentations


Ads by Google