它运行正常,但我的列表根本不打印。另外,我应该在哪里为我的列表添加转换为小写方法?而且我还需要在列表之前或之后修剪空白区域,哪种方法最适合呢?
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
}
答案 0 :(得分:0)
您需要将groceryList
添加到groceryPanel
。在groceryPanel.add(groceryList);
中添加buildGroceryPanel()
会有所帮助。
答案 1 :(得分:-2)
不确定这是否是您所要求的,但在String类中已经存在两种方法。例如:
String str = " SamPle TeXt ";
str = str.toLowerCase().trim();
System.out.println(str);
将打印出“示例文本”