Download Textfield and Mouse events ppt

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Previous programs used a JLabel for OUTPUT.
Another Swing component that can be used for
both user input and output is the JTextfield.
Suppose we want to create a GUI which allows
the user to enter two values, and have them
summed when a button is pressed.
Continued…
We will need two textfields, one for each value
to be summed …………
• To create a JTextField component, call a
JTextfield class constructor.
http://java.sun.com/j2se/1.5.0/docs/api/
final int FIELD_WIDTH = 10; // In characters
JTextField op1 = new JTextField(FIELD_WIDTH);
OR
JTextField op1 = new JTextField(“0”);
OR
final int FIELD_WIDTH = 10; // In characters
JTextField op1 = new JTextField(“0”, FIELD_WIDTH);
It is good form to label a textfield, so that the user
knows what it is for:
JLabel op1Label = new JLabel(“Operand 1: ");
JLabel op2Label = new JLabel(“Operand 2: ");
*
final int FIELD_WIDTH = 10; // In characters
JTextField op1TxtFld = new JTextField(“0”, FIELD_WIDTH);
JTextField op1TxtFld = new JTextField(“0”, FIELD_WIDTH);
JTextField sumTxtFld = new JTextField(“0”, FIELD_WIDTH);
http://java.sun.com/j2se/1.5.0/docs/api/
We will also need to create the button …
JButton addBtn = new JButton(“ADD”);
//The button's actionPerformed method reads the user input from the
//input text fields, converts to int, sums them, and sets output textfield.
class AddListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
}
int op1Int = Integer.parseInt(op1TxtFld.getText() );
int op2Int = Integer.parseInt(op2TxtFld.getText() );
int sum = op1Int + op2Int;
sumTxtFld.setText(“” + sum);
* note: textfields from main are used, must be declared as Final
// create listener object and install on button
AddListener alisten = new AddListener();
addBtn.addActionListener(alisten);
So,.. Component declarations would be ……
JLabel op1Label = new JLabel(“Operand 1: ");
JLabel op2Label = new JLabel(“Operand 2: ");
*
final int FIELD_WIDTH = 10; // In characters
final JTextField op1TxtFld = new JTextField(“0”, FIELD_WIDTH);
final JTextField op1TxtFld = new JTextField(“0”, FIELD_WIDTH);
final JTextField sumTxtFld = new JTextField(“0”, FIELD_WIDTH);
http://java.sun.com/j2se/1.5.0/docs/api/
So we have…………
public class Adder {
public static void main (String[] args) {
JLabel op1Label = new JLabel(“Operand 1: ");
JLabel op2Label = new JLabel(“Operand 2: ");
final int FIELD_WIDTH = 10; // In characters
final JTextField op1TxtFld = new JTextField(“0”, FIELD_WIDTH);
final JTextField op2TxtFld = new JTextField(“0”, FIELD_WIDTH);
final JTextField sumTxtFld = new JTextField(“0”, FIELD_WIDTH);
JButton addBtn = new JButton(“ADD”);
class AddListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int op1Int = Integer.parseInt(op1TxtFld.getText() );
int op2Int = Integer.parseInt(op2TxtFld.getText() );
int sum = op1Int + op2Int;
sumTxtFld.setText(“” + sum);
}
}
AddListener alisten = new AddListener();
addBtn.addActionListener(alisten);
//components are all ready to put on panel !!
JPanel panel = new JPanel();
panel.add(op1TxtFld);
panel.add(op2TxtFld);
panel.add(addBtn);
panel.add(sumTxtFld);
panel.add(op1Label);
panel.add(op2Label);
JFrame theframe = new JFrame();
theframe.setSize(100,200 );
theframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theframe.add(panel);
theframe.setVisible(true);
} //end main
} // end class
Mouse Events
• Use a mouse listener to capture mouse
events
• Implement the MouseListener interface:
public interface MouseListener
{
void mousePressed(MouseEvent event);
// Called when a mouse button has been pressed on a component
void mouseReleased(MouseEvent event);
// Called when a mouse button has been released on a component
void mouseClicked(MouseEvent event);
// Called when the mouse has been clicked on a component
void mouseEntered(MouseEvent event);
// Called when the mouse enters a component
void mouseExited(MouseEvent event);
Continued…
// Called when the mouse exits a component
}
Mouse Events
• mousePressed, mouseReleased: called
when a mouse button is pressed or released
• mouseClicked: if button is pressed and
released in quick succession, and mouse
hasn't moved
• mouseEntered, mouseExited: mouse has
entered or exited the component's area
Mouse Events
• Add a mouse listener to a component by
calling the addMouseListener method:
public class MyMouseListener implements MouseListener
{
// Implements five methods
}
MouseListener listener = new MyMouseListener();
component.addMouseListener(listener);
Continued…
Mouse Events
• Sample program: enhance
RectangleComponentViewer program of
Chapter 5; when user clicks on rectangle
component, move the rectangle
File RectangleComponent.java
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
import
import
import
import
java.awt.Graphics;
java.awt.Graphics2D;
java.awt.Rectangle;
javax.swing.JComponent;
/**
This component lets the user move a rectangle by
clicking the mouse.
*/
public class RectangleComponent extends JComponent
{
public RectangleComponent()
{
// The rectangle that the paint method draws
box = new Rectangle(BOX_X, BOX_Y,
BOX_WIDTH, BOX_HEIGHT);
}
Continued…
File RectangleComponent.java
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.draw(box);
}
/**
Moves the rectangle to the
@param x the x-position of
@param y the y-position of
*/
public void moveTo(int x, int
{
box.setLocation(x, y);
repaint();
}
given location.
the new location
the new location
y)
Continued…
File RectangleComponent.java
37:
38:
39:
40:
41:
42:
43:
44: }
private Rectangle box;
private
private
private
private
static
static
static
static
final
final
final
final
int
int
int
int
BOX_X = 100;
BOX_Y = 100;
BOX_WIDTH = 20;
BOX_HEIGHT = 30;
Mouse Events
• Call repaint when you modify the shapes
that paintComponent draws
box.setLocation(x, y); repaint();
• Mouse listener: if the mouse is pressed,
listener moves the rectangle to the mouse
location
Continued…
Mouse Events
class MousePressListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
component.moveTo(x, y);
}
// Do-nothing methods
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
• All five methods of the interface must be
implemented; unused methods can be empty
RectangleComponentViewer
Program Output
Figure 4:
Clicking the Mouse Moves the
Rectangle
File
RectangleComponentViewer2.java
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
/**
This program displays a RectangleComponent.
*/
public class RectangleComponentViewer
{
public static void main(String[] args)
{
final RectangleComponent component
= new RectangleComponent();
// Add mouse press listener
class MousePressListener implements MouseListener
{
Continued…
File
RectangleComponentViewer2.java
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
public void mousePressed(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
component.moveTo(x, y);
}
// Do-nothing methods
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
MouseListener listener = new MousePressListener();
component.addMouseListener(listener);
Continued…
File
RectangleComponentViewer2.java
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45: }
JFrame frame = new JFrame();
frame.add(component);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
Self Check
7. What would happen if you omitted the call
to repaint in the moveTo method?
8. Why must the MousePressListener class
supply five methods?
Answers
7. The rectangle would only be painted at the
new location when the component is
repainted for some other reason, for
example, when the frame is resized.
8. It implements the MouseListener interface,
which has five methods.
Related documents