【问题标题】:How come my lists doesn't print when I run the program?当我运行程序时,为什么我的列表不打印?
【发布时间】:2017-03-21 13:12:33
【问题描述】:

它运行正常,但我的列表根本不打印。另外,我应该在哪里为我的列表添加转换为小写的方法?而且我还需要修剪列表之前或之后的空格,哪种方法最适合?

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
}

【问题讨论】:

  • 你在哪里打印?

标签: java oop user-interface trim tolower


【解决方案1】:

不确定这是否是您所要求的,但对于您在 String 类中所说的内容,已经存在两种方法。示例:

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

将打印出“示例文本”

【讨论】:

  • 不完整的答案,列表应该使用 for 循环或类似的东西。
【解决方案2】:

您需要将您的groceryList 添加到您的groceryPanel。在buildGroceryPanel() 中添加groceryPanel.add(groceryList); 会有所帮助。

【讨论】:

    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多