我的头好像被捆住了。我是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 + ".", 20, yPn);
yPn = yPn + 15;
}
g.drawLine(20, 600, 280, 600);
}
public void actionPerformed(ActionEvent e)
{
nameInput = namesInput.getText();
// if(e.getActionCommand().equals("Submit"))
// {
//
// nameInput = namesInput.getText();
// }
// else
// {
//
// action = false;
// }
repaint();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!