좋습니다. 여기 뉴비 야. 사용자가 '홈 목록'에서 음식을 선택하고 '>>'버튼을 클릭하여 '쇼핑 목록'인 왼쪽의 목록에 추가하거나 그 반대의 경우에이 항목을 만들었습니다. 사용자가 버튼을 클릭 한 후 버튼을 클릭하면 약간의 변화가 시작 되기는하지만 잘 작동합니다. 전체 목록을 다시 인쇄하고 배열로 나타납니다. JList에 선택한 값을 추가하기 만하면됩니다. Heres는 코드 :배열 출력 방법을 수정하는 방법
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import javax.swing.JTextField;
public class MAIN extends JFrame {
Button ltor, rtol;
JList homelist, shoppinglist;
DefaultListModel homefoodlist = new DefaultListModel();
DefaultListModel shoppingfoodlist = new DefaultListModel();
JTextField foodlog;
String[] hfood = {"Tuna", "Mayo", "Ketchup", "Sun Flower Oil", "Buscuits", "Cookies", "Turkey"};
String[] sfood = {"Chocolate", "bread", "Milk", "Toast", "Beef", "Chicken"};
public static void main(String[] args) {
new MAIN();
}
private MAIN(){
JPanel thepanel = new JPanel();
thehandler handler = new thehandler();
this.setLocationRelativeTo(null);
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("Shopping List");
this.add(thepanel);
//Creating the Home List(left list)
for(String homefood: hfood){
homefoodlist.addElement(homefood);
}
homelist = new JList(homefoodlist);
homelist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
thepanel.add(homelist);
//Buttons for moving lists from left to right
ltor = new Button(">>");
thepanel.add(ltor);
ltor.addActionListener(handler);
rtol = new Button("<<");
rtol.addActionListener(handler);
thepanel.add(rtol);
//Creating the Shopping list(right list)
for(String shoppingfood: sfood){
shoppingfoodlist.addElement(shoppingfood);
}
shoppinglist = new JList(shoppingfoodlist);
shoppinglist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
thepanel.add(shoppinglist);
}
//ActionListener
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent e){
//The HomeList to the ShoppingList
if(e.getSource() == ltor){
if(homelist.isSelectionEmpty() == false){
shoppingfoodlist.addElement(homefoodlist);
homefoodlist.remove(homelist.getSelectedIndex());
}else{
JOptionPane.showMessageDialog(null, "Select a food from either list");
}
}
if(e.getSource() == rtol){
if(shoppinglist.isSelectionEmpty() == false){
homefoodlist.addElement(shoppingfoodlist);
shoppingfoodlist.remove(shoppinglist.getSelectedIndex());
}else{
JOptionPane.showMessageDialog(null, "Select a food from either list");
}
}
}
}
}
선택한 요소 대신 전체 목록을 소개 할 가능성이 있습니까? 'shoppingfoodlist.addElement (homefoodlist);' – iberbeu