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”);
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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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 (); //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(); }
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 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
//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);
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…………..
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 ……..
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