1.11M Members

GUI problem with checkbox and radiobutton when click it

 
0
 

i got problem when i clicl the checkbox 0r radiobutton..when i compile there is no error..and also how can i put the background image at my gui??hope you guys can help me..

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.text.DecimalFormat;
import javax.swing.border.*;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.BorderLayout;


public class delizus extends JFrame implements ActionListener
{

   private Image backgroundImage;


    //Price label for Donut
   private JLabel lblChocOreo = new JLabel ( "  RM 2.50 ");
   private JLabel lblPeanut = new JLabel (    "  RM 2.50 ");
   private JLabel lblStrawberry = new JLabel (   "  RM 2.50 ");
   private JLabel lblVanilla = new JLabel (      "  RM 2.50 ");
   private JLabel lblBlueberry = new JLabel (   "  RM 2.50");


   //Price label for Additional food
   private JLabel lblCorn = new JLabel ("   RM 2.00");
   private JLabel lblSoup = new JLabel ("   RM 3.00");
   private JLabel lblSandwich = new JLabel ("   RM 1.50");

   //Price for drink
   private JLabel lblPepsi = new JLabel ("   RM 2.00");
   private JLabel lblMountain = new JLabel ("   RM 2.00");
   private JLabel lblFn = new JLabel ("   RM 2.00");

   private JLabel lblPepsiL = new JLabel ("   RM 3.00");
   private JLabel lblMountainL = new JLabel ("   RM 3.00");
   private JLabel lblFnL = new JLabel ("   RM 3.00");
   //Labelling column 1
   private JLabel lblName1 = new JLabel ("DONUT", SwingConstants.LEFT);
   private JLabel lblPrice1 = new JLabel (   "PRICE", SwingConstants.LEFT);
   private JLabel lblQuantity1 = new JLabel ("QUANTITY", SwingConstants.CENTER);

   //Labelling column 2
   private JLabel lblName2 = new JLabel ("ADDITIONAL FOOD", SwingConstants.LEFT);
   private JLabel lblPrice2 = new JLabel ("PRICE", SwingConstants.LEFT);
   private JLabel lblQuantity2 = new JLabel ("QUANTITY", SwingConstants.CENTER);

   //Labelling column 3
   private JLabel lblName3 = new JLabel ("DRINK", SwingConstants.LEFT);
   private JLabel lblRegular = new JLabel (        "REGULAR", SwingConstants.LEFT);
   private JLabel lblLarge = new JLabel (        "LARGE", SwingConstants.LEFT);
   private JLabel lblQuantity3 = new JLabel ("QUANTITY", SwingConstants.CENTER);

   //DONUT text field
   private JTextField tfChocOreo = new JTextField ("      ");
   private JTextField tfPeanut = new JTextField ("");
   private JTextField tfStrawberry = new JTextField (" ");
   private JTextField tfVanilla = new JTextField (" ");
   private JTextField tfBlueberry = new JTextField (" ");

   //ADDITIONAL FOOD Text Field
   private JTextField tfCorn = new JTextField (" ");
   private JTextField tfSoup = new JTextField (" ");
   private JTextField tfSandwich = new JTextField (" ");

   //DRINK text field
   private JTextField tfPepsi = new JTextField (" ");
   private JTextField tfMountain = new JTextField (" ");
   private JTextField tfFn = new JTextField (" ");

   //Checkbox for DONUT
   private JCheckBox cbChocOreo = new JCheckBox ("CHOCOLAT OREO");
   private JCheckBox cbPeanut = new JCheckBox ("PEANUT");
   private JCheckBox cbStrawberry = new JCheckBox ("STRAWBERRY");
   private JCheckBox cbVanilla = new JCheckBox ("VANILLA");
   private JCheckBox cbBlueberry = new JCheckBox ("BLUEBERRY");

   //Checkbox for ADDITIONAL FOOD
   private JCheckBox cbCorn =  new JCheckBox ("CORN");
   private JCheckBox cbSoup = new JCheckBox ("MUSHROOM SOUP");
   private JCheckBox cbSandwich = new JCheckBox ("SANDWICH");

   //checkbox for DRINK
   private JCheckBox cbPepsi = new JCheckBox ("PEPSI");
   private JCheckBox cbMountain = new JCheckBox ("MOUNTAIN DEW");
   private JCheckBox cbFn = new JCheckBox ("F&N");

