【问题标题】:Assign int value to JCheckBox将 int 值分配给 JCheckBox
【发布时间】:2014-10-10 23:00:53
【问题描述】:

我试图为五个不同的 JCheckbox 设置一个值,但这样做有点麻烦。 在任何方法之前,我已将复选框设置为主类中的布尔类型数据:

 boolean s1 = solarPanel.isSelected();
 boolean s2 = ducted.isSelected();
 boolean s3 = homeTheatre.isSelected();
 boolean s4 = spa.isSelected();
 boolean s5 = swimming.isSelected();

现在我试图通过函数为每个复选框分配一个 int 值:

private void options()
{ 
    if (s1 == true){
        s1 = 7500;
    } 
    if (s2 == true){
        s2 = 5000;
    } 
    if (s3 == true){
        s3 = 8000;
    } 
    if (s4 == true){
        s4 = 3500;
    } 
    if (s5 == true){
        s5 = 12000;
    } 
}

但出现“不兼容的类型”错误。从这里我将在另一个函数中调用“选项”函数,以便计算最终价格。提前致谢!

【问题讨论】:

  • 很好奇...为什么不只传递一个布尔数组,其中每个元素代表其中一个复选框的值...?
  • “我试图为五个不同的 JCheckbox 设置一个值” 通过 'set' DYM 一次只能选择一个?如果是这样,我建议使用自定义单元格渲染器查看JRadioButtonButtonGroup 或更好的JComboBox 或单选JList 显示自定义对象(例如具有属性productNameenergyConsumption)。
  • 我将如何处理@MadProgrammer? @AndrewThompson 不,我需要让用户能够在任何时候选择多个或根本不选择。
  • 你有五个值,创建一个5boolean[] options = new boolean[5];)的boolean数组,然后为每个选项,在options数组中设置它的对应元素...
  • 还是有点困惑... 'private void options() { boolean[] options = new boolean[5];选项[0] = 7500;选项[1] = 5000;选项[2] = 8000;选项[3] = 3500;选项[4] = 12000; }' 像这样? @MadProgrammer

标签: java swing user-interface integer boolean


【解决方案1】:

考虑这种替代方法,我们正在处理Product 形式的一组对象,这些对象使用自定义单元格渲染器在JList 中显示属性productNameenergyConsumption

输出

User Selected:
Product name: Ducted Heating  power consumption: 5000
Product name: Home Theater  power consumption: 8000
Product name: Heated Pool  power consumption: 12000

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ListIterator;
import javax.swing.*;

public class ProductSelector {

    public static void main(String[] args) {
        final Product[] products = {
            new Product("None", 0),
            new Product("Ducted Heating", 5000),
            new Product("Home Theater", 8000),
            new Product("Heated Spa", 3500),
            new Product("Heated Pool", 12000)
        };
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JList list = new JList(products);
                list.setVisibleRowCount(products.length);
                list.setCellRenderer(new ProductCellRenderer(30));

                JOptionPane.showMessageDialog(null, new JScrollPane(list));
                java.util.List selected = list.getSelectedValuesList();
                ListIterator li = selected.listIterator();
                System.out.println("User Selected:");
                while (li.hasNext()) {
                    System.out.println(li.next());
                }
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class ProductCellRenderer extends DefaultListCellRenderer {

    int scale;

    ProductCellRenderer(int scale) {
        this.scale = scale;
    }

    @Override
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(
                list, value, index, isSelected, cellHasFocus);
        if (c instanceof JLabel && value instanceof Product) {
            JLabel l = (JLabel) c;
            Product product = (Product) value;
            l.setHorizontalTextPosition(SwingConstants.TRAILING);
            l.setVerticalTextPosition(SwingConstants.CENTER);
            int width = product.getPowerConsumption() / scale;
            int type = BufferedImage.TYPE_INT_RGB;
            if (width > 0) {
                BufferedImage bi = new BufferedImage(
                        width,
                        16,
                        type);
                l.setIcon(new ImageIcon(bi));
            }
            l.setText(product.getProductName());
        }
        return c;
    }
}

class Product {

    private String productName;
    private int powerConsumption;

    public Product() {
    }

    public Product(String productName, int powerConsumption) {
        this.productName = productName;
        this.powerConsumption = powerConsumption;
    }

    /**
     * @return the productName
     */
    public String getProductName() {
        return productName;
    }

    /**
     * @param productName the productName to set
     */
    public void setProductName(String productName) {
        this.productName = productName;
    }

    /**
     * @return the powerConsumption
     */
    public int getPowerConsumption() {
        return powerConsumption;
    }

    /**
     * @param powerConsumption the powerConsumption to set
     */
    public void setPowerConsumption(int powerConsumption) {
        this.powerConsumption = powerConsumption;
    }

    @Override
    public String toString() {
        return "Product name: " + productName
                + "  power consumption: " + powerConsumption;
    }
}

【讨论】:

    【解决方案2】:

    那些是 boolean 并且您正在尝试将 int 文字分配给它,

    如果你想保存那些int字面量的值,你需要另一种类型的变量

    还有(s1 == true)类似于(s1)

    【讨论】:

      【解决方案3】:

      您不能将整数分配给布尔值,该布尔值对于 truefalse 仅保留 1 位数据

      this data type for simple flags that track true/false conditions. 
      This data type represents one bit of information, but its "size" 
      isn't something that's precisely defined.
      

      【讨论】:

        猜你喜欢
        • 2021-12-17
        • 1970-01-01
        • 2013-10-15
        • 2023-03-26
        • 2023-02-22
        • 2014-10-10
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多