我试图将值设置为五个不同的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;
}
}
但是出现“不兼容的类型”错误。从这里我将在另一个函数中调用'options'函数,以便计算最终价格。在此先感谢!
答案 0 :(得分:3)
考虑这种替代方法,我们以Product
的形式处理一组具有属性productName
&的对象。使用自定义单元格渲染器在energyConsumption
中显示JList
。
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;
}
}
答案 1 :(得分:2)
这些是boolean
,您正在尝试为其分配int
字面值,
如果要保留int
字面值
(s1 == true)
也类似于(s1)
答案 2 :(得分:1)
你不能给boolean赋一个整数,boolean只是true
和false
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.