Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Exit from frame using button

  Asked By: Bastet    Date: Aug 07    Category: Java    Views: 1655
  

Please help me how i can exit from a frame (instead of closing by CTRL+c).

Codes below is being presented in full. its comprise of many lines

================


import java.awt.*;
import java.awt.event.*;


public class Testt

{
public static void main(String[]args) throws NoSuchMethodError
{

Test1 test = new Test1();
}
}



class Test1 implements ActionListener
{

TextField text1, text2, text3;
Label label1,label2;
Button button1, button2,button3,button4,button5,button6;
Frame frame;
MenuBar menubar;
MenuItem item1;
FlowLayout flow;
Menu menu;


public Test1()
{


frame = new Frame("Have a nice day");

frame.setBounds(0,0,500,400);

menubar = new MenuBar();
frame.setMenuBar(menubar);

menu = new Menu("First Menu");
menubar.add(menu);

item1 = new MenuItem("Open");
menu.add(item1);

flow = new FlowLayout();
frame.setLayout(flow);

text1 = new TextField(12);
text2 = new TextField(12);
text3 = new TextField(12);

label1 = new Label(" and ");
label2 = new Label("=");

frame.add(text1);
frame.add(label1);
frame.add(text2);
frame.add(label2);
frame.add(text3);

button1 = new Button(" X ");
button2 = new Button(" / ");
button3 = new Button(" + ");
button4 = new Button(" - ");
button5 = new Button("Clear");
button6 = new Button("Exit");

frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);

frame.setVisible(true);

button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);


}

public void actionPerformed(ActionEvent ee)
{

String msg = new String("Hullo from java");

// Multiply

if(ee.getSource() == button1)
{
Double double1 = new Double(text1.getText());
Double double2 = new Double(text2.getText());

double dd = double1.doubleValue() * double2.doubleValue();

text3.setText(String.valueOf(dd));
}

// Divide

if(ee.getSource() == button2)
{
Double double1 = new Double(text1.getText());
Double double2 = new Double(text2.getText());

double dd = double1.doubleValue() / double2.doubleValue();

text3.setText(String.valueOf(dd));
}

// Add

if(ee.getSource() == button3)
{
Double double1 = new Double(text1.getText());
Double double2 = new Double(text2.getText());

double dd = double1.doubleValue() + double2.doubleValue();

text3.setText(String.valueOf(dd));
}

// Subtract

if(ee.getSource() == button4)
{
Double double1 = new Double(text1.getText());
Double double2 = new Double(text2.getText());

double dd = double1.doubleValue() - double2.doubleValue();

text3.setText(String.valueOf(dd));
}



if(ee.getSource() == button5)
{

text1.setText(null);
text2.setText(null);
text3.setText(null);

}

}

}

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Rhys Evans     Answered On: Aug 07

I have modified ur code to do so....


import java.awt.*;
import java.awt.event.*;


public class  Testt

{
public static  void main(String[]args) throws
NoSuchMethodError
{

Test1 test  = new Test1();
}
}



class Test1 implements ActionListener
{

TextField text1, text2, text3;
Label label1,label2;
Button button1,
button2,button3,button4,button5,button6;
Frame frame;
MenuBar menubar;
MenuItem item1;
FlowLayout flow;
Menu menu;


public Test1()
{


frame = new Frame("Have a nice day");

frame.setBounds(0,0,500,400);

menubar = new MenuBar();
frame.setMenuBar(menubar);

menu = new Menu("First Menu");
menubar.add(menu);

item1 = new MenuItem("Open");
menu.add(item1);

flow = new FlowLayout();
frame.setLayout(flow);

text1 = new TextField(12);
text2 = new TextField(12);
text3 = new TextField(12);

label1 = new Label(" and ");
label2 = new Label("=");

frame.add(text1);
frame.add(label1);
frame.add(text2);
frame.add(label2);
frame.add(text3);

button1 = new Button(" X ");
button2 = new Button(" / ");
button3 = new Button(" + ");
button4 = new Button(" - ");
button5 = new Button("Clear");
button6 = new Button("Exit");

frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);

frame.setVisible(true);

button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);

}

public void  actionPerformed(ActionEvent ee)
{

String msg = new String("Hullo from java");

// Multiply

if(ee.getSource() == button1)
{
Double double1 = new Double(text1.getText());
Double double2 = new Double(text2.getText());

double dd = double1.doubleValue() *
double2.doubleValue();

text3.setText(String.valueOf(dd));
}

// Divide

if(ee.getSource() == button2)
{
Double double1 = new Double(text1.getText());
Double double2 = new Double(text2.getText());

double dd = double1.doubleValue() /
double2.doubleValue();

text3.setText(String.valueOf(dd));
}

// Add

if(ee.getSource() == button3)
{
Double double1 = new Double(text1.getText());
Double double2 = new Double(text2.getText());

double dd = double1.doubleValue() +
double2.doubleValue();

text3.setText(String.valueOf(dd));
}

// Subtract

if(ee.getSource() == button4)
{
Double double1 = new Double(text1.getText());
Double double2 = new Double(text2.getText());

double dd = double1.doubleValue() -
double2.doubleValue();

text3.setText(String.valueOf(dd));
}



if(ee.getSource() == button5)
{
text1.setText(null);
text2.setText(null);
text3.setText(null);
}

if(ee.getSource() == button6)
{
System.exit(0);
}


}

}

 
Answer #2    Answered By: Mildred Bailey     Answered On: Aug 07

I applied this code ........... System.exit(0) .............
but it doesnt work and not giving any runtime error too.

 
Answer #3    Answered By: Lee Butler     Answered On: Aug 07

When u click on the exit  button the application will
terminate...

 
Answer #4    Answered By: Jennifer Davis     Answered On: Aug 07

All you need to do is to implement an ActionListener(just add the words
"implements ActionListener" in your class  name declaration) , add it to your
frame(mostly inside its constructor) as shown below:
public YourClass(){
your code......
..........
addActionListener(this); //adding action listener to the object
being constructed
}
Then you have to implement the listeners method . This is must or ur
class will never compile
public void  actionPerformed(ActionEvent ae){
//use the following method if u have many buttons
if (ae.getSource() == YourButtonobject) {
System.exit(0);
}
}

 
Didn't find what you were looking for? Find more on Exit from frame using button Or get search suggestion and latest updates.




Tagged: