import javax.swing.JFrame; import javax.swing.*; import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ScientificNotation extends JFrame { private JTextField field; public ScientificNotation() { setTitle("Scientific Notation"); setVisible(true); field = new JTextField(25); setLayout(new GridLayout(2,2)); add(field); field.setVisible(true); Container pane = getContentPane(); field.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String number = field.getText(); String newString = ""; int digits = 0; if (number.charAt(0) == '0' && number.charAt(1) == '.') { for (int i =2; i < number.length(); i++) { while (number.charAt(i) == '0') { digits--; } newString = newString + String.valueOf(number.charAt(i)); } JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE); } else { for (int i = 0; i < number.length(); i++) { while(number.charAt(i) != '.') { digits++; if (i ==0) { newString = newString + String.valueOf(number.charAt(i)); } else if (i==1) { newString = newString + "."; } else { newString = newString + String.valueOf(number.charAt(i)); } } newString = newString + String.valueOf(number.charAt(i)); } JOptionPane.showMessageDialog(null, newString + "E" + digits, "Scientific Notation is:", JOptionPane.INFORMATION_MESSAGE); } }}); //setContentPane(pane); } public static void main(String[] args) { ScientificNotation sn = new ScientificNotation(); } }
How do you get the JTextField to be "normal" sized?
Also, why is my window locking up and the JOptionPane never appearing?