0

I am making a shop program, and when I click one of the checkboxes,(which is supposed to change the value of the cost and to change the value of the JLabel) but instead, all of the checkboxes and pictures went to the bottom and there was the JLabel

I have tried changing the font, size, placement but it always stayed in the same spot

import java.awt.*;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.*;

public class Main extends JFrame{
    public static void main(String[] args) throws Exception{
        JFrame f = new JFrame("Shop");

        f.setExtendedState(JFrame.MAXIMIZED_BOTH); 

        String loc1;
        JButton button;
        JCheckBox q1;
        JCheckBox q2;
        JCheckBox q3;
        JCheckBox q4;
        JCheckBox q5;
        float xboxP = (float) 399.00;
        float ipadP = (float) 1299.00;
        float ps4P = (float) 399.99;
        float nintendoP = (float) 299.99;
        float pcP = (float) 750.00;
        float cost = 0;
        String totalC;

        ArrayList items = new ArrayList();

        JPanel p = new JPanel();
        button = new JButton("Done");
        //(x,y,width,length)
        button.setBounds(0, 560, 100, 50);
        f.add(button);

        q1 = new JCheckBox("XBOX");
        q1.setBounds(0,0,100,20);
        f.add(q1);

        URL url1 = Main.class.getResource(
                "/pictures/XBOX.png");
        ImageIcon icon1 = new ImageIcon(url1);
        JLabel l1 = new JLabel(icon1);
        l1.setBounds(100,100,100,100);
        f.add(l1);

        l1.setBounds(100,0,100,100);
        f.add(l1);
        q2 = new JCheckBox("IPad");
        q2.setBounds(0, 100, 50, 30);
        f.add(q2);

        URL url2 = Main.class.getResource(
                "/pictures/IPAD.png");
        ImageIcon icon2 = new ImageIcon(url2);
        JLabel l2 = new JLabel(icon2);
        l2.setBounds(100,100,100,100);
        f.add(l2);

        q3 = new JCheckBox("PS4");
        q3.setBounds(0, 200, 50, 30);
        f.add(q3);

        URL url3 = Main.class.getResource(
                "/pictures/PS4.png");
        ImageIcon icon3 = new ImageIcon(url3);
        JLabel l3 = new JLabel(icon3);
        l3.setBounds(100,200,100,100);
        f.add(l3);

        q4 = new JCheckBox("Nintendo Switch");
        q4.setBounds(0, 300, 150, 30);
        f.add(q4);

        URL url4 = Main.class.getResource(
                "/pictures/Nintendo Switch.png");
        ImageIcon icon4 = new ImageIcon(url4);
        JLabel l4 = new JLabel(icon4);
        l4.setBounds(0,300,150,150);
        f.add(l4);

        q5 = new JCheckBox("Gaming PC");
        q5.setBounds(0, 400, 100, 30);
        f.add(q5);

        URL url5 = Main.class.getResource(
                "/pictures/Gaming PC.png");
        ImageIcon icon5 = new ImageIcon(url5);
        JLabel l5 = new JLabel(icon5);
        l5.setBounds(0,420,100,100);
        f.add(l5);

        p.setLayout(null);

        JCheckBox box1 = new JCheckBox();

        f.add(p);
        // setLayout(null);
        f.pack();
        f.setDefaultCloseOperation(3);
        f.setSize(700, 650);
        f.setVisible(true);
        q1.addActionListener(null);
        //Button j;
        JLabel price = new JLabel("Cost:$0");

        price.setBounds(0, 500, 10, 10);

        //Border border = BorderFactory.createLineBorder(Color.BLACK, 5);

        //price.setBorder(border);
        price.setFont(new Font("Serif", Font.PLAIN, 10));
        f.setLocation(0,500);
        f.add(price);
        f.setForeground(Color.BLACK);
        //f.setBackground(Color.WHITE);
        price.setOpaque(true);
        f.repaint();
        while (true) {
            StringBuilder sb = new StringBuilder();
            sb.append("");
            sb.append(cost);
            String strI = sb.toString();
            totalC= "Cost:$"+strI;
            price.setText(totalC);
            f.add(price); 
            f.validate();
            boolean q1s = q1.isSelected();
            boolean q2s = q2.isSelected();
            boolean q3s = q3.isSelected();
            boolean q4s = q4.isSelected();
            boolean q5s = q5.isSelected();
            //Question 1
            if (q1s==true) {
                if(items.contains("xbox")) {
                    cost = cost;
                }
                else {
                    cost=cost+xboxP;
                    items.add("xbox");
                }
            } 
            if (q1s==false) {
                if(items.contains("xbox")) {
                    cost = cost;
                }
                if(items.contains("xbox")) {
                    items.remove("xbox");
                    cost = cost-xboxP;
                }
                else {
                    cost=cost;
                }                   
            }
            //Question 2
            if (q2s == true) {
                if(items.contains("ipad")) {
                    cost = cost;
                }
                else {
                    cost = cost+ipadP;
                    items.add("ipad");
                }
            }
            if (q2s == false) {
                if(items.contains("ipad")) {
                    items.remove("ipad");
                    cost = cost-ipadP;
                }
                else {
                    cost = cost;
                }
            }
            //Question 3
            if (q3s == true) {
                if(items.contains("ps4")) {
                    cost = cost;
                }
                else {
                    cost = cost+ipadP;
                    items.add("ps4");
                }
            }
            if (q3s == false) {
                if(items.contains("ps4")) {
                    items.remove("ps4");
                    cost = cost-ps4P;
                }
                else {
                    cost = cost;
                }
            }
            //Question 4
            if (q4s == true) {
                if(items.contains("nintendo")) {
                    cost = cost;
                }
                else {
                    cost = cost+nintendoP;
                    items.add("nintendo");
                }
            }
            if (q4s == false) {
                if(items.contains("nintendo")) {
                    items.remove("nintendo");
                    cost = cost-nintendoP;
                }
                else {
                    cost = cost;
                }
            }
            //Question 5
            if (q5s == true) {
                if(items.contains("pc")) {
                    cost = cost;
                }
                else {
                    cost = cost+pcP;
                    items.add("pc");
                }
            }
            if (q5s == false) {
                if(items.contains("pc")) {
                    items.remove("pc");
                    cost = cost-pcP;
                }
                else {
                    cost = cost;
                }
            }
        }
    }
}

