在新的一行打印g.Drawstring for循环中运行时? [英] Printing g.Drawstring on a new line when for loop is run?
问题描述
我已经设置了分配,我一直在这个小程序天了,试图找出一个解决方案,但我自己没有搜索量造就了一个答案,我可以找到适合完全是我所需要的。
问题是我需要创建一个Java小程序,告诉你一定长度的多的话怎么也有。所以,如果我输入你好,它会说:
长度为2的1个字
长度为5的1个字
我使用g.DrawString输出输入文本的结果。如果我输入多个单词,所有输入的单词的长度相同它将输出一行正确的信息。如果我输入不同长度的两句话但它仍然只输出一行,完全将忽略输入的最后一个字之前任何东西。我似乎无法弄清楚如何获得g.Drawstring向下移动一行。这里是code我到目前为止有:
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);
}
}
整个程序完美的作品时,它只是里面像DrJava运行,它只是不希望,当它在小程序的形式工作。
的更新的
我要指出,我认识到使用一个JLabel等会更容易些,但我没有被教导什么做这个,我只是一直在学习Java的一个非常非常短的时间,我不希望使用任何我尚未至今任教。
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.
推荐答案
您想要做的是有列表
字符串。
希望你已经了解了列表
,你可以使用数组但加入他们动态吸。
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.
}
}
在你的油漆
方法,你可以再通过列表循环。你需要做虽然什么,每一行,你需要更新是
位置自其移动到下一行。既然你不能使用你还没有学到的东西,我不会建议的FontMetrics
它可以让你衡量字母的高度。而不是仅仅猜测的高度和递增是
的每一行。事情是这样的。
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
}
}
这篇关于在新的一行打印g.Drawstring for循环中运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!