Question
Please Help: I need to creare a separate actionListener method. Currently they reside in my design method. I need to make seperate methods and call
Please Help: I need to creare a separate actionListener method. Currently they reside in my design method. I need to make seperate methods and call them in my constructor.
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class JDemoFrameThatCloses extends javax.swing.JFrame
{
double subtotalCost = 0;
double taxTotalCost;
double GrandTotal;
DecimalFormat df = new DecimalFormat("0.00");
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
public JDemoFrameThatCloses ()
{
designComponents ();//call on the design class.
}
private void designComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));
jLabel1.setText("Dripping Cups of Coffee");
jLabel2.setText("By Jordan Ramos");
jTextField1.setText("Enter Name");
jButton1.setText("Add to Order");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
addtoOrderActionPerformed(evt);
}
});
jTextField2.setText("Quantity");
jRadioButton1.setText("Mocha");
buttonGroup1.add(jRadioButton1);
jRadioButton2.setText("Latte");
buttonGroup1.add(jRadioButton2);
jRadioButton3.setText("Drip");
buttonGroup1.add(jRadioButton3);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setEnabled(false);
jScrollPane1.setViewportView(jTextArea1);
jButton2.setText("Complete Order");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
completeOrderActionPerformed(evt);
}
});
jButton3.setText("Clear");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt)
{
clearActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(210, 210, 210))
.addComponent(jTextField2))
.addGroup(layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addGap(18, 18, 18)
.addComponent(jRadioButton2)
.addGap(18, 18, 18)
.addComponent(jRadioButton3)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(49, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(84, 84, 84))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton3)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(65, 65, 65))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3))
.addContainerGap(16, Short.MAX_VALUE))
);
pack();
} // end of design class
private void addtoOrderActionPerformed(java.awt.event.ActionEvent evt)
{
String item;
double cost;
double TAX = .0975;
if (jRadioButton1.isSelected()) {
item = "Mocha";
cost = 3.75;
}
else if (jRadioButton2.isSelected()) {
item = "Latte";
cost = 3.25;
}
else if (jRadioButton3.isSelected()) {
item = "Drip";
cost = 1.75;
}
else {
System.err.println("Nothing selected");
return;
}
int quantity = Integer.parseInt(jTextField2.getText());
jTextArea1.append(item+"\t"+quantity+"x"+cost+ "\t" + (cost*quantity) +" ");
subtotalCost += cost*quantity;
taxTotalCost += (subtotalCost*TAX);
GrandTotal = subtotalCost + taxTotalCost;
}
private void clearActionPerformed(java.awt.event.ActionEvent evt)
{
jTextArea1.setText("");
jTextField1.setText("");
jTextField2.setText("");
subtotalCost=0;
taxTotalCost=0;
buttonGroup1.clearSelection();
}
private void completeOrderActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(this, "Customer Name is " + jTextField1.getText() + " " + "Quantity is " + jTextField2.getText() + " " + "SubTotal cost is $" + df.format(subtotalCost) + " " + "Tax Total is $ " + df.format(taxTotalCost) + " " + "Grand Total is $ " + df.format(GrandTotal) );}
public static void main(String args[])
{
new JDemoFrameThatCloses().setVisible(true);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started