I don't know how to write code to exit for this program.someone please help me :(

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Panel extends JPanel implements ActionListener{

	private JTextField height, weight, answer;
	private JButton calc, clear, exit;

	public Panel(){
		this.setLayout(new GridLayout(2,1));
		JPanel topPanel = new JPanel();
		topPanel.add(new JLabel("WEIGHT(KG)"));
		topPanel.add(height = new JTextField(5));
		topPanel.add(new JLabel("HEIGHT(M)"));
		topPanel.add(weight = new JTextField(5));
		topPanel.add(new JLabel("BMI"));
		topPanel.add(answer =new JTextField(10));
		JPanel bottomPanel = new JPanel();
		bottomPanel.add(calc = new JButton("CALCULATE"));
		bottomPanel.add(clear = new JButton("CLEAR"));
		bottomPanel.add(exit = new JButton("EXIT"));
		
		this.add(topPanel);
		this.add(bottomPanel);
		calc.addActionListener(this);
		clear.addActionListener(this);
		exit.addActionListener(this);
		answer.setEditable(false);
}

public void actionPerformed(ActionEvent e)
{
	double w =Double.parseDouble(weight.getText());
	double h =Double.parseDouble(height.getText());
	double ans=0;
	if(e.getSource()==calc)
	{
		ans = (w/(h*h));
	}
	else if(e.getSource()==clear)
	{
		height.setText("");
		weight.setText("");
		
	}
	else if(e.getSource()==exit) ////what i must write to make program exit?
	{ 


	}
	answer.setText(""+ans);
	
	if(ans>0&&ans<18.5)
	{
		answer.setBackground(Color.YELLOW);
	}
	else if(ans>18.5&&ans<25.0)
	{
		answer.setBackground(Color.GREEN);
	}
	else if(ans>=25.0)
	{
		answer.setBackground(Color.RED);
	}
}
}

Recommended Answers

All 2 Replies

how to write code to exit for this program

Code this where you want to exit:
System.exit(0);

If you just want to exit from a method, use the return statement.

tq..i get it :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.