I want there to be a label at the bottom that displays the total cost. Thanks for checking it in advanced.

c0der
  • 18,467
  • 6
  • 33
  • 65
NanoPixel
  • 122
  • 8

1 Answers1

2

Avoid using null layouts and setting bounds manually. Use Layout managers to layout (set bounds) for you.
A common strategy to solve complex computing tasks, is to break them into small, well defined manageable tasks. Divide and conquer.
This also applies to gui: break the design into small, easy to layout containers. Start from something simple and build up from it :

import java.awt.GridLayout;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JFrame{

    public static void main(String[] args) throws Exception{

        URL[] urls = { //when posting, always use web resources to make your code mcve
                new URL("https://findicons.com/files/icons/345/summer/128/cake.png"),
                new URL("http://icons.iconarchive.com/icons/atyourservice/service-categories/128/Sweets-icon.png"),
         };

        JFrame f = new JFrame("Shop");
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //JFrame content pane uses a BorderLayout by default.
        //to change it: 
        f.setLayout(new GridLayout(0,1)); 

        JPanel panel1 = new JPanel();// uses FlowLayout by default
        JCheckBox q1 = new JCheckBox("CAKE");
        panel1.add(q1);  //add components to the sub panel 
        ImageIcon icon1 = new ImageIcon(urls[0]);
        JLabel l1 = new JLabel(icon1);
        panel1.add(l1);
        f.add(panel1); //add sub panel to main panel 

        JPanel panel2 = new JPanel();
        JCheckBox q2 = new JCheckBox("ICE CREAM");
        panel2.add(q2);

        ImageIcon icon2 = new ImageIcon(urls[1]);
        JLabel l2 = new JLabel(icon2);
        panel2.add(l2);
        f.add(panel2);

        f.pack();
        f.setVisible(true);
    }
}

More example of breaking complex layout into smaller, simpler pieces: 1 , 2

c0der
  • 18,467
  • 6
  • 33
  • 65