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.

Edited 6 Years Ago by intes77: n/a

To create action listener object.
Define a class that implements ActionListener, create a new instance of the class and add it as a listener to the button.

Do a Search for ActionListener or addActionListener( on this forum for code samples.

You can do this in a more compact (but maybe less readable?) way by using an anonymous inner class, eg

myButton.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
    // do whatever
  }
});

how can i make 4 different action listeners with these 4 buttons?

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 idea is that i would make ActionListeners for the 4 buttons

myButton2.addActionListener(this);
myButton3.addActionListener(this);
myButton4.addActionListener(this);
myButton5.addActionListener(this);

now my problem is how can i make their 4 different actionPerformed methods
because the standard code is always like this:

public void actionPerformed(ActionEvent e)
{
//button events will go here
}

any help please, im confused

Edited 6 Years Ago by intes77: n/a

Use the solution I gave you in my previous post - and have a different anonymous inner class, each with its own different actionPeformed, for each button:

myButton1.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
    // do whatever you want when button 1 is pressed
  }
});

myButton2.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
    // do whatever you want when button 2 is pressed
  }
});

// etc

oh i got ur point. so in adding another action listener.
you made an inner class for it.

just curious. is this "@Override" thing a part of the code?

It's called an annotation - they were new in java 1.5 (ish). This one says that the following method is intended to override a method in the superclass (or implement a method in a declared interface). It allows the compiler to check that you are overriding as you intended - before annotations, if you spelled the name of a method wrong when you tried to override, you wouldn't get an error message, but the code wouldn't do what you expected.
Now you know what they are called you can google for a load more examples.

Earlier versions of the compiler did NOT have that option. When you attempted to override a method in a class, if you misspelled the method name or got the args wrong, the compiler was very happy to add the NEW method to the existing methods for the class instead of overriding the method you intended to override. Then your code didn't work as desired and it was a hard bug to find.

So after many complaints, the compiler writers added a feature that allows us programmers to have the compiler check that the method we've coded does override one of the methods of the class.

@Override is also v useful when you're reading other people's code. Without it there's no way to see that a method is overriding another without checking every method in every superclass by hand.

dude thanks a lot!!! here's my final main class

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

public class FrameDemo implements WindowListener{
static Line l=new Line();
	private Frame myFrame;
	private Label myLabel,myLabel2,myLabel3,myLabel4,myLabel5;
	private TextField myTextField,myTextField2,myTextField3,myTextField4,myTextField5,myTextField6,myTextField7,myTextField8;
	private Button myButton,myButton1,myButton2,myButton3;


	public FrameDemo()throws NumberFormatException{

		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();
		myTextField6=new TextField();
		myTextField7=new TextField();
		myTextField8=new TextField();

		myButton=new Button("GET RADIUS");
		myButton1=new Button("GET DIAMETER");
		myButton2=new Button("GET AREA");
		myButton3=new Button("GET CIRCUMFERENCE");

		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);
		myButton1.setBackground(Color.BLACK);
		myButton1.setForeground(Color.WHITE);
		myButton2.setBackground(Color.BLACK);
		myButton2.setForeground(Color.WHITE);
		myButton3.setBackground(Color.BLACK);
		myButton3.setForeground(Color.WHITE);
		myFrame.setSize(570, 140);
		myFrame.setLayout(new GridLayout(4,3));

		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.add(myButton1);
		myFrame.add(myTextField6);
		myFrame.add(myButton2);
		myFrame.add(myTextField7);
		myFrame.add(myButton3);
		myFrame.add(myTextField8);
		myFrame.addWindowListener(this);
		myButton.addActionListener(
		new ActionListener()
		{
		  @Override
		  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);
		  }
		});

				myButton1.addActionListener(
				new ActionListener()
				{
				  @Override
				  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.getDiameter();
				        dis2=Double.toString(dis);
						myTextField6.setText(dis2);
				  }
		});

				myButton2.addActionListener(
				new ActionListener()
				{
				  @Override
				  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.getArea();
				        dis2=Double.toString(dis);
						myTextField7.setText(dis2);
				  }
		});

				myButton3.addActionListener(
				new ActionListener()
				{
				  @Override
				  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.getCircumference();
				        dis2=Double.toString(dis);
						myTextField8.setText(dis2);
				  }
		});
		myFrame.show();
	}

	public static void main(String[] args){
		FrameDemo f=new FrameDemo();
	}

	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){
	}
}

i was able to add 3 more buttons thanks a lot dude!!!

One small observation: when I see something like

myTextField,myTextField2,myTextField3,myTextField4,myTextField5,myTextField6,myTextField7,myTextField8;

I wonder why not have an array

TextField[] myTextFields = new TextField[8];

and reduce all those horrible repeated lines by the much more readable

for (int i = 0; i< myTextFields.length; i++) {
  myTextFields[i] = new TextField();
  myTextFields[i].setForeground(Color.WHITE);
  myTextFields[i].setBackground(Color.BLACK);
  myTextFields[i].setFont(new Font("Verdana", Font.BOLD, 15));
  myFrame.add(myTextFields[i]);
}

ps:
I notice you are using the old AWT graphical objects. Unless you hace some specific reason not to, you should be using the newer Swing objects (JFrame, JButton, JLabel etc).

This question has already been answered. Start a new discussion instead.