Swing GUI Components You can create graphics components
- Slides: 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 : JText. Field , JLabel, JButton Objects are created by calling a class constructor: JLabel x. Label = new JLabel("x = "); JText. Field x. Field = new JText. Field(width); JButton move. Button = new JButton("Move", button. Icon); JButton another. Button = new JButton(“go”);
use Control panel These components are placed in a control panel, a container for user interface components. JPanel the. Panel = new JPanel (); the. Panel. add(x. Label); the. Panel. add(x. Field); the. Panel. add(move. Button); the. Panel. add(another. Button); This panel can then be placed in a frame, and the frame shown. JFrame the. Frame = new JFrame(); the. Frame. setcontent. Pane(the. Panel); the. Frame. pack(); the. Frame. show(); *it is possible to put the panel directly on the applet, but more GUI experience is needed …
public class my. Frapp extends Applet { public my. Frapp() { JLabel x. Label = new JLabel("x = "); //instantiate components JText. Field x. Field = new JText. Field(5); JLabel y. Label = new JLabel(“y = "); JText. Field y. Field = new JText. Field(5); JButton another. Button = new JButton(“Go"); JPanel the. Panel = new JPanel (); the. Panel. add(x. Label); the. Panel. add(x. Field); the. Panel. add(y. Label); the. Panel. add(y. Field); the. Panel. add(another. Button); //place components on panel JFrame the. Frame = new JFrame(); //put panel in frame and show the. Frame. set. Content. Pane(the. Panel); the. Frame. pack(); //adjust components to framesize the. Frame. show(); }
Let’s execute applet my. Frapp so far……. . ( website: my. Frapp 1. java )
In order to recognize when the button is pressed, we need an event listener which is registered with the button !! public class my. Frapp extends Applet{ public my. Frapp() { //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 Action. Event objects A class which is to be used for creating action listener object must implement the Action. Listener interface. *also in java. awt. event package The Action. Listener interface contains one abstract method: void action. Performed(Action. Event event);
So our listener must implement the Action. Listener interface. public class my. Frapp extends Applet{ public my. Frapp() { //instantiate components //create listener class Btn. Listener implements Action. Listener{ public void action. Performed(Action. Event e){ // code task to be done when button is pushed } } //register a listener object with button component Action. Listener bltn = new Btn. Listener(); another. Button. add. Action. Listener(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 set. Tri( double sx, double sy){ startx = sx; starty = sy; } } public void draw (Graphics 2 D g) { // code which draws a triangle at position (startx, starty) using // graphics environment g }
//THIS WILL DRAW the first TRIANGLE public class my. Frapp extends Applet{ private Triangle tri = new Triangle(50. 00, 50. 00); public my. Frapp() { //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) Graphics 2 D g 2 = (Graphics 2 D) g; tri. draw(g 2); } //now we are ready to write that button listener
The button listener class…… class Btn. Listener implements Action. Listener{ public void action. Performed(Action. Event e){ double newx = Double. parse. Double(x. Field. get. Text()); * double newy = Double. parse. Double(y. Field. get. Text()); tri. set. Tri(newx, newy); repaint(); } } //register a listener object with button component Action. Listener bltn = new Btn. Listener(); another. Button. add. Action. Listener(bltn); * If declared as local variables, must be declared as final………
Check that this runs correctly………. (website: my. Frapp. java)
We now know all we need to write a frame application. . Examine the following applet:
public class my. Frapp extends Applet { public my. Frapp() { JLabel x. Label = new JLabel("x = "); //create gui components final JText. Field x. Field = new JText. Field(5); JLabel y. Label = new JLabel(“y = "); final JText. Field y. Field = new JText. Field(5); JButton another. Button = new JButton(“Go"); //set up listener for button class Btn. Listener implements Action. Listener{ public void action. Performed(Action. Event e){ int val 1 = Integer. parse. Int(x. Field. get. Text()); * int val 2 = Integer. parse. Int(y. Field. get. Text()); val 1 = val 1+val 2; x. Field. set. Text(Integer. to. String(val 1)); } } Action. Listener lstn = new Btn. Listener(); another. Button. add. Action. Listener(lstn);
// constructor continued JPanel the. Panel = new JPanel (); //place components on panel the. Panel. add(x. Label); the. Panel. add(x. Field); the. Panel. add(y. Label); the. Panel. add(x. Field); the. Panel. add(another. Button); JFrame the. Frame = new JFrame(); //put panel in frame and show the. Frame. set. Content. Pane(the. Panel); the. Frame. pack(); //adjust components to framesize the. Frame. show(); } //end constructor } //end applet
Check that this runs correctly………. (website: Step 1. java) We really didn’t need the applet at all…………. .
This is what we have so far ……. . public class Step 1 {extends Applet { public static Step 1() 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: Step 2. 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 x. Label = new JLabel("x = "); final JText. Field x. Field = new JText. Field(width); JLabel y. Label = new JLabel("y = "); final JText. Field y. Field = new JText. Field(width); final JText. Area sum. Area = new JText. Area(10, 20); JButton another. Button = new JButton("Go"); //create listener class Btn. Listener implements Action. Listener{ public void action. Performed(Action. Event e){ int val 1 = Integer. parse. Int(x. Field. get. Text()); int val 2 = Integer. parse. Int(y. Field. get. Text()); val 1 = val 1 + val 2; sum. Area. append("The result is: " + val 1); } }
// put components on panel and set panel color to red //add components to panel JPanel the. Panel = new JPanel (); the. Panel. set. Background(Color. red); the. Panel. add(x. Label); the. Panel. add(x. Field); the. Panel. add(y. Label); the. Panel. add(y. Field); the. Panel. add(another. Button); the. Panel. add(sum. Area);
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 the. Frame = new JFrame(); the. Frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); Dimension dim = new Dimension(200, 300); the. Frame. set. Size(dim); the. Frame. set. Background(Color. cyan); //this is UNDER the panel the. Frame. set. Location(0, 0); the. Frame. set. Content. Pane(the. Panel); the. Frame. show();
Check that this runs correctly………. (website: Step 3. java) Try packing the frame and see what happens……….
- If you think you can you can poem
- 4logik
- Java swing responsive gui
- Ge gi gue gui güe güi
- Swing containers
- Graphics and guis with matlab
- If you can imagine it you can achieve it
- If you can’t measure it, you can’t improve it
- If you cannot measure it you cannot manage it
- If you don't measure it you can't manage it
- Investitionsanalyse kennzahlen
- Jika noel(create(q)) adalah 0, maka front(create(q)) adalah
- Graphics application software
- Swing components
- Java swing components
- Gui programmierung java
- Graphics monitors and workstations in computer graphics
- Introduction to computer graphics - ppt
- Gui components
- Gui components
- You can tell harris about it just ____(easily) as i can
- You can tell harris about it just ____(easily) as i can
- Positive comparative superlative