我刚做了一个小程序,客户从JList中选择3种颜色 以及我需要如何将这些颜色添加到Customers对象
我有像这样的构造函数
public Customer (String cc1, String cc2, String cc3){
this.colour1 = cc1;
this.colour2 = cc2;
this.colour3 = cc3;
我写的简单代码就是这个
public class Test2 extends JApplet {
Container container;
JButton b1;
JPanel panel;
JTextArea area;
String[] colours = {"Yellow", "Orange", "Red", "Purple",
"Blue", "Green", "Brown", "Black", "White"};
JList list, selectList;
String cc1, cc2, cc3; //Chosen colour
public void init() {
container = getContentPane();
panel = new JPanel(new FlowLayout());
area = new JTextArea();
b1 = new JButton("Submit");
list = new JList(colours);
selectList = new JList();
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setVisibleRowCount(5);
panel.add(new JScrollPane(list));
panel.add(b1);
panel.add(new JScrollPane(selectList));
panel.add(area);
container.add(panel);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < colours.length; i++) {
Object selected[] = list.getSelectedValues();
selectList.setListData(selected);
}
}
});
}
}
如何制作,以便该人选择的颜色转到Customer对象 谢谢
答案 0 :(得分:0)
如果Customer
已经存在,那么您需要某种设置器,否则只需创建一个新的Customer
对象
您想从列表中选择所选值并将其添加到您可以控制的String
数组中,但您只想添加最多3个项目
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] values = list.getSelectedValues();
String[] colors = new String[3];
for (int index = 0; index < Math.min(3, values.length); index++) {
colors[index] = values[index].toString();
}
Customer customer = new Customer(colors[0], colors[1], colors[2]);
}
});
或类似的东西