1 / 22

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:

lexine
Download Presentation

Swing GUI Components

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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(); }

  4. Let’s execute applet myFrapp so far…….. ( website: myFrapp1.java )

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

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

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

  8. 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.

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

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

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

  12. Check that this runs correctly………. (website: myFrapp.java)

  13. We now know all we need to write a frame application.. Examine the following applet:

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

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

  16. Check that this runs correctly………. (website: Step1.java) We really didn’t need the applet at all…………..

  17. This is what we have so far …….. • public class Step1 { public static void main (String [] args) { public Step1() { • //create gui components • //set up listener for button • //create panel • //place components on panel • //create frame • //put panel in frame and show • } • } public class Step1 extends Applet { now we have a frame application ……..

  18. Check that this runs correctly………. (website: Step2.java) Now let’s provide a larger output area and customize the frame a bit………

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

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

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

  22. Check that this runs correctly………. (website: Step3.java) Try packing the frame and see what happens……….

More Related