   //radio button for SIZE
   private JRadioButton rbRegular = new JRadioButton ("REGULAR");
   private JRadioButton rbLarge = new JRadioButton ("LARGE");


   //button group for membership
   private ButtonGroup rbGroup = new ButtonGroup();



   //button for receipt, clear, and exit
   private JButton btnReceipt = new JButton("RECEIPT");
   private JButton btnClear = new JButton("CLEAR");
   private JButton btnExit = new JButton("EXIT"); 

   //Text Area for calculation
   private JTextArea txtReceipt = new JTextArea("\tWELCOME TO DELIZUS DONUT\t\t" + "\n"); 


   //layout fixed size
   private static final int width = 1240;
   private static final int height = 750; 


   Container pane = getContentPane();
   Color c = new Color(217, 161, 63);


   public delizus()
   {
       //setting for main panel
       setTitle("DELIZUS DONUT");
       setSize(width, height);
       setLocation(0,0);
       setVisible(true);
       setDefaultCloseOperation(EXIT_ON_CLOSE);


       JPanel p1 = new JPanel();
       p1.setLayout(new GridLayout(6,3));
       p1.setBorder(new TitledBorder("DONUT"));
       p1.setBackground(Color.GREEN);


       p1.add(lblName1);
       lblName1.setForeground(Color.BLACK);
       p1.add(lblPrice1);
       lblPrice1.setForeground(Color.BLACK);
       p1.add(lblQuantity1);
       lblQuantity1.setForeground(Color.BLACK);


       p1.add(cbChocOreo);
       cbChocOreo.setBackground(Color.GREEN);
       p1.add(lblChocOreo);
       p1.add(tfChocOreo);


       p1.add(cbPeanut);
       cbPeanut.setBackground(Color.GREEN);
       p1.add(lblPeanut);
       p1.add(tfPeanut);


       p1.add(cbStrawberry);
       cbStrawberry.setBackground(Color.GREEN);
       p1.add(lblStrawberry);
       p1.add(tfStrawberry);


       p1.add(cbVanilla);
       cbVanilla.setBackground(Color.GREEN);
       p1.add(lblVanilla);
       p1.add(tfVanilla);


       p1.add(cbBlueberry);
       cbBlueberry.setBackground(Color.GREEN);
       p1.add(lblBlueberry);
       p1.add(tfBlueberry);


       JPanel p2 = new JPanel();
       p2.setLayout(new GridLayout(4,3));
       p2.setBorder(new TitledBorder("ADDITIONAL FOOD"));
       p2.setBackground(Color.RED);



       p2.add(lblName2);
       lblName2.setForeground(Color.BLACK);
       p2.add(lblPrice2);
       lblPrice2.setForeground(Color.BLACK);
       p2.add(lblQuantity2);
       lblQuantity2.setForeground(Color.BLACK);


       p2.add(cbCorn);
       cbCorn.setBackground(Color.RED);
       p2.add(lblCorn);
       p2.add(tfCorn);



       p2.add(cbSoup);
       cbSoup.setBackground(Color.RED);
       p2.add(lblSoup);
       p2.add(tfSoup);



       p2.add(cbSandwich);
       cbSandwich.setBackground(Color.RED);
       p2.add(lblSandwich);
       p2.add(tfSandwich);


       JPanel p3 = new JPanel();
       p3.setLayout(new GridLayout(5,4));
       p3.setBorder(new TitledBorder("DRINKS"));
       p3.setBackground(Color.YELLOW);


       p3.add(lblName3);
       lblName3.setForeground(Color.BLACK);
       p3.add(lblRegular);
       lblName3.setForeground(Color.BLACK);
       p3.add(lblLarge);
       p3.add(lblQuantity3);
       lblQuantity3.setForeground(Color.BLACK);

       p3.add(cbPepsi);
       cbPepsi.setBackground(Color.ORANGE);
       p3.add(lblPepsi);
        p3.add(lblPepsiL);
       p3.add(tfPepsi);


       p3.add(cbMountain);
       cbMountain.setBackground(Color.ORANGE);
       p3.add(lblMountain);
       p3.add(lblMountainL);
       p3.add(tfMountain);




       p3.add(cbFn);
       cbFn.setBackground(Color.ORANGE);
       p3.add(lblFn);
       p3.add(lblFnL);
       p3.add(tfFn);



       p3.setBackground(Color.ORANGE);
       rbGroup.add(rbRegular);
       p3.add(rbRegular);
       rbRegular.setBackground(Color.ORANGE);
       //rbRegular.addActionListener(this);
       rbGroup.add(rbLarge);
       p3.add(rbLarge);
       rbLarge.setBackground(Color.ORANGE);
       //rbLarge.addActionListener(this);

       JPanel p4 = new JPanel();
       p4.setLayout(new GridLayout(11,3));
       p4.setBorder(new TitledBorder("RECEIPT"));
       p4.setBackground(Color.YELLOW);


       p4.add(btnReceipt);
       btnReceipt.setSize(80,30);
       btnReceipt.addActionListener(this);
       p4.add(btnClear);
       btnClear.setSize(80,30);
       btnClear.addActionListener(this);
       p4.add(btnExit);
       btnExit.setSize(80,30);
       btnExit.addActionListener(this); //end panel button

       JPanel p5 = new JPanel();
       p5.setLayout(new FlowLayout());


       p5.add(btnReceipt);
       btnReceipt.setSize(80,30);
       btnReceipt.addActionListener(this);
       p5.add(btnClear);
       btnClear.setSize(80,30);
       btnClear.addActionListener(this);
       p5.add(btnExit);
       btnExit.setSize(80,30);
       btnExit.addActionListener(this); //end panel button



       JPanel receipt = new JPanel();
       receipt.setBorder( new TitledBorder("BILL PAYMENT RECEIPT"));
       receipt.setBackground(c);
       receipt.add(txtReceipt); 
       txtReceipt.setTabSize(7);
       txtReceipt.setFont(new Font("Serif", Font.BOLD, 13));//end panel receipt 



       JPanel imgPanel = new JPanel();
       imgPanel.setLayout(new FlowLayout());
       imgPanel.setBackground(Color.BLACK);


       pane.setLayout(new BorderLayout());
       pane.add(p1,"North");
       pane.add(p2,"Center");
       pane.add(p3,"West");
       pane.add(p5,"South");
       pane.add(receipt,"East"); // end outermost panel


    }


