0

問題なく動作しますが、リストがまったく印刷されません。また、リストの小文字への変換メソッドをどこに追加すればよいですか? また、リストの前後の空白を削除する必要がありますが、どの方法が最適ですか?

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
}
4

2 に答える 2

0

に を追加する必要がありgroceryListますgroceryPanel。追加groceryPanel.add(groceryList);するbuildGroceryPanel()と役立ちます。

于 2016-11-08T02:49:05.997 に答える
-2

これがあなたが求めているものかどうかはわかりませんが、あなたが話していることのための 2 つのメソッドが String クラスに既に存在します。例:

String str = " SamPle TeXt ";
str = str.toLowerCase().trim();
System.out.println(str);

「サンプルテキスト」を出力します

于 2016-11-08T02:26:13.610 に答える