Hi Ian,
Try the following code,i have made few changes to your code.
1)Added exception handling
2)Changed the design
3)Formatted the double value.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class SquareRoots extends JFrame{
private JPanel textPanel,buttonPanel, basePanel;
private JTextField textBox, textAnswer;
private JButton btnCalculate, btnExit;
public final static Dimension hpad20 = new Dimension(20,1);
public final static Dimension vpad15 = new Dimension(1,15);
public final static Dimension hpad10 = new Dimension(10,1);
public final static Dimension vpad10 = new Dimension(1, 10);
public SquareRoots(String titleText){
super (titleText);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
SquareRoots.this.dispose();
System.exit(0);
}
});
// ******************************************************
// ******************** Text Panel*********************
// ******************************************************
textBox=new JTextField(10){
public Dimension getMaximumSize() {
return new Dimension(getPreferredSize().width, getPreferredSize().height);
}
};
textAnswer=new JTextField(10){
public Dimension getMaximumSize() {
return new Dimension(getPreferredSize().width, getPreferredSize().height);
}
};
textPanel= new JPanel();
textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));
textPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Find SquareRoot"));
JPanel wrapper1 =new JPanel();
wrapper1.setLayout(new GridLayout(2,2,5,10));
wrapper1.add(new JLabel("Enter Number"));
wrapper1.add(textBox);
wrapper1.add(new JLabel("Result"));
wrapper1.add(textAnswer);
JPanel wrapper2 =new JPanel();
wrapper2.setLayout(new BoxLayout(wrapper2, BoxLayout.Y_AXIS));
wrapper2.add(Box.createRigidArea(vpad15));
wrapper2.add(wrapper1);
wrapper2.add(Box.createRigidArea(vpad15));
textPanel.add(Box.createRigidArea(hpad20));
textPanel.add(wrapper2);
textPanel.add(Box.createRigidArea(hpad20));
// ******************************************************
// ******************** Button Panel*********************
// ******************************************************
btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ButtonListener());
btnExit = new JButton("Exit");
btnExit.addActionListener(new btnExitListener());
buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
JPanel butWrapper =new JPanel();
butWrapper.setLayout(new BoxLayout(butWrapper, BoxLayout.X_AXIS));
butWrapper.add(Box.createRigidArea(hpad20));
butWrapper.add(btnCalculate);
butWrapper.add(Box.createRigidArea(hpad10));
butWrapper.add(btnExit);
butWrapper.add(Box.createRigidArea(hpad20));
buttonPanel.add(Box.createRigidArea(vpad10));
buttonPanel.add(butWrapper);
buttonPanel.add(Box.createRigidArea(vpad15));
// ******************************************************
// ******************** base Panel*********************
// ******************************************************
basePanel= new JPanel();
basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.X_AXIS));
JPanel childPanel =new JPanel();
childPanel.setLayout(new BoxLayout(childPanel, BoxLayout.Y_AXIS));
childPanel.add(Box.createRigidArea(vpad15));
childPanel.add(Box.createRigidArea(vpad15));
childPanel.add(textPanel);
childPanel.add(buttonPanel);
basePanel.add(Box.createRigidArea(hpad20));
basePanel.add(Box.createRigidArea(hpad20));
basePanel.add(childPanel);
basePanel.add(Box.createRigidArea(hpad20));
basePanel.add(Box.createRigidArea(hpad20));
Container cp = getContentPane();
cp.add("Center",basePanel);
this.setSize(500,500);
this.setResizable(false);
this.pack();
this.setVisible(true);
}
public double calcSquare(double x){
return Math.sqrt(x);
}
class btnExitListener implements ActionListener{
public btnExitListener(){}
public void actionPerformed(ActionEvent e){
SquareRoots.this.dispose();
System.exit(0);
}
}
class ButtonListener implements ActionListener{
/* Use the constructor to set up any necessary constants or variables only.
Don't reference any particular events with it (e.g. clicking the button).
These should be done in the correct method */
public ButtonListener() { }
public void actionPerformed(ActionEvent e) {
/*
Test to see if the source of the action event is really the textbox
(i.e. carriage return was pressed) */
if(e.getSource() == btnCalculate) {
try{
String y = textBox.getText();
double d = Double.parseDouble(y);
DecimalFormat z =new DecimalFormat("####.00");
textAnswer.setText(z.format(calcSquare(d)));
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(null,"Invalid Input. "+e1.getMessage(),"Input Error", JOptionPane.ERROR_MESSAGE);
textBox.setText("");
textAnswer.setText("");
}
}
}
}
public static void main(String[] args){
new SquareRoots("Square Roots");
}
}