swing gui components

22
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”);

Upload: lexine

Post on 16-Jan-2016

72 views

Category:

Documents


0 download

DESCRIPTION

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: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Swing GUI Components

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”);

Page 2: Swing GUI Components

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 …

Page 3: Swing GUI Components

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 (); //place components on panel thePanel.add(xLabel); thePanel.add(xField); thePanel.add(yLabel); thePanel.add(yField); thePanel.add(anotherButton);

JFrame theFrame = new JFrame(); //put panel in frame and show theFrame.setContentPane(thePanel); theFrame.pack(); //adjust components to framesize theFrame.show(); }

Page 4: Swing GUI Components

Let’s execute applet myFrapp so far……..

( website: myFrapp1.java )

Page 5: Swing GUI Components

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

}

Page 6: Swing GUI Components

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);

Page 7: Swing GUI Components

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}

Page 8: Swing GUI Components

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.

Page 9: Swing GUI Components

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 }}

Page 10: Swing GUI Components

//THIS WILL DRAW the first TRIANGLEpublic 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

Page 11: Swing GUI Components

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………

Page 12: Swing GUI Components

Check that this runs correctly……….

(website: myFrapp.java)

Page 13: Swing GUI Components

We now know all we need to write a frame application..

Examine the following applet:

Page 14: Swing GUI Components

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);

Page 15: Swing GUI Components

// 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

Page 16: Swing GUI Components

Check that this runs correctly……….

(website: Step1.java)

We really didn’t need the applet at all…………..

Page 17: Swing GUI Components

public class Step1 extends Applet { public class Step1 {

//create gui components //set up listener for button //create panel //place components on panel //create frame //put panel in frame and show

}}

public Step1() {public static void main (String [] args) {

now we have a frame application ……..

This is what we have so far ……..

Page 18: Swing GUI Components

Check that this runs correctly……….

(website: Step2.java)

Now let’s provide a larger output area and customize the frame a bit………

Page 19: Swing GUI Components

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); } }

Page 20: Swing GUI Components

// 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);

Page 21: Swing GUI Components

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();

Page 22: Swing GUI Components

Check that this runs correctly……….

(website: Step3.java)

Try packing the frame and see what happens……….