import java.awt.GridLayout; import java.awt.event.MouseEvent; import javax.swing.GroupLayout; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import java.awt.event.MouseAdapter; import javax.swing.JTextField; /** * Displays the grafical interface for converting Fahrenheit to Celsius and back again. * @author Rainier */ public class ConversionFrame extends JFrame { GroupLayout layout; JLabel jLblFarenheid = new JLabel("Farenheid"); JLabel jLblCelsius = new JLabel("Celcius"); JLabel jLblMessage = new JLabel(); JTextField jTxtFarenheid = new JTextField(); JTextField jTxtCelsius = new JTextField(); JButton jBtnConvert = new JButton(); /** Creates a new instance of ConversionFrame */ public ConversionFrame() { //Set the label of the frame. super("Conversion application"); this.setSize(600,180); //When pressing the "X" unload the application. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); //We will place the labels textboxes and button on the right position of the frame. setFrameLayout(); //Hook up a Mouse click event handler to the ConvertButton jBtnConvert.addMouseListener (new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent evt) { jBtnConvertClicked(evt); } } ); } /** * Event handler for when the Convert button gets clicked. */ private void jBtnConvertClicked(MouseEvent evt) { String celsius = jTxtCelsius.getText(); String farenheid = jTxtFarenheid.getText(); FarenhetiCelsiusAdapter adapter = new FarenhetiCelsiusAdapter(); //Make sure we do not have any spaces in the textbox. celsius = celsius.trim(); farenheid = farenheid.trim(); //In case none of the textboxes are filled. Display the text You are required to fill one of the textboxes for the conversion. if(celsius.length() ==0 && farenheid.length() ==0) { jLblMessage.setText("You are required to fill one of the textboxes for the conversion."); jLblMessage.setVisible(true); } if(celsius.length() !=0 && farenheid.length()!=0) { jLblMessage.setText("Both text boxes are filled, please fill in just one the application will calculate the value of the other."); jLblMessage.setVisible(true); } try { //If the celsius textbox is filled and the farenheid textbox is not filled. //fill the adapter with the celsius value and retrieve the farenheid value. if(celsius.trim().length()!=0 && farenheid.trim().length()==0) { adapter.setCelsius(Double.parseDouble( celsius )); jTxtFarenheid.setText( String.valueOf( adapter.getFarenheid() )); } //If the Farenheid value is filled and the celsius not. //fill the adapter with the farenheid value and retrieve the celsius value. if(celsius.trim().length()==0 && farenheid.trim().length()!=0) { adapter.setFarenheid(Double.parseDouble( farenheid )); jTxtCelsius.setText( String.valueOf( adapter.getCelsius() ) ); } } catch(NumberFormatException ex) { jLblMessage.setText("Please enter only numeric values into the text boxes."); } } /** * Places the controls on the right position of the form. */ private void setFrameLayout() { //We want to make GroupLayout on the default contentpane ofthe frame. GroupLayout layout = new GroupLayout(getContentPane()); setLayout(layout); jBtnConvert.setText("Convert"); // Turn on automatically adding gaps between components layout.setAutoCreateGaps(true); // Turn on automatically creating gaps between components that touch // the edge of the container and the container. layout.setAutoCreateContainerGaps(true); // Create a sequential group for the horizontal axis. // I look at this as defining what will be placed in which column of a table // (I can view it as a table because I used the auto create gaps // if you want to define the position yourself remove the autogaps and add gaps just as you would add components.) // In this case I will add the label for Celsius and the label for Farenheid to the first column. // I will add GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); hGroup.addGroup( layout.createParallelGroup().addComponent(jLblCelsius).addComponent(jLblFarenheid)); hGroup.addGroup( layout.createParallelGroup().addComponent(jTxtCelsius).addComponent(jTxtFarenheid).addComponent(jBtnConvert).addComponent(jLblMessage)); // Create a sequential group for the vertical axis. // Same Idea as the horizontal axis but here we are saying what will be in which "row" of the "table"" GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup(); vGroup.addGroup( layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLblCelsius).addComponent(jTxtCelsius)); vGroup.addGroup( layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLblFarenheid).addComponent(jTxtFarenheid)); vGroup.addGroup( layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jBtnConvert)); vGroup.addGroup( layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLblMessage)); // Now we set the horizontal and the vertical groups which we defined above. // In effect we have defined for each component where the component will be placed on the horizontal axis and where it will be placed on the vertical axis. layout.setHorizontalGroup(hGroup); layout.setVerticalGroup(vGroup); } }