I already created the GUI using the netbeans drag-and-drop style GUI generator,
ID: 3914079 • Letter: I
Question
I already created the GUI using the netbeans drag-and-drop style GUI generator, althoug, I dont understand how to implement the code with the pre-existing automatically generated code from the GUI generator in order to make this work.
Temperature Converter GUI Write a GUI program, using the NetBeans GUI generator, that will convert temperatures from fahrenheit to celsius and from celsius to fahrenheit. The user should be able to enter a temperature to convert, click on a radio button to specify which conversion to do, and then click on a button to see the converted temperature displayed. Use the following formulas for the temperature conversions: F-9/5*C+32 and ? 5/9*(F-32) where F is the fahrenheit temperature and C is the celsius temperature. The temperatures should be displayed to 1 decimal place, e.g. 93.7 degrees Fahrenheit. Note: Submit the assignment as a zip file of your NetBeans project folder. Zip the entire folder not just the java files. Temperature Converter Temperature Converter Choose Conversion Type: Fahrenheit to Celsius O Celsius to Fahrenheit Enter the temperature to convert 25 The temperature converts to: 77.0 degrees Fahrenheit Convert TemperatureExplanation / Answer
Source Code:
CalculaterForm.java
import javax.swing.ButtonGroup;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class CalculaterForm extends javax.swing.JFrame {
/**
* Creates new form CalculaterForm
*/
public CalculaterForm() {
initComponents();
ButtonGroup r_bt=new ButtonGroup();
r_bt.add(f_c);
r_bt.add(c_f);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
f_c = new javax.swing.JRadioButton();
c_f = new javax.swing.JRadioButton();
jLabel3 = new javax.swing.JLabel();
temperature = new javax.swing.JTextField();
answer = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
convert_temp = new javax.swing.JButton();
answer1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(444, 300));
jLabel1.setFont(new java.awt.Font("Verdana", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 51, 51));
jLabel1.setText("Temperature Converter");
jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel2.setText("Choose Convertion Type:");
f_c.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
f_c.setText("Fahrenheit to Celsius");
c_f.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
c_f.setText("Celsius to Fahrenheit");
jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel3.setText("Enter the temperature to convert:");
answer.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
answer.setText("The temperature converts to:");
jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
convert_temp.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
convert_temp.setText("Convert Temperatures");
convert_temp.setToolTipText("convert");
convert_temp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convert_tempActionPerformed(evt);
}
});
answer1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(131, 131, 131)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(157, 157, 157)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(c_f, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(f_c, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(150, 150, 150)
.addComponent(convert_temp))
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(answer, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(answer1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(temperature, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(154, 154, 154)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(f_c)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(c_f)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(temperature, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(answer)
.addComponent(jLabel5)
.addComponent(answer1))
.addGap(18, 18, Short.MAX_VALUE)
.addComponent(convert_temp)
.addGap(35, 35, 35))
);
pack();
}// </editor-fold>
private void convert_tempActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
double temp1 =Double.parseDouble(this.temperature.getText());
if(f_c.isSelected() && !c_f.isSelected()){
answer.setText(String.format("%s %.1f %s", "The temperature converts to: ", ((temp1-32)*5/9), "degrees Celcieus"));
}
else if(c_f.isSelected() && !f_c.isSelected()){
answer.setText(String.format("%s %.1f %s","The temperature converts to: ", ((temp1*9/5)+32), "degrees Fahrenhiet"));
}
else{
answer.setText("Can't Select Both or None");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CalculaterForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CalculaterForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CalculaterForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CalculaterForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CalculaterForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel answer;
private javax.swing.JLabel answer1;
private javax.swing.JRadioButton c_f;
private javax.swing.JButton convert_temp;
private javax.swing.JRadioButton f_c;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField temperature;
// End of variables declaration
}
===================================================================================
Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
CalculaterForm form=new CalculaterForm();
form.setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.