运行 for 循环时在新行上打印 g.Drawstring? [英] Printing g.Drawstring on a new line when for loop is run?

查看:20
本文介绍了运行 for 循环时在新行上打印 g.Drawstring?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被安排了一个任务,我已经在这个小程序上工作了好几天,试图自己找出一个解决方案,但没有多少搜索提供了一个我能找到的完全符合我需要的答案.问题是我需要创建一个 java 小程序,它告诉你有多少特定长度的单词.所以如果我输入Hi There",它会说:1 字长 21 字长 5我正在使用 g.DrawString 输出输入文本的结果.如果我输入一个以上的单词并且输入的所有单词的长度相同,它将输出一行包含正确信息的信息.如果我输入两个不同长度的单词,它仍然只会输出一行,并且在输入最后一个单词之前完全忽略任何内容.我似乎无法弄清楚如何让 g.Drawstring 向下移动.这是我到目前为止的代码:

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 等会更容易,但我没有被教过与此有关的任何事情,我只学习了很短的时间,我不想使用任何我想要的东西至今没教过.

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.

推荐答案

你想做的是拥有 Strings 的 List. 希望你已经了解了 列表,你可以使用数组,但动态添加它们很糟糕.

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.
    }
}

在您的 paint 方法中,您可以遍历列表.但是你需要做的是,对于每一行,你需要更新 y 位置,因为它移动到下一行.由于你不能使用你没有学到的东西,我不建议使用 FontMetrics 来测量字母的高度.而只是猜测高度并为每一行增加y.像这样

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
    }
}

这篇关于运行 for 循环时在新行上打印 g.Drawstring?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