     public void actionPerformed(ActionEvent event)
    {
        DecimalFormat df = new DecimalFormat("0.00");


        //get the quantity DONUT
        int p1 = Integer.parseInt(tfChocOreo.getText());
        int p10 = Integer.parseInt(tfPeanut.getText());
        int p11 = Integer.parseInt(tfStrawberry.getText());
        int p12 = Integer.parseInt(tfVanilla.getText());
        int p13 = Integer.parseInt(tfBlueberry.getText());  



        //Get the quantity for ADDITIONAL FOOD
        int p2 = Integer.parseInt(tfCorn.getText());
        int p21 = Integer.parseInt(tfSoup.getText());
        int p22 = Integer.parseInt(tfSandwich.getText());


        //get the quantity for DRINK
        int p3  = Integer.parseInt(tfPepsi.getText());
        int p31 = Integer.parseInt(tfMountain.getText());
        int p32 = Integer.parseInt(tfFn.getText()); 



        if (event.getSource() == btnReceipt)
        {
            txtReceipt.append("DONUT         " + "\tPrice"+ "\tQuantity" + "\t    Amount" + "\n");
            txtReceipt.append("\n");


            if (cbChocOreo.isSelected())
            {
                double totalChocOreo = p1 * 2.50;
                txtReceipt.append("CHOCOLAT OREO         " + "\tRM2.50" + "\t      " + p1 + "\t    RM" + df.format( totalChocOreo) + "\n"); // set to text area
            }

            if (cbPeanut.isSelected())
            {
                double totalPeanut = p10 * 2.50;
                txtReceipt.append("PEANUT            " + "\tRM2.50" + "\t      " + p10+ "\t    RM" + df.format(totalPeanut) + "\n");
            }

            if (cbStrawberry.isSelected())
            {  
                double totalStrawberry= p11 * 2.50;
                txtReceipt.append("STRAWBERRY            " + "\tRM2.50" + "\t      " + p11 + "\t    RM" + df.format(totalStrawberry) + "\n");
            }

            if (cbVanilla.isSelected())
            {
                double totalVanilla = p12 * 2.50;
                txtReceipt.append("VANILLA                " + "\tRM2.50" + "\t      " + p12 + "\t    RM" + df.format(totalVanilla) + "\n");
            }

            if (cbBlueberry.isSelected())
            {
                double totalBlueberry = p13 * 2.50;  
                txtReceipt.append("BLUEBERRY             " + "\tRM2.50" + "\t      " + p13 + "\t    RM" + df.format(totalBlueberry) + "\n");
            }

            if (cbCorn.isSelected())
            {
                double totalCorn = p2 * 2.00;
                txtReceipt.append("CORN          " + "\tRM2.00" + "\t      " + p2 + "\t    RM" + df.format(totalCorn) + "\n");
            }

            if (cbSoup.isSelected())
            {
                double totalSoup = p21 * 3.00;
                txtReceipt.append("SOUP            " + "\tRM3.00" + "\t      " + p21 + "\t    RM" + df.format(totalSoup) + "\n");
            }

            if (cbSandwich.isSelected())
            {
                double totalSandwich = p22 * 1.50;
                txtReceipt.append("SANDWICH              " + "\tRM1.50" + "\t      " + p22 + "\t    RM" + df.format(totalSandwich) + "\n");
            }


            double totalChocOreo = p1 * 2.50;
            double totalPeanut = p10 * 2.50;
            double totalStrawberry = p12 * 2.50;
            double totalVanilla = p11 * 2.50;
            double totalBlueberry = p13 * 2.50;

            double totalCorn = p2 * 2.00;
            double totalSoup = p21 * 3.00;
            double totalSandwich = p22 * 1.50;


            double totalDonut = totalChocOreo+ totalPeanut + totalStrawberry + totalVanilla + totalBlueberry ;

            double totalFood = totalCorn + totalSoup + totalSandwich;


            double totalPrice = totalDonut+ totalFood;
            double tax = 0.05 * totalPrice;
            double serviceCharge = 0.05 * totalPrice;
            double Total = totalPrice + tax + serviceCharge;

            if(rbRegular.isSelected())
            {
                double discount = (Total * 0.1);
                double smallPrice = Total - discount;
                txtReceipt.append("\n");
                txtReceipt.append("GOV TAX" + "\t\t\t\t" + "    RM" + df.format(tax) + "\n");
                txtReceipt.append("SERVICE CHARGE 5%" + "\t\t\t" + "    RM" + df.format(serviceCharge) + "\n");
                txtReceipt.append("MEMBERSHIP DISCOUNT 10%" + "\t\t" + "  - RM" + df.format(discount) + "\n");
                txtReceipt.append("TOTAL PAYMENT" + "\t\t\t" + "    RM" + df.format(rbRegular) + "\n");
                txtReceipt.append("\n");
                txtReceipt.append("\t    THANK YOU FOR SHOPPING" + "\n");
                txtReceipt.append("\t            PLEASE COME AGAIN" + "\n"); 
                txtReceipt.append("\n"); // set to text area

            }

            if(rbLarge.isSelected())
            {
               double discount = (Total * 0.0);
               double largePrice = Total - discount;
               txtReceipt.append("\n");
               txtReceipt.append("GOV TAX" + "\t\t\t\t" + "    RM" + df.format(tax) + "\n");
               txtReceipt.append("SERVICE CHARGE 5%" + "\t\t\t" + "    RM" + df.format(serviceCharge) + "\n");
               txtReceipt.append("MEMBERSHIP DISCOUNT 10%" + "\t\t" + "  - RM" + df.format(discount) + "\n");
               txtReceipt.append("TOTAL PAYMENT" + "\t\t\t" + "    RM" + df.format(rbLarge) + "\n");
               txtReceipt.append("\n");
               txtReceipt.append("\t  THANK YOU FOR SHOPPING" + "\n");
               txtReceipt.append("\t          PLEASE COME AGAIN" + "\n");
               txtReceipt.append("\n"); // set to text area
            }
        }


        else if (event.getSource() == btnClear)
        {
            tfChocOreo.setText("0");
            tfPeanut.setText("0");
            tfStrawberry.setText("0");
            tfVanilla.setText("0");
            tfBlueberry.setText("0");
            tfCorn.setText("0");
            tfSoup.setText("0");
            tfSandwich.setText("0");
            tfPepsi.setText("0");
            tfMountain.setText("0");
            tfFn.setText("0");



            cbChocOreo.setSelected(false);
            cbPeanut.setSelected(false);
            cbStrawberry.setSelected(false);
            cbVanilla.setSelected(false);
            cbBlueberry.setSelected(false);
            cbCorn.setSelected(false);
            cbSoup.setSelected(false);
            cbSandwich.setSelected(false);
            cbPepsi.setSelected(false);
            cbMountain.setSelected(false);
            cbFn.setSelected(false);



            rbRegular.setSelected(false);
            rbLarge.setSelected(false);

            txtReceipt.setText("\tWELCOME TO DELIZUS DONUT\t\t" + "\n"); //clearing all data that are available
        }

        else if (event.getSource() == btnExit)
        {
            System.exit(0); //exit the program
        }
    }

