Buttons and Frames

Download Report

Transcript Buttons and Frames

Swing GUI Components
You can create graphics components to place on your applet using
classes available in the Swing package ( javax.swing )
Class names start with J :
JTextField , JLabel, JButton
Objects are created by calling a class constructor:
JLabel xLabel = new JLabel("x = ");
JTextField xField = new JTextField(width);
JButton moveButton = new JButton("Move",buttonIcon);
JButton anotherButton = new JButton(“go”);
use Control panel
These components are placed in a control panel, a container for user
interface components.
JPanel thePanel = new JPanel ();
thePanel.add(xLabel);
thePanel.add(xField);
thePanel.add(moveButton);
thePanel.add(anotherButton);
This panel can then be placed in a frame, and the frame shown.
JFrame theFrame = new JFrame();
theFrame.setcontentPane(thePanel);
theFrame.pack();
theFrame.show();
*it is possible to put the panel directly on the applet, but more GUI experience is needed …
public class myFrapp extends Applet {
public myFrapp() {
JLabel xLabel = new JLabel("x = "); //instantiate components
JTextField xField = new JTextField(5);
JLabel yLabel = new JLabel(“y = ");
JTextField yField = new JTextField(5);
JButton anotherButton = new JButton(“Go");
JPanel thePanel = new JPanel ();
thePanel.add(xLabel);
thePanel.add(xField);
thePanel.add(yLabel);
thePanel.add(yField);
thePanel.add(anotherButton);
//place components on panel
JFrame theFrame = new JFrame();
//put panel in frame and show
theFrame.setContentPane(thePanel);
theFrame.pack();
//adjust components to framesize
theFrame.show();
}
Let’s execute applet myFrapp so far……..
( website: myFrapp1.java )
In order to recognize when the button is pressed, we need an event
listener which is registered with the button !!
public class myFrapp extends Applet{
public myFrapp() {
//instantiate components
NEW!!
//create a listener class
NEW!!
//register a listener object with button component
//place components on panel
//put panel in frame and show
}
Button clicks create ActionEvent objects
A class which is to be used for creating action listener object
must implement the ActionListener interface.
*also in java.awt.event package
The ActionListener interface contains one abstract method:
void actionPerformed(ActionEvent event);
So our listener must implement the ActionListener interface.
public class myFrapp extends Applet{
public myFrapp() {
//instantiate components
//create listener class
class BtnListener implements ActionListener{
public void actionPerformed(ActionEvent e){
// code task to be done when button is pushed
}
}
//register a listener object with button component
ActionListener bltn = new BtnListener();
anotherButton.addActionListener(bltn);
}
//place components on panel
//put panel in frame and show
Let’s finish our applet so that when the user presses
the button, the values in the textfields are used to
reposition a triangle on the applet.
A Triangle class would be helpful here, so assume:
public class Triangle{
private double startx, starty;
public Triangle(double sx, double sy) {
startx = sx;
starty = sy;
}
public void setTri( double sx, double sy){
startx = sx;
starty = sy;
}
}
public void draw (Graphics2D g) {
// code which draws a triangle at position (startx,starty) using
// graphics environment g
}
//THIS WILL DRAW the first TRIANGLE
public class myFrapp extends Applet{
private Triangle tri = new Triangle(50.00,50.00);
public myFrapp() {
//instantiate components
//create a listener class
//register a listener object with button component
//place components on panel
//put panel in frame and show
}
public void paint (Graphics g)
Graphics2D g2 = (Graphics2D) g;
tri.draw(g2);
}
//now we are ready to write that button listener
The button listener class……
class BtnListener implements ActionListener{
public void actionPerformed(ActionEvent e){
double newx = Double.parseDouble(xField.getText()); *
double newy = Double.parseDouble(yField.getText());
tri.setTri(newx,newy);
repaint();
}
}
//register a listener object with button component
ActionListener bltn = new BtnListener();
anotherButton.addActionListener(bltn);
* If declared as local variables, must be declared as final………
Check that this runs correctly……….
(website: myFrapp.java)
We now know all we need to write a frame application..
Examine the following applet:
public class myFrapp extends Applet {
public myFrapp() {
JLabel xLabel = new JLabel("x = "); //create gui components
final JTextField xField = new JTextField(5);
JLabel yLabel = new JLabel(“y = ");
final JTextField yField = new JTextField(5);
JButton anotherButton = new JButton(“Go");
//set up listener for button
class BtnListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int val1 = Integer.parseInt(xField.getText()); *
int val2 = Integer.parseInt(yField.getText());
val1 = val1+val2;
xField.setText(Integer.toString(val1));
}
}
ActionListener lstn = new BtnListener();
anotherButton.addActionListener(lstn);
// constructor continued
JPanel thePanel = new JPanel ();
//place components on panel
thePanel.add(xLabel);
thePanel.add(xField);
thePanel.add(yLabel);
thePanel.add(xField);
thePanel.add(anotherButton);
JFrame theFrame = new JFrame();
//put panel in frame and show
theFrame.setContentPane(thePanel);
theFrame.pack(); //adjust components to framesize
theFrame.show();
} //end constructor
} //end applet
Check that this runs correctly……….
(website: Step1.java)
We really didn’t need the applet at all…………..
This is what we have so far ……..
public class
class Step1
Step1 {extends Applet {
public
public
public static
Step1()
void main (String [] args) {
{
//create gui components
//set up listener for button
//create panel
//place components on panel
//create frame
//put panel in frame and show
}
}
now we have a frame application ……..
Check that this runs correctly……….
(website: Step2.java)
Now let’s provide a larger output area and customize
the frame a bit………
Now the output is sent to a text area…..
JLabel xLabel = new JLabel("x = ");
final JTextField xField = new JTextField(width);
JLabel yLabel = new JLabel("y = ");
final JTextField yField = new JTextField(width);
final JTextArea sumArea = new JTextArea(10,20);
JButton anotherButton = new JButton("Go");
//create listener class
class BtnListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int val1 = Integer.parseInt(xField.getText());
int val2 = Integer.parseInt(yField.getText());
val1 = val1 + val2;
sumArea.append("The result is: " + val1);
}
}
// put components on panel and set panel color to red
//add components to panel
JPanel thePanel = new JPanel ();
thePanel.setBackground(Color.red);
thePanel.add(xLabel);
thePanel.add(xField);
thePanel.add(yLabel);
thePanel.add(yField);
thePanel.add(anotherButton);
thePanel.add(sumArea);
and program will now exit when frame is closed,
Size the frame rather than pack,
Set a background color for the frame (frame is UNDER the panel)
Place the frame is a particular location
JFrame theFrame = new JFrame();
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = new Dimension(200,300);
theFrame.setSize(dim);
theFrame.setBackground(Color.cyan);//this is UNDER the panel
theFrame.setLocation(0,0);
theFrame.setContentPane(thePanel);
theFrame.show();
Check that this runs correctly……….
(website: Step3.java)
Try packing the frame and see what happens……….