问题描述
我似乎束缚了自己的头。我是Java的新手,正在尝试学习。 到目前为止,我正在做一个applet程序,该程序应该接受用户输入(朋友列表)并将其垂直输出到applet窗口中。 到目前为止,我正在努力解决以下问题:
- 通过两个或多个按钮监听事件
- 从TextField接收用户输入,并将其添加到ArrayList
以下是我到目前为止的代码。谢谢。
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
public class NamesArraylistExample extends Applet implements ActionListener
{
ArrayList<String> listOfClassmates = new ArrayList<String>();
Label titleLabel;
Label noticeLabelA;
Label noticeLabelB;
Label namesInputLabel;
Label namesOutputLabel;
TextField namesInput;
Button submitButton;
Button quitButton;
String nameInput;
boolean action = true;
int maxInput = 20;
int countInput = 0;
int yPn = 220;
public void init()
{
setSize(300,700);
titleLabel = new Label("WELCOME TO THE NAMES LISTING PROGRAM");
noticeLabelA = new Label("Please enter your classmates' names below");
noticeLabelB = new Label("Press \"Quit\" button to cease adding");
namesInputLabel = new Label("Please enter your friends names: (*)");
namesOutputLabel = new Label("Below is a list of your friends.");
namesInput = new TextField(35);
submitButton = new Button("Submit");
quitButton = new Button("Quit");
add(titleLabel);
add(noticeLabelA);
add(noticeLabelB);
add(namesInputLabel);
add(namesInput);
add(submitButton);
add(quitButton);
submitButton.addActionListener(this);
quitButton.addActionListener(this);
}
public void paint(Graphics g)
{
g.drawLine(20,175,280,175);
while (action=true && countInput<maxInput)
{
listOfClassmates.add(nameInput);
countInput++;
}
g.drawString("Here is a list of your friends",20,200);
//displays the array list contents vertically
for (int i = 0; i < listOfClassmates.size(); i++)
{
// accessing each element in the array list as per index
String s = listOfClassmates.get(i);
g.drawString("*"+ s + ".",yPn);
yPn = yPn + 15;
}
g.drawLine(20,600,600);
}
public void actionPerformed(ActionEvent e)
{
nameInput = namesInput.getText();
// if(e.getActionCommand().equals("Submit"))
// {
//
// nameInput = namesInput.getText();
// }
// else
// {
//
// action = false;
// }
repaint();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)