106 lines
3.4 KiB
Java
106 lines
3.4 KiB
Java
package Calculator;
|
|
|
|
import java.awt.Button;
|
|
import java.awt.Dialog;
|
|
import java.awt.FlowLayout;
|
|
import java.awt.Frame;
|
|
import java.awt.GridBagLayout;
|
|
import java.awt.GridLayout;
|
|
import java.awt.Label;
|
|
import java.awt.Panel;
|
|
import java.awt.TextField;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.WindowAdapter;
|
|
import java.awt.event.WindowEvent;
|
|
|
|
public class MyFrame extends Frame {
|
|
public MyFrame() {
|
|
super("Calculator");
|
|
addWindowListener(new WindowAdapter() {
|
|
@Override
|
|
public void windowClosing(WindowEvent e) {
|
|
// TODO Auto-generated method stub
|
|
System.exit(0);
|
|
}
|
|
});
|
|
setVisible(true);
|
|
setBounds(400, 400, 300, 200);
|
|
TextField num1 = new TextField();
|
|
Label addKey = new Label("+");
|
|
TextField num2 = new TextField();
|
|
Button equal2 = new Button("=");
|
|
TextField goal = new TextField();
|
|
setLayout(new FlowLayout());
|
|
setResizable(false);
|
|
add(num1);
|
|
add(addKey);
|
|
add(num2);
|
|
add(equal2);
|
|
add(goal);
|
|
equal2.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
try {
|
|
goal.setText(add(Double.valueOf(num1.getText()), Double.valueOf(num2.getText())));
|
|
try {
|
|
goal.setText(add(Integer.valueOf(num1.getText()), Integer.valueOf(num2.getText())));
|
|
} catch (java.lang.NumberFormatException event) {
|
|
// TODO: handle exception
|
|
}
|
|
} catch (java.lang.NumberFormatException event) {
|
|
// TODO: handle exception
|
|
CreateWarning(num1, num2, goal);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public void CreateWarning(TextField num1, TextField num2, TextField goal) {
|
|
Dialog warning = new Dialog(this, "warning!");
|
|
warning.setLayout(new GridLayout(2, 1));
|
|
warning.setSize(400, 200);
|
|
warning.setVisible(true);
|
|
warning.addWindowListener(new WindowAdapter() {
|
|
@Override
|
|
public void windowClosing(WindowEvent e) {
|
|
// TODO Auto-generated method stub
|
|
warning.dispose();
|
|
num1.setText("");
|
|
num2.setText("");
|
|
goal.setText("");
|
|
}
|
|
});
|
|
|
|
Panel Message = new Panel(new FlowLayout());
|
|
Panel CloseButton = new Panel(new FlowLayout());
|
|
|
|
Button closeButton = new Button("close");
|
|
Label message = new Label("please enter a valid number!");
|
|
|
|
closeButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
// TODO Auto-generated method stub
|
|
warning.dispose();
|
|
num1.setText("");
|
|
num2.setText("");
|
|
goal.setText("");
|
|
}
|
|
});
|
|
|
|
Message.add(message);
|
|
CloseButton.add(closeButton);
|
|
warning.add(Message);
|
|
warning.add(CloseButton);
|
|
}
|
|
|
|
public String add(double num1, double num2) {
|
|
return String.valueOf(num1 + num2);
|
|
}
|
|
|
|
public String add(int num1, int num2) {
|
|
return String.valueOf(num1 + num2);
|
|
}
|
|
}
|