here's my code,
i wanted to add another button that would have its own ActionListener.
NOTE: its in object oriented style.
i wanted to add these codes in my main class for my additional buttons, but my problem is
i dont know how to make their ActionListeners.
myButton2=new Button("GET DIAMETER");
myButton3=new Button("GET AREA");
myButton4=new Button("GET RADIUS");
myButton5=new Button("GET CIRCUMFERENCE");
myFrame.add(myButton2);
myFrame.add(myButton3);
myFrame.add(myButton4);
myFrame.add(myButton5);
my complete code:
MAIN CLASS:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class FrameDemo implements WindowListener, ActionListener{
static Line l=new Line();
private Frame myFrame;
private Label myLabel,myLabel2,myLabel3,myLabel4,myLabel5;
private TextField myTextField,myTextField2,myTextField3,myTextField4,myTextField5;
private Button myButton;
public FrameDemo()throws NumberFormatException{
Scanner input=new Scanner(System.in);
myFrame=new Frame("My Frame");
myLabel=new Label("P1 X --->");
myLabel2=new Label("P1 Y --->");
myLabel3=new Label("P2 X --->");
myLabel4=new Label("P2 Y --->");
myLabel5=new Label("The Distance: ");
myTextField=new TextField();
myTextField2=new TextField();
myTextField3=new TextField();
myTextField4=new TextField();
myTextField5=new TextField();
myButton=new Button("GET DISTANCE");
myLabel.setBackground(Color.BLACK);
myLabel.setForeground(Color.WHITE);
myLabel2.setBackground(Color.BLACK);
myLabel2.setForeground(Color.WHITE);
myLabel3.setBackground(Color.BLACK);
myLabel3.setForeground(Color.WHITE);
myLabel4.setBackground(Color.BLACK);
myLabel4.setForeground(Color.WHITE);
myLabel5.setBackground(Color.BLACK);
myLabel5.setForeground(Color.WHITE);
myLabel.setFont(new Font("Verdana", Font.BOLD, 15));
myLabel2.setFont(new Font("Verdana", Font.BOLD, 15));
myLabel3.setFont(new Font("Verdana", Font.BOLD, 15));
myLabel4.setFont(new Font("Verdana", Font.BOLD, 15));
myLabel5.setFont(new Font("Verdana", Font.BOLD, 15));
myButton.setBackground(Color.BLACK);
myButton.setForeground(Color.WHITE);
myFrame.setSize(450, 100);
myFrame.setLayout(new GridLayout(3,4));
myFrame.add(myLabel);
myFrame.add(myTextField);
myFrame.add(myLabel2);
myFrame.add(myTextField2);
myFrame.add(myLabel3);
myFrame.add(myTextField3);
myFrame.add(myLabel4);
myFrame.add(myTextField4);
myFrame.add(myButton);
myFrame.add(myTextField5);
myFrame.addWindowListener(this);
myButton.addActionListener(this);
myFrame.show();
}
public static void main(String[] args){
FrameDemo f=new FrameDemo();
}
public void actionPerformed(ActionEvent e){
String x1,y1,x2,y2,dis2;
double dis;
x1=myTextField.getText(); y1=myTextField2.getText();
x2=myTextField3.getText(); y2=myTextField4.getText();
l.setP1(new Point(Integer.parseInt(x1),Integer.parseInt(y1)));
l.setP2(new Point(Integer.parseInt(x2),Integer.parseInt(y2)));
dis=l.getRadius();
dis2=Double.toString(dis);
myTextField5.setText(dis2);
}
public void windowDeactivated(WindowEvent e){
}
public void windowActivated(WindowEvent e){
}
public void windowDeiconified(WindowEvent e){
}
public void windowIconified(WindowEvent e){
}
public void windowClosed(WindowEvent e){
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowOpened(WindowEvent e){
}
}
LINE CLASS:
public class Line
{
public Point p1;
public Point p2;
public Line()
{
p1=new Point();
p2=new Point();
}
public Line(Point p1,Point p2)
{
this.p1=p1;
this.p2=p2;
}
public void setP1(Point p1)
{
this.p1=p1;
}
public void setP2(Point p2)
{
this.p2=p2;
}
public Point getP1()
{
return this.p1;
}
public Point getP2()
{
return this.p2;
}
public double getRadius()
{
double len=0;
if(p1.getX()==p2.getX())
len=Math.abs(p2.getY()-p1.getY());
else if(p1.getY()==p2.getY())
len=Math.abs(p2.getX()-p1.getX());
else
len=Math.sqrt((Math.abs(p2.getX()-p1.getX())*Math.abs(p2.getX()-p1.getX()))+(Math.abs(p2.getY()-p1.getY())*Math.abs(p2.getY()-p1.getY())));
return len;
}
public double getDiameter()
{
return getRadius()*2;
}
public double getArea()
{
return Math.PI*getRadius()*getRadius();
}
public double getCircumference()
{
return 2*Math.PI*getRadius();
}
}
POINT CLASS:
public class Point
{
public int x;
public int y;
public Point()
{
this.x=0;
this.y=0;
}
public Point(int x,int y)
{
this.x=x;
this.y=y;
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}
THERE ARE NO ERRORS WITH THIS PROGRAM, IM JUST HAVING PROBLEMS IN PROVIDING
ActionListeners to my additional buttons.
any tips or advices are gladly accepted
Thanks in advance.