actionlistener没有用jbutton click替换所需的面板

3xiyfsfu  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(340)

我正在创建一个待办事项列表,并试图在页脚面板中创建一个名为“添加任务”的按钮,该按钮将消失,并替换为提示用户键入有关要添加到列表中的任务的信息。
我只是觉得actionlistener有点问题,因为它好像什么都没做。我不确定我做错了什么。
以下是相关代码,如有任何帮助,将不胜感激。main方法将包含主要问题,但我已经为上下文提供了其余代码。

import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class ToDo{

    ArrayList<Item> TodoList = new ArrayList<>();
    static int size=0;

    public void addItem(String item, String category, int priority)
    {
        TodoList.add(new Item(item,category,priority));
        size++;
    }

    //remove item at specified index spot
    private void removeItem(String item )
    {
        TodoList.remove(getIndex(item));
        size--;

    }

    public void getList()
    {
        for (Item item : TodoList)
        {
            System.out.println(item.toString());
        }
    }

    // Return the index of the searched item, accounts for differences in white spaces and cases
    public int getIndex(String item)
    {
        //return -1 if not found  ,  \\s+ => more than one white space
        int index = -1;
        String itemString = item.replaceAll("\\s+", "").toLowerCase();
        for (int i = 0; i < TodoList.size(); i++) {
            String listItem = TodoList.get(i).getItem().replaceAll("\\s+", "").toLowerCase();
            if (listItem.contains(itemString)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public String getItemString(int i)
    {
        return TodoList.get(i).toString();
    }

    public void sortItems()
    {
        TodoList.sort(Item.priorityComparator);
    }

    public void print() {
        System.out.println("To-do List: ");
        System.out.println("-----------");
        getList();
        if (TodoList == null) {
            System.out.println("You're all done for today!");
        }
    }

    public static void main(String[] args) {

        ToDo todo = new ToDo();

        todo.addItem("Get pickles", "Shopping", 2);
        todo.addItem("Read book", "School", 3);
        todo.addItem("Send letter", "Other", 1);
        todo.addItem("Buy planner", "School", 4);
        todo.addItem("Get potatoes", "Shopping", 3);

        //initialize data array to hold items
        String[] data = new String[100];

        for (int i = 0; i < size; i++) {
            //sort items and populate data array with items converted to string
            todo.sortItems();
            data[i] = todo.getItemString(i);
        }

        ///declare components of panels
        JCheckBox[] checkBox;
        JButton resetButton = new JButton("Reset List");
        ;
        JButton addButton = new JButton("Add Task");

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel headerPanel = new JPanel();
        JPanel contentPane = new JPanel();
        JPanel footerPanel = new JPanel();

        JLabel title = new JLabel("TO-DO");
        headerPanel.add(title);

        contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(5, 5));

        checkBox = new JCheckBox[size];
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(0, 1, 5, 5));

        for (int i = 0; i < size; i++) {
            checkBox[i] = new JCheckBox(data[i]);
            centerPanel.add(checkBox[i]);
        }

        contentPane.add(headerPanel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);

        footerPanel.add(resetButton);
        footerPanel.add(addButton);

        contentPane.add(footerPanel, BorderLayout.SOUTH);

        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               footerPanel.add(new JTextField("What category is the task?"));
           }
       });

        frame.setContentPane(contentPane);
        frame.setSize(400,500);
        frame.setResizable(false);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        todo.print();

        System.out.println("-----------------\n");

        todo.removeItem("Get pickles");

        todo.sortItems();

        todo.print();

    }
}

以下是代码中引用的项目类:

import java.util.Comparator;

public class Item{

    private String item;
    private String category;
    private int priority;

    //default constructor to initialize
    public Item(String item, String category, int priority){
        this.item = item;
        this.category = category;
        this.priority = priority;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public int getPriority() {
        return priority;
    }

    public String translatePriority()
    {
        if (priority == 1)
            return "low";
        else if (priority == 2)
            return "medium";
        else if (priority == 3)
            return "high";
        else if (priority == 4)
            return "urgent";
        else
            return "invalid priority";

    }

    @Override
    public String toString() {
        return "PRIORITY: " +
                translatePriority() + " || " + category + ", " + item + " ";
    }

    // override Compare method of Comparator in order to reorder based on priority
    public static Comparator<Item> priorityComparator = new Comparator<Item>() {

        public int compare(Item i1, Item i2) {

            int priority1 = i1.getPriority();
            int priority2 = i2.getPriority();

            /*For ascending order*/
            return priority2-priority1;

        }};

}
im9ewurl

im9ewurl1#

首先:
变量名不应以大写字符开头
不需要使用静态变量。
提示用户输入有关要添加到列表中的任务的信息。
使用 JOptionPane 提示用户提供信息。
有关更多信息和示例,请参阅:如何创建对话框。

footerPanel.add(new JTextField("What category is the task?"));

文本字段不显示,因为您需要 revalidate() 添加组件后的面板,以便可以为文本字段指定大小和位置。
但是,即使这样做也不会有帮助,因为您创建的组件没有引用,因此您将无法轻松获得用户键入的文本。另外,您将如何从面板中移除组件(无需参考即可轻松移除)?
joptionpane是更简单的解决方案。

相关问题