将 Selected JList 组件添加到对象
Adding Selected JList component to Object
我刚做了一个小程序,客户从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
已经存在,那么您需要某种 setter,否则只需创建一个新的 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]);
}
});
或类似的东西
我刚做了一个小程序,客户从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
已经存在,那么您需要某种 setter,否则只需创建一个新的 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]);
}
});
或类似的东西