问题
I have been set an assignment and I have been working on this applet for days now, trying to figure out a solution myself but no amount of searching has brought up an answer which I can find to fit quite what I need. The problem is I need to create a java applet which tell you how many words of a certain length there are. So if I type "Hi There" it will say: 1 word of length 2 1 word of length 5 I am using g.DrawString to output the result of the entered text. If I enter more than one word and all the words entered are the same length it will output one line with the correct information. If I enter two words of different lengths however it will still only output one line and totally ignore anything before the last word entered. I just can't seem to figure out how to get g.Drawstring to move down a line. Here is the code I have so far:
import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class assignmentneat extends Applet implements ActionListener {
String pr_name;
TextField pr_input;
public void init()
{
pr_input = new TextField(50);
add(pr_input);
pr_input.addActionListener(this);
}
public void start()
{
pr_name = " ";
}
public void actionPerformed(ActionEvent e)
{
int a = 0;
int b;
pr_name = e.getActionCommand();
String[] words = pr_name.split(" ");
for (String word : words)
if (a < word.length())
a = word.length();
int pr_count[] = new int[a+1];
for (String word : words) {
pr_count[word.length()]++; }
for (b = 0; b < pr_count.length; b++){
if (pr_count[b] > 0) {
pr_name = ("There are " + pr_count[b] + " words of length " + b);
repaint();
}
}
}
public void paint(Graphics g)
{
g.drawString(pr_name,100,100);
}
}
The whole program works perfectly when its just run inside something like DrJava, it just doesn't want to work when it's in applet form. update I should mention, I realise using a Jlabel etc would be easier, but I haven't been taught anything to do with this, I've only been studying java for a very very short time and I don't want to use anything I haven't been taught so far.
回答1:
What you want to do is have List
of Strings.
Hopefully you have learned about Lists
, you can use arrays but adding to them dynamically sucks.
Using a list you could do something like this in your last loop
public class assignment... {
private List<String> list = new ArrayList<>();
....
public void actionPerfomed(ActionEvent e) {
....
// instead of this
//for (b = 0; b < pr_count.length; b++){
//if (pr_count[b] > 0) {
//pr_name = ("There are " + pr_count[b] + " words of length " + b);
//repaint();
//}
// do this
for (b = 0; b < pr_count.length; b++){
if (pr_count[b] > 0) {
list.add("There are " + pr_count[b] + " words of length " + b);
}
}
repaint(); // repaint after all is added to the list.
}
}
In your paint
method you could then loop through the list. What you need to do though, for each line, you need to update the y
position since its moving to the next line. Since you can't use things you haven't learned, I won't suggest FontMetrics
which lets you measure the height of letters. Instead just guess the height and increment the y
for each line. Something like this
public void paint(Graphics g) {
int y = 20;
for (String s : list) {
g.drawString(s, 20, y);
y += 15; // 15 is just a guess. Play with it til you get it right
}
}
来源:https://stackoverflow.com/questions/22194381/printing-g-drawstring-on-a-new-line-when-for-loop-is-run