問題なく動作しますが、リストがまったく印刷されません。また、リストの小文字への変換メソッドをどこに追加すればよいですか? また、リストの前後の空白を削除する必要がありますが、どの方法が最適ですか?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class ShoppingList extends JFrame {
private JPanel groceryPanel;
private JPanel selectedGroceryPanel;
private JPanel buttonPanel;
private JList groceryList;
private JList selectedGroceryList;
private JLabel label;
private JTextField selectedGroceryItem;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private String[] lists = { "Burger Patty", "Honey Ham", "Milk",
"Egg", "Orange Juice", "Ketchup", "Lettuce",
"Hamburger Buns", "Tomatoes", "Cheese"
};
//ctor
public ShoppingList() {
setTitle("Shopping List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildGroceryPanel();
buildSelectedGroceryPanel();
buildButtonPanel();
add(groceryPanel, BorderLayout.NORTH);
add(selectedGroceryPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}//end ctor
private void buildGroceryPanel() {
groceryPanel = new JPanel();
groceryList = new JList(lists);
groceryList.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}//end buildGroceryPanel
private void buildSelectedGroceryPanel() {
selectedGroceryPanel = new JPanel();
label = new JLabel("You selected: ");
selectedGroceryList = new JList();
selectedGroceryItem = new JTextField(10);
selectedGroceryItem.setEditable(false);
selectedGroceryPanel.add(label);
selectedGroceryPanel.add(selectedGroceryItem);
}//end buildSelectedGroceryPanel
private void buildButtonPanel() {
buttonPanel = new JPanel();
button1 = new JButton("ADD");
button2 = new JButton("REMOVE");
button3 = new JButton("SAVE");
button4 = new JButton("LOAD");
button1.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());
button3.addActionListener(new ButtonListener());
button4.addActionListener(new ButtonListener());
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(button4);
}//end buildButtonPanel
private class ButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object[] selections =
groceryList.getSelectedValues();
selectedGroceryList.setListData(selections);
}
}
public static void main(String[] args) {
new ShoppingList();
}//end main
}