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

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

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

  1. import java.lang.reflect.Array;
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.awt.event.*;
  5. import java.awt.*;
  6. import javax.swing.*;
  7. public class ToDo{
  8. ArrayList<Item> TodoList = new ArrayList<>();
  9. static int size=0;
  10. public void addItem(String item, String category, int priority)
  11. {
  12. TodoList.add(new Item(item,category,priority));
  13. size++;
  14. }
  15. //remove item at specified index spot
  16. private void removeItem(String item )
  17. {
  18. TodoList.remove(getIndex(item));
  19. size--;
  20. }
  21. public void getList()
  22. {
  23. for (Item item : TodoList)
  24. {
  25. System.out.println(item.toString());
  26. }
  27. }
  28. // Return the index of the searched item, accounts for differences in white spaces and cases
  29. public int getIndex(String item)
  30. {
  31. //return -1 if not found , \\s+ => more than one white space
  32. int index = -1;
  33. String itemString = item.replaceAll("\\s+", "").toLowerCase();
  34. for (int i = 0; i < TodoList.size(); i++) {
  35. String listItem = TodoList.get(i).getItem().replaceAll("\\s+", "").toLowerCase();
  36. if (listItem.contains(itemString)) {
  37. index = i;
  38. break;
  39. }
  40. }
  41. return index;
  42. }
  43. public String getItemString(int i)
  44. {
  45. return TodoList.get(i).toString();
  46. }
  47. public void sortItems()
  48. {
  49. TodoList.sort(Item.priorityComparator);
  50. }
  51. public void print() {
  52. System.out.println("To-do List: ");
  53. System.out.println("-----------");
  54. getList();
  55. if (TodoList == null) {
  56. System.out.println("You're all done for today!");
  57. }
  58. }
  59. public static void main(String[] args) {
  60. ToDo todo = new ToDo();
  61. todo.addItem("Get pickles", "Shopping", 2);
  62. todo.addItem("Read book", "School", 3);
  63. todo.addItem("Send letter", "Other", 1);
  64. todo.addItem("Buy planner", "School", 4);
  65. todo.addItem("Get potatoes", "Shopping", 3);
  66. //initialize data array to hold items
  67. String[] data = new String[100];
  68. for (int i = 0; i < size; i++) {
  69. //sort items and populate data array with items converted to string
  70. todo.sortItems();
  71. data[i] = todo.getItemString(i);
  72. }
  73. ///declare components of panels
  74. JCheckBox[] checkBox;
  75. JButton resetButton = new JButton("Reset List");
  76. ;
  77. JButton addButton = new JButton("Add Task");
  78. JFrame frame = new JFrame();
  79. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  80. JPanel headerPanel = new JPanel();
  81. JPanel contentPane = new JPanel();
  82. JPanel footerPanel = new JPanel();
  83. JLabel title = new JLabel("TO-DO");
  84. headerPanel.add(title);
  85. contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  86. contentPane.setLayout(new BorderLayout(5, 5));
  87. checkBox = new JCheckBox[size];
  88. JPanel centerPanel = new JPanel();
  89. centerPanel.setLayout(new GridLayout(0, 1, 5, 5));
  90. for (int i = 0; i < size; i++) {
  91. checkBox[i] = new JCheckBox(data[i]);
  92. centerPanel.add(checkBox[i]);
  93. }
  94. contentPane.add(headerPanel, BorderLayout.NORTH);
  95. contentPane.add(centerPanel, BorderLayout.CENTER);
  96. footerPanel.add(resetButton);
  97. footerPanel.add(addButton);
  98. contentPane.add(footerPanel, BorderLayout.SOUTH);
  99. JButton okButton = new JButton("OK");
  100. okButton.addActionListener(new ActionListener() {
  101. public void actionPerformed(ActionEvent e) {
  102. footerPanel.add(new JTextField("What category is the task?"));
  103. }
  104. });
  105. frame.setContentPane(contentPane);
  106. frame.setSize(400,500);
  107. frame.setResizable(false);
  108. frame.setLocationByPlatform(true);
  109. frame.setVisible(true);
  110. todo.print();
  111. System.out.println("-----------------\n");
  112. todo.removeItem("Get pickles");
  113. todo.sortItems();
  114. todo.print();
  115. }
  116. }

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

  1. import java.util.Comparator;
  2. public class Item{
  3. private String item;
  4. private String category;
  5. private int priority;
  6. //default constructor to initialize
  7. public Item(String item, String category, int priority){
  8. this.item = item;
  9. this.category = category;
  10. this.priority = priority;
  11. }
  12. public String getItem() {
  13. return item;
  14. }
  15. public void setItem(String item) {
  16. this.item = item;
  17. }
  18. public String getCategory() {
  19. return category;
  20. }
  21. public void setCategory(String category) {
  22. this.category = category;
  23. }
  24. public void setPriority(int priority) {
  25. this.priority = priority;
  26. }
  27. public int getPriority() {
  28. return priority;
  29. }
  30. public String translatePriority()
  31. {
  32. if (priority == 1)
  33. return "low";
  34. else if (priority == 2)
  35. return "medium";
  36. else if (priority == 3)
  37. return "high";
  38. else if (priority == 4)
  39. return "urgent";
  40. else
  41. return "invalid priority";
  42. }
  43. @Override
  44. public String toString() {
  45. return "PRIORITY: " +
  46. translatePriority() + " || " + category + ", " + item + " ";
  47. }
  48. // override Compare method of Comparator in order to reorder based on priority
  49. public static Comparator<Item> priorityComparator = new Comparator<Item>() {
  50. public int compare(Item i1, Item i2) {
  51. int priority1 = i1.getPriority();
  52. int priority2 = i2.getPriority();
  53. /*For ascending order*/
  54. return priority2-priority1;
  55. }};
  56. }
im9ewurl

im9ewurl1#

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

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

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

相关问题