gpt4 book ai didi

java - 将组合框中的当前项存储在 ArrayList 中

转载 作者:行者123 更新时间:2023-11-30 09:04:18 25 4
gpt4 key购买 nike

我有一个简单的程序,可以制作不同大小和颜色的形状。

形状和颜色从 ComboBoxes 中选择,而大小由 slider 控制。我想通过按一个按钮将当前选择的形状、大小和颜色存储在我定义的新类的 ArrayList 中,然后按另一个按钮打印信息。

private ArrayList<ShapeItem> ShapeItem;


slider = new JSlider (JSlider.HORIZONTAL,minValue,maxValue,initValue);
add(slider);
slider.addChangeListener(this);


// Instantiate a new ComboBox with options for Shape
shapeChoice = new JComboBox();
shapeChoice.addItem("Point");
shapeChoice.addItem("Square");
shapeChoice.addItem("Circle");
shapeChoice.addItem("Doughnut");
shapeChoice.addItem("Pentagon");
shapeChoice.addItem("Hexagon");
add(shapeChoice); // Add the JComboBox to the JPanel
shapeChoice.addActionListener(this); //Register the JComboBox selection with Java

// Instantiate a new ComboBox with options for Colour
colorChoice = new JComboBox();
colorChoice.addItem("Black");
colorChoice.addItem("Red");
colorChoice.addItem("Green");

然后:

readItems = new JButton("Read values");
add(readItems);
readItems.addActionListener(this);

printItems = new JButton("List all values");
add(printItems);
printItems.addActionListener(this);

ShapeItem = new ArrayList<ShapeItem>();

我最好的办法是介绍

if (e.getSource() == readItems)
{
String item0 = shapeChoice.getSelectedItem();
String item1 = colorChoice.getSelectedItem();


}

// if the "List all values" button is pressed
else if (e.getSource() == printItems)
{
// loop through the ArrayList of items prnting each one out in turn to the JTextArea
for (int j=0; j<i; j++)
{
textOutput.append(itemList.get(j).shapeChoose + "\n");
textOutput.append(itemList.get(j).colourChoose + "\n");
textOutput.append("\n");
}
}

但是在

String item0 = shapeChoice.getSelectedItem();

我收到“不兼容类型错误”

我新建的类是这样的:

public class ShapeItem

{

public String shapeChoose;
public String colorChoose;
public int thesize;

我尝试的方法是否正确,还是有其他方法可以实现?

最佳答案

没有捷径,您必须遍历 JComboBox 中的项目并将它们中的每一个添加到数组中。

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComboBox;

public class Main {
JComboBox<String> shapeChoice = new JComboBox<>();
shapeChoice.addItem("Point");
shapeChoice.addItem("Square");
shapeChoice.addItem("Circle");
shapeChoice.addItem("Doughnut");
shapeChoice.addItem("Pentagon");
shapeChoice.addItem("Hexagon");
System.out.println(combo2Array(shapeChoice).toString());
}
public static List<String> combo2Array(JComboBox<String> combo){

List<String> list = new ArrayList<>();
for(int i=0;i<combo.getItemCount();i++){
list.add(combo.getItemAt(i));
}
return list;
}
}

输出:

[Point, Square, Circle, Doughnut, Pentagon, Hexagon]

如果需要,您也可以将其更改为泛型(不必是字符串)。但想法是一样的。

我做了我认为是您正在寻找的设计并将其发布在 gist 上.但这将是一个糟糕的设计选择,因为您正在创建一个比您实际需要的列表大得多的列表。 使用 3 个列表(每个组件一个)将是更好的设计选择,这就是为什么我不会在这里用 gist code 更新我的答案。 .

编辑 2:

仅添加当前选中的内容:

public void addSelected2List(){
ShapeItem shape = new ShapeItem();
shape.setShapeChoose((String)shapeChoice.getSelectedItem());
shape.setColourChoose((String)colourChoice.getSelectedItem());
shape.setSize((int)sizeChoice.getSelectedItem());
list.add(shape);
}

关于java - 将组合框中的当前项存储在 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com