Skip to content

Instantly share code, notes, and snippets.

@jermenkoo
Created April 16, 2014 12:48
Show Gist options
  • Save jermenkoo/10869020 to your computer and use it in GitHub Desktop.
Save jermenkoo/10869020 to your computer and use it in GitHub Desktop.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyException extends JFrame implements ActionListener {
JButton calculate;
JTextField input;
JLabel output;
public MyException() {
this.setSize(250, 350);
this.setLayout(null);
JLabel val = new JLabel("value >= 0");
val.setBounds(20, 50, 100, 50);
this.getContentPane().add(val);
input = new JTextField("0");
input.setBounds(130, 50, 100, 50);
this.getContentPane().add(input);
val = new JLabel("Square root");
val.setBounds(20, 150, 100, 50);
this.getContentPane().add(val);
output = new JLabel("");
output.setBounds(130, 150, 100, 50);
this.getContentPane().add(output);
calculate = new JButton("Calculate");
calculate.setBounds(80, 250, 100, 50);
this.getContentPane().add(calculate);
calculate.addActionListener(this);
}
public static void main(String[] args) {
new MyException().setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == calculate) {
try {
double value = Double.parseDouble(input.getText().trim());
output.setText("" + Math.sqrt(value));
} catch (NumberFormatException _) {
JOptionPane.showMessageDialog(this, "NOT A NUMBER!", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment