gpt4 book ai didi

java - 将 int 值分配给 JCheckBox

转载 作者:行者123 更新时间:2023-12-01 08:00:50 24 4
gpt4 key购买 nike

我尝试为五个不同的 JCheckbox 设置一个值,但这样做遇到了一些麻烦。我已在任何方法之前将复选框设置为主类中的 boolean 类型数据:

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

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

最佳答案

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

enter image description here

输出

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

关于java - 将 int 值分配给 JCheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25356401/

24 4 0
文章推荐: python - 如何在日期时间轴图上指定矩形?
文章推荐: jquery - Flexslider 从随机幻灯片开始,然后继续按顺序加载
文章推荐: r - 在 Sweave : is this a bug? 中使用 legend()
文章推荐: jquery - 如何不断检查
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com