java - JCheckBox下的JLabel

标签 java swing layout

我正在制作一个商店程序,当我单击其中一个复选框时(这应该会更改成本值并更改 JLabel 的值),但相反,所有复选框和图片都转到底部有 JLabel

我尝试过更改字体、大小、位置,但它总是停留在同一个位置

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;
                }
            }
        }
    }
}

我希望底部有一个标签显示总成本。 感谢您提前检查。

最佳答案

避免使用空布局并手动设置边界。使用Layout managers为您布局(设置边界)。
解决复杂计算任务的常见策略是将其分解为小的、定义良好的可管理任务。分而治之。
这也适用于 gui:将设计分解为小的、易于布局的容器。从简单的事情开始,并以此为基础:

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);
    }
}

将复杂布局分解为更小、更简单部分的更多示例:1 , 2

关于java - JCheckBox下的JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53893216/

相关文章:

java - 使用 Class.forName 加载类时出现 ClassNotFoundException

单击图标时 Android DrawerLayout 崩溃

css - 布局问题 : Two Fixed Columns in Center

java - 使用 Swing 显示 GUI 框架

java - Concurrency runInBackground方法逻辑实战[9.3.2]

java - 使用 JavaZOOM BasicPlayer 类播放某些 MP3 时出错

java - 使android listview布局可滚动

java - 如何将 Jackson TypeReference 用于泛型类

java新手问题: richer java subprocesses

java - 如何在 Scala 中序列化远程 Actor 的集合?