java小程序中的arraylist按钮事件处理

8fq7wneg  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(266)

我的头好像被捆住了。我是java新手,正在努力学习。到目前为止,我正在做一个applet程序,它应该接收用户输入(朋友列表)并在applet窗口上垂直输出。到目前为止,我正在努力解决以下问题:
侦听来自两个或多个按钮的事件
从textfield获取用户输入,并将其添加到arraylist
下面是我目前的代码。谢谢您。

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5. public class NamesArraylistExample extends Applet implements ActionListener
  6. {
  7. ArrayList<String> listOfClassmates = new ArrayList<String>();
  8. Label titleLabel;
  9. Label noticeLabelA;
  10. Label noticeLabelB;
  11. Label namesInputLabel;
  12. Label namesOutputLabel;
  13. TextField namesInput;
  14. Button submitButton;
  15. Button quitButton;
  16. String nameInput;
  17. boolean action = true;
  18. int maxInput = 20;
  19. int countInput = 0;
  20. int yPn = 220;
  21. public void init()
  22. {
  23. setSize(300, 700);
  24. titleLabel = new Label("WELCOME TO THE NAMES LISTING PROGRAM");
  25. noticeLabelA = new Label("Please enter your classmates' names below");
  26. noticeLabelB = new Label("Press \"Quit\" button to cease adding");
  27. namesInputLabel = new Label("Please enter your friends names: (*)");
  28. namesOutputLabel = new Label("Below is a list of your friends.");
  29. namesInput = new TextField(35);
  30. submitButton = new Button("Submit");
  31. quitButton = new Button("Quit");
  32. add(titleLabel);
  33. add(noticeLabelA);
  34. add(noticeLabelB);
  35. add(namesInputLabel);
  36. add(namesInput);
  37. add(submitButton);
  38. add(quitButton);
  39. submitButton.addActionListener(this);
  40. quitButton.addActionListener(this);
  41. }
  42. public void paint(Graphics g)
  43. {
  44. g.drawLine(20, 175, 280, 175);
  45. while (action=true && countInput<maxInput)
  46. {
  47. listOfClassmates.add(nameInput);
  48. countInput++;
  49. }
  50. g.drawString("Here is a list of your friends", 20, 200);
  51. //displays the array list contents vertically
  52. for (int i = 0; i < listOfClassmates.size(); i++)
  53. {
  54. // accessing each element in the array list as per index
  55. String s = listOfClassmates.get(i);
  56. g.drawString("*"+ s + ".", 20, yPn);
  57. yPn = yPn + 15;
  58. }
  59. g.drawLine(20, 600, 280, 600);
  60. }
  61. public void actionPerformed(ActionEvent e)
  62. {
  63. nameInput = namesInput.getText();
  64. // if(e.getActionCommand().equals("Submit"))
  65. // {
  66. //
  67. // nameInput = namesInput.getText();
  68. // }
  69. // else
  70. // {
  71. //
  72. // action = false;
  73. // }
  74. repaint();
  75. }
  76. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题