1 Replies - 5820 Views - Last Post: 20 October 2005 - 04:07 AM Rate Topic: -----

#1 Rk47  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 11-October 05

Adding JButtons

Posted 19 October 2005 - 09:00 PM

Hello everyone , can anyoneplease tell me how to add a squareroot button to this code. I mean what do I write in actionPerformed ?
Thanks!!!


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Squareplay extends JApplet implements ActionListener{
  JButton square = new JButton ("Square");
  JButton quit = new JButton ("Quit");
  JLabel value = new Jlabel();
  JTextField numField = new JtextField(10);
  Container con = getContentpane();
  
  public void init() {
    con.setLayout ( new FlowLayout());
    con.add (square);
    con.add (numfield);
    con.add (value);
    con.add (quit);
    square.addActionListener(this);
    quit.addActionListener(this);
  }
  
  public void paint (Graphics g){
    square.repaint();
    quit.repaint();
    value.repaint();
    numField.repaint();
  }
  public void actionPerformed(ActionEvent e){
    Object source = e.getSource;
    if (source == square) {
      String s = numfield.getText();
      int k = Integer.parseInt(s);
      k = k*k;
      String result = " " + k;
      value.setText(result);
      repaint();
    }
    else
      if (source == quit) system.exit(0);
  }
}
}



Is This A Good Question/Topic? 1

Replies To: Adding JButtons

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Adding JButtons

Posted 20 October 2005 - 04:07 AM

You can use the Math.sqrt() method from the math library....see here, and here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1