    public static void main(String [] args)
    {
        delizus d = new delizus(); //main application
        d.setVisible(true);
        d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
 
1
 

got problem when i clicl the checkbox 0r radiobutton..when i compile there is no error

Could you tell us what happens after you clicked either?

..and also how can i put the background image at my gui??

Here's some links that might help
http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm
http://stackoverflow.com/questions/523767/how-to-set-background-image-in-java

 
0
 

sorry wrong explanation..actually i can click the checkbox n radiobutton but when i want to calculate it..means i click the receipt button...terminal error occur...

 
0
 

i try to put the bg image at my gui..but still i don't know where the coding should put it..someone can help me?

 
0
 

thanks you guys for the info about the background..i manage to make it..but still there problem with my gui..no error after compile..but my receipt,clear n reset button got problem ....i really stuck and don't know how to fix it...

 
1
 

fix is

// DONUT text field
	private JTextField tfChocOreo = new JTextField("0");
	private JTextField tfPeanut = new JTextField("0");
	private JTextField tfStrawberry = new JTextField("0");
	private JTextField tfVanilla = new JTextField("0");
	private JTextField tfBlueberry = new JTextField("0");

	// ADDITIONAL FOOD Text Field
	private JTextField tfCorn = new JTextField("0");
	private JTextField tfSoup = new JTextField("0");
	private JTextField tfSandwich = new JTextField("0");

	// DRINK text field
	private JTextField tfPepsi = new JTextField("0");
	private JTextField tfMountain = new JTextField("0");
	private JTextField tfFn = new JTextField("0");

and

// get the quantity DONUT
		int p1 = (tfChocOreo.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfChocOreo.getText());
		int p10 = (tfPeanut.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfPeanut.getText());
		int p11 = (tfStrawberry.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfStrawberry.getText());
		int p12 = (tfVanilla.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfVanilla.getText());
		int p13 = (tfBlueberry.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfBlueberry.getText());

		// Get the quantity for ADDITIONAL FOOD
		int p2 = (tfCorn.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfCorn.getText());
		int p21 = (tfSoup.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfSoup.getText());
		int p22 = (tfSandwich.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfSandwich.getText());

		// get the quantity for DRINK
		int p3 = (tfPepsi.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfPepsi.getText());
		int p31 = (tfMountain.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfMountain.getText());
		int p32 = (tfFn.getText().trim().equals("")) ? 0 : Integer
				.parseInt(tfFn.getText());

hope this helps

 
0
 

thx 4 your helps..but still the receipt,clear,n reset button cannot function..i really don't know what the problem..maybe because of the import??i will try to fix it..

 
1
 

the problem lies here

txtReceipt.append("MEMBERSHIP DISCOUNT 10%" + "\t\t" + " - RM"
						+ df.format(discount) + "\n");
				txtReceipt.append("TOTAL PAYMENT" + "\t\t\t" + " RM"
						+ df.format(rbRegular) + "\n");
				txtReceipt.append("\n");
				txtReceipt.append("\t THAN

the rbRegular is a radio button and has no double or integer value in it....

fix that yourself and let us know...

 
0
 

i've tried it..but the terminal error still occur..i think i'm going crazy..hahaha..but thanks a lot for your helps..
is it package that i use is correct?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: