samedi 20 mai 2023

How to add quantity button to checkbox in Java Swing

``/*

  • Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  • Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template */

import javax.swing.*; import java.text.DecimalFormat;

/** *

  • @author arnavgupta */ public class NewJFrame extends javax.swing.JFrame {

    /**

    • Creates new form NewJFrame */ public NewJFrame() { initComponents(); }

    /**

    • 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") //
      private void initComponents() {

      jLabel1 = new javax.swing.JLabel(); Cricket = new javax.swing.JCheckBox(); Rugby = new javax.swing.JCheckBox(); Food = new javax.swing.JCheckBox(); Mac = new javax.swing.JCheckBox(); PC = new javax.swing.JCheckBox(); Bench = new javax.swing.JCheckBox(); jScrollPane1 = new javax.swing.JScrollPane(); SubTotal = new javax.swing.JTextPane(); jScrollPane2 = new javax.swing.JScrollPane(); Tax = new javax.swing.JTextPane(); jScrollPane3 = new javax.swing.JScrollPane(); Total = new javax.swing.JTextPane(); SubtotalLabel = new javax.swing.JLabel(); TaxLabel = new javax.swing.JLabel(); TotalLabel = new javax.swing.JLabel(); Calc = new javax.swing.JButton(); Clear = new javax.swing.JButton(); Exit = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jLabel1.setFont(new java.awt.Font("Kailasa", 1, 24)); // NOI18N jLabel1.setText("Sanjay's Goods");

      Cricket.setText("Cricket Bat $400"); Cricket.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { CricketActionPerformed(evt); } });

      Rugby.setText("Rugby Ball $40"); Rugby.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { RugbyActionPerformed(evt); } });

      Food.setText("Masala Dosa $19"); Food.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { FoodActionPerformed(evt); } });

      Mac.setText("Brand New Macbook pro $1"); Mac.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MacActionPerformed(evt); } });

      PC.setText("30 Year old PC $500"); PC.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PCActionPerformed(evt); } });

      Bench.setText("Bench Press with weights $100");

      jScrollPane1.setViewportView(SubTotal);

      jScrollPane2.setViewportView(Tax);

      jScrollPane3.setViewportView(Total);

      SubtotalLabel.setText("Subtotal");

      TaxLabel.setText("Tax");

      TotalLabel.setText("Total");

      Calc.setText("Calculate"); Calc.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { CalcActionPerformed(evt); } });

      Clear.setText("Clear"); Clear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ClearActionPerformed(evt); } });

      Exit.setText("Exit"); Exit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ExitActionPerformed(evt); } });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Exit) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(93, 93, 93) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(TotalLabel) .addComponent(TaxLabel)) .addGap(74, 74, 74)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(SubtotalLabel) .addGap(66, 66, 66))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2) .addComponent(jScrollPane1) .addComponent(jScrollPane3)))) .addGap(112, 112, 112)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(106, 106, 106)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(37, 37, 37) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Rugby) .addComponent(Food) .addComponent(Cricket)) .addGap(123, 123, 123) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Mac) .addComponent(Bench) .addComponent(PC))) .addGroup(layout.createSequentialGroup() .addGap(62, 62, 62) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Clear) .addComponent(Calc)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(21, 21, 21) .addComponent(jLabel1) .addGap(46, 46, 46) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Cricket) .addComponent(Mac)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Rugby) .addComponent(PC)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Food) .addComponent(Bench)) .addGap(54, 54, 54) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(SubtotalLabel) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(TaxLabel) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(11, 11, 11) .addComponent(Calc) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Clear) .addGap(11, 11, 11) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(TotalLabel) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(13, 13, 13) .addComponent(Exit) .addGap(18, 18, 18)) );

      pack(); }//

    private void CricketActionPerformed(java.awt.event.ActionEvent evt) {

    }

    private void RugbyActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }

    private void MacActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }

    private void PCActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }

    private void CalcActionPerformed(java.awt.event.ActionEvent evt) { DecimalFormat df = new DecimalFormat("0.00"); double total; double subtotal = 0; double tax; double taxrate = .08;

     if (Cricket.isSelected()){
         subtotal = subtotal + 400;
     }
     if (Rugby.isSelected()){
         subtotal = subtotal + 40;
     }
     if (Food.isSelected()){
         subtotal = subtotal + 19;
     }
    
     if (Mac.isSelected()){
         subtotal = subtotal + 1;
     }
    
     if (PC.isSelected()){
         subtotal = subtotal + 500;
     }
    
     if (Bench.isSelected()){
         subtotal = subtotal + 100;
     }
    
     SubTotal.setEditable(false);
     Tax.setEditable(false);
     Total.setEditable(false);
     SubTotal.setText(Double.toString(subtotal));
     subtotal = Double.parseDouble(SubTotal.getText());
     tax = subtotal * taxrate;
     total = tax + subtotal;
     Tax.setText(Double.toString(tax));
     Total.setText(Double.toString(total));
    
     Tax.setText(df.format(tax));
     SubTotal.setText(df.format(subtotal));
     Total.setText(df.format(total));
    

    }

    private void ClearActionPerformed(java.awt.event.ActionEvent evt) { Cricket.setSelected(false); Rugby.setSelected(false); Food.setSelected(false); Mac.setSelected(false); PC.setSelected(false); Bench.setSelected(false); SubTotal.setText(" "); Tax.setText(" "); Total.setText(" "); }

    private void ExitActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); }

    private void FoodActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }

    /**

    • @param args the command line arguments / public static void main(String args[]) { / Set the Nimbus look and feel / // / 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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //

      /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); }

    // Variables declaration - do not modify private javax.swing.JCheckBox Bench; private javax.swing.JButton Calc; private javax.swing.JButton Clear; private JCheckBox Cricket; private javax.swing.JButton Exit; private JCheckBox Food; private javax.swing.JCheckBox Mac; private javax.swing.JCheckBox PC; private JCheckBox Rugby; private javax.swing.JTextPane SubTotal; private javax.swing.JLabel SubtotalLabel; private javax.swing.JTextPane Tax; private javax.swing.JLabel TaxLabel; private javax.swing.JTextPane Total; private javax.swing.JLabel TotalLabel; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; // End of variables declaration
    }

I have a simple store GUI and I have certain products for sale using checkboxes but now I want to make it so that the user can buy multiple of the same product, how can I do that?




Aucun commentaire:

Enregistrer un commentaire