This a gui program of a car wash. consists of two class demo and total that would display total in a new window after calculations. everything was working fine, dicided to run it one more time for screenshot to sumbit but it stucks, like program is running but window freezes. I am using net bean drag and drop method. enter image description here demo class
package car.wash_05;
public class demo extends javax.swing.JFrame {
private double small_Engine = 100.0;
private double big_engine = 200.0;
private double battery = 100.0;
private double oi_filter = 70.0;
private double brakes = 100.0;
private double gears = 100.0;
private double regular_wash = 50.0;
private double wash_Compound = 100.0;
// Declare variables to store the selected services
private boolean smallEngineSelected = false;
private boolean bigEngineSelected = false;
private boolean batterySelected = false;
private boolean oilAndFilterSelected = false;
private boolean brakesSelected = false;
private boolean gearsSelected = false;
private boolean regularWashSelected = false;
private boolean washAndCompoundSelected = false;
// Declare a variable to store the subtotal price
private double subtotal = 0.0;
public demo() {
System.out.println("Before GUI initialization");
initComponents();
System.out.println("After GUI initialization");
}
private void updateSubtotal() {
subtotal = 0.0;
if (smallEngineSelected)
subtotal += small_Engine;
if (bigEngineSelected)
subtotal += big_engine;
if (batterySelected)
subtotal += battery;
if (oilAndFilterSelected)
subtotal += oi_filter;
if (brakesSelected)
subtotal += brakes;
if (gearsSelected)
subtotal += gears;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jRadioButton2 = new javax.swing.JRadioButton();
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jPanel2 = new javax.swing.JPanel();
jRadioButton4 = new javax.swing.JRadioButton();
jRadioButton5 = new javax.swing.JRadioButton();
jRadioButton6 = new javax.swing.JRadioButton();
jPanel3 = new javax.swing.JPanel();
jCheckBox1 = new javax.swing.JCheckBox();
jCheckBox2 = new javax.swing.JCheckBox();
jCheckBox3 = new javax.swing.JCheckBox();
jCheckBox4 = new javax.swing.JCheckBox();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jRadioButton2.setText("jRadioButton2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Auto Tech and Mech Services");
setEnabled(false);
jLabel1.setText("Welcome to Auto Tech and Mech Services");
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel1.setToolTipText("");
jRadioButton1.setText("Small");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
jRadioButton3.setText("Big");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton1)
.addComponent(jRadioButton3))
.addContainerGap(10, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(jRadioButton1)
.addGap(29, 29, 29)
.addComponent(jRadioButton3)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel2.setName(""); // NOI18N
jRadioButton4.setText("None");
jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton4ActionPerformed(evt);
}
});
jRadioButton5.setText("Regular Wash");
jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton5ActionPerformed(evt);
}
});
jRadioButton6.setText("Wash & Compound");
jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton6ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton4)
.addComponent(jRadioButton5)
.addComponent(jRadioButton6))
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jRadioButton4)
.addGap(28, 28, 28)
.addComponent(jRadioButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButton6)
.addGap(14, 14, 14))
);
jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel3.setToolTipText("");
jPanel3.setName(""); // NOI18N
jCheckBox1.setText("Battery");
jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox1ActionPerformed(evt);
}
});
jCheckBox2.setText("Oil Filters");
jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox2ActionPerformed(evt);
}
});
jCheckBox3.setText("Brakes");
jCheckBox3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox3ActionPerformed(evt);
}
});
jCheckBox4.setText("Gear");
jCheckBox4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox4ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jCheckBox1)
.addComponent(jCheckBox2)
.addComponent(jCheckBox3)
.addComponent(jCheckBox4))
.addContainerGap(105, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jCheckBox1)
.addGap(18, 18, 18)
.addComponent(jCheckBox2)
.addGap(18, 18, 18)
.addComponent(jCheckBox3)
.addGap(18, 18, 18)
.addComponent(jCheckBox4)
.addContainerGap(8, Short.MAX_VALUE))
);
jButton1.setText("Calculate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Exit");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2))
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 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()
.addGap(12, 12, 12)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1.getAccessibleContext().setAccessibleName("");
jPanel2.getAccessibleContext().setAccessibleName("");
pack();
}// </editor-fold>
//Small engine ratio button
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
smallEngineSelected = true;
bigEngineSelected = false;
jRadioButton3.setSelected(false);
updateSubtotal();
}
//big engine ratio button
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
bigEngineSelected = true;
smallEngineSelected = false;
jRadioButton1.setSelected(false);
updateSubtotal();
}
//Oil filter check button
private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {
oilAndFilterSelected = jCheckBox2.isSelected();
updateSubtotal();
}
//none ration button
private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {
washAndCompoundSelected = false;
regularWashSelected = false ;
jRadioButton5.setSelected(false);
jRadioButton6.setSelected(false);
}
//battery check button
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
batterySelected = jCheckBox1.isSelected();
updateSubtotal();
}
//Brakes check button
private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) {
brakesSelected = jCheckBox3.isSelected();
updateSubtotal();
}
//gear check button
private void jCheckBox4ActionPerformed(java.awt.event.ActionEvent evt) {
gearsSelected = jCheckBox4.isSelected();
updateSubtotal();
}
//rugular was ration button
private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {
regularWashSelected = true;
washAndCompoundSelected =false;
jRadioButton6.setSelected(false);
updateSubtotal();
}
//Wash and compound ration button
private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {
washAndCompoundSelected = true;
regularWashSelected = false;
jRadioButton5.setSelected(false);
updateSubtotal();
}
//calculate button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double total = subtotal + (subtotal * 0.1);
double tax=subtotal * 0.1;
Total totalFrame = new Total(total, subtotal, tax);
totalFrame.setVisible(true);
}
//exit button
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new demo().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JCheckBox jCheckBox3;
private javax.swing.JCheckBox jCheckBox4;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JRadioButton jRadioButton5;
private javax.swing.JRadioButton jRadioButton6;
// End of variables declaration
}
total class
package car.wash_05;
public class Total extends javax.swing.JFrame {
public Total() {
}
public Total(double tota, double subtota, double ta) {
initComponents();
jLabel1.setText("Subtotal: $" + subtota);
jLabel2.setText("tax: $" + ta);
jLabel3.setText("Total: $" + tota);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Total");
jButton1.setText("Ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2");
jLabel3.setText("jLabel3");
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(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addComponent(jLabel3)))
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jButton1)))
.addContainerGap(105, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(20, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addContainerGap())
);
pack();
}// </editor-fold>
//ok button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Total().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration
}
I trid adding check, restarting the system and reinstalling java. it wouldn't work.it just freezes, not even the close and other buttons would work, have to force chose it from task manager.
On line 75 in the initComponents
method you are using setEnabled(false)
this disables the window and all of its children (all of the components on the frame) hence it feels like your window is frozen.
To fix this issue simply remove the line setEnabled(false)
from the following code
/* Rest of your code */
jRadioButton2.setText("jRadioButton2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Auto Tech and Mech Services");
setEnabled(false); //Line causing this issue
jLabel1.setText("Welcome to Auto Tech and Mech Services");
/* Rest of your code */
• Remember naming conventions! Avoid naming your class demo.java
instead use Demo.java
.
• Secondly you don't need a seperate method to process the action performed just move it directly into the button.addActionListener
's action listener.
• Thirdly (It is personal preference), you don't need to use -
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Demo().setVisible(true);
}
});
You can use lambdas to shorten this code (learn more about that here) and make the code only one line as demonstrated below.
java.awt.EventQueue.invokeLater(() -> new Demo().setVisible(true));