Java 将int值赋给JCheckBox
我正试图将一个值设置为五个不同的JCheckbox,但这样做有点困难。 在任何方法之前,我已将复选框设置为主类中的布尔类型数据:Java 将int值赋给JCheckBox,java,swing,user-interface,integer,boolean,Java,Swing,User Interface,Integer,Boolean,我正试图将一个值设置为五个不同的JCheckbox,但这样做有点困难。 在任何方法之前,我已将复选框设置为主类中的布尔类型数据: boolean s1 = solarPanel.isSelected(); boolean s2 = ducted.isSelected(); boolean s3 = homeTheatre.isSelected(); boolean s4 = spa.isSelected(); boolean s5 = swimming.isSelected(); 现
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”函数,以便计算最终价格。提前谢谢 这些是布尔值,您试图为其指定int-literal
如果要保存这些int-literal的值,则需要另一种类型的变量
而且s1==true与s1类似,它们是布尔值,您试图为其赋值int-literal
如果要保存这些int-literal的值,则需要另一种类型的变量
此外,s1==true与s1类似您不能为布尔值指定一个整数,因为布尔值仅为true和false保留1位数据
不能将整数指定给布尔值,因为布尔值仅为true和false保留1位数据
考虑这种替代方法,其中我们处理的是一组产品形式的对象,该产品的属性productName&energyConsumption使用自定义单元格渲染器显示在JList中 输出
考虑这种替代方法,其中我们处理的是一组产品形式的对象,该产品的属性productName&energyConsumption使用自定义单元格渲染器显示在JList中 输出
奇怪…为什么不传递一个布尔数组,其中每个元素代表一个复选框的值…?我试图通过“set”DYM将一个值设置为五个不同的JCheckbox,一次只能选择一个?如果是这样,我建议查看JRadioButton和ButtonGroup或更好的JComboBox或单选JList,使用自定义单元格渲染器显示自定义对象,例如属性productName和energyConsumption。我该如何处理@MadProgrammer@AndrewThompson否我需要让用户能够在任何时候选择多个或一个都不能选择。如果有五个值,请创建一个包含5个布尔[]选项的布尔数组=新布尔[5];,然后对于每个选项,在选项数组中设置它的对应元素…仍然有点混乱…'私有无效选项{boolean[]options=new boolean[5];options[0]=7500;options[1]=5000;options[2]=8000;options[3]=3500;options[4]=12000;}诸如此类@MadProgrammerCurious…为什么不传递一个布尔数组,其中每个元素代表一个复选框的值…?我试图通过“set”DYM将一个值设置为五个不同的JCheckbox,一次只能选择一个?如果是这样,我建议查看JRadioButton和ButtonGroup或更好的JComboBox或单选JList,使用自定义单元格渲染器显示自定义对象,例如属性productName和energyConsumption。我该如何处理@MadProgrammer@AndrewThompson否我需要让用户能够在任何时候选择多个或一个都不能选择。如果有五个值,请创建一个包含5个布尔[]选项的布尔数组=新布尔[5];,然后对于每个选项,在选项数组中设置它的对应元素…仍然有点混乱…'私有无效选项{boolean[]options=new boolean[5];options[0]=7500;options[1]=5000;options[2]=8000;options[3]=3500;options[4]=12000;}诸如此类@疯狂程序员
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.
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;
}
}