Reputation: 3
i need a little help with introducing a method or something, that will close the desktop application after i press the default X button in the corner. I struggled with a lot of methods i found online, but i either don't know where to put it or i'm missing something...
Here's my code:
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
public class Calculator implements ActionListener {
Frame frame = new Frame();
Label label1 = new Label("First Number");
Label label2 = new Label("Operator");
Label label3 = new Label("Second Number");
Label label4 = new Label("Result");
TextField text1 = new TextField();
Choice choice = new Choice();
TextField text2 = new TextField();
TextField text3 = new TextField();
Button button = new Button("Calculate");
Calculator() {
label1.setBounds(50, 100, 100, 20);
label2.setBounds(50, 140, 100, 20);
label3.setBounds(50, 180, 100, 20);
label4.setBounds(50, 220, 100, 20);
text1.setBounds(200, 100, 100, 20);
choice.setBounds(200, 140, 100, 20);
text2.setBounds(200, 180, 100, 20);
text3.setBounds(200, 220, 100, 20);
button.setBounds(200, 200, 100, 20);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.add(label4);
frame.add(text1);
frame.add(choice);
frame.add(text2);
frame.add(text3);
frame.add(button);
button.addActionListener(this);
choice.add("+");
choice.add("-");
choice.add("*");
choice.add("/");
frame.setLayout(null);
frame.setVisible(true);
frame.setSize(400, 350);
}
public void actionPerformed(ActionEvent event) {
try {
double number1 = Double.parseDouble(text1.getText());
double number2 = Double.parseDouble(text2.getText());
String selectedOperation = choice.getSelectedItem();
if (selectedOperation.equals("+"))
text3.setText(String.valueOf(number1 + number2));
else if (selectedOperation.equals("-"))
text3.setText(String.valueOf(number1 - number2));
else if (selectedOperation.equals("*"))
text3.setText(String.valueOf(number1 * number2));
else if (selectedOperation.equals("/")) {
if (number2 != 0)
text3.setText(String.valueOf(number1 / number2));
else System.out.println("Can't do that...");
}
} catch (NumberFormatException ignore) {
System.out.println("You can only introduce numbers!");
}
}
}
public class Main {
public static void main(String[] args) {
new Calculator();
}
}
Some help would be very appreciated, as i'm trying to learn Java!
Upvotes: 0
Views: 406
Reputation: 151
Not quite sure why you are using AWT rather than Swing. As the latter tends to be recommended over the former.
But nevertheless - you can make the application exit by adding a window listener. E.g.:
frame.addWindowListener(new WindowAdapter()
{
@Override public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
As a side note, if you were using Swing rather than AWT (i.e. a JFrame
rather than Frame
) you can also do :
JFrame jframe = ....;
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Upvotes: 1
Reputation: 109613
No one uses the AWT GUI anymore. Use Swing, JFrame, JButton, JLabel, JTextField. And then JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE)
.
Upvotes: 0