Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java小程序和控制台_Java_Applet - Fatal编程技术网

Java小程序和控制台

Java小程序和控制台,java,applet,Java,Applet,我是Java小程序的新手 我想在Java小程序中显示它在我的控制台中的显示,我知道应该使用paint方法,但我不知道如何在代码中调用它 public void paint(Graphics g) { //Draw a rectangle width=250, height=100 g.drawRect(0,0,250,100); //Set the color to blue g.setColor(Col

我是Java小程序的新手 我想在Java小程序中显示它在我的控制台中的显示,我知道应该使用paint方法,但我不知道如何在代码中调用它

public void paint(Graphics g) {        
    //Draw a rectangle width=250, height=100       
    g.drawRect(0,0,250,100);         
    //Set the color to blue      
    g.setColor(Color.blue);         
    //Write the message to the web page       
    g.drawString("Running",10,50);   
这就是我现在拥有的,整个程序都是在控制台上运行的

System.out.println(x)
还有什么可以称之为

paint(x)

要在小程序上显示与控制台上显示相同的内容?另外,我需要它不断刷新。

您没有对超类方法的调用。放

super.paint(g);
在其他事情之前

如果希望在控制台上同时在小程序上显示消息,则需要有一个调用repaint()的方法;。我设置它的方法是使用字符串的Arraylist。当您希望将某些内容写入小程序输出时,可以将新字符串添加到ArrayList,然后调用repaint()。然后,在paint()中


最后,我质疑这条路线是否真的最适合你想要实现的目标。我建议您研究一下文本字段和滚动条,看看它们是否更适合您的参数。

在小程序上显示是什么意思?你的意思是让swing应用程序在窗体或面板上显示文本吗?1)用小写字母键入的单词很难阅读,就像试图听别人喃喃自语一样。请在句子的开头使用大写字母表示单词I,并使用诸如
ArrayList
或Oracle之类的专有名称。2) 为什么要编写小程序?如果是老师指定的,请参考。还有其他方法吗?我必须构建一个html来调用java函数并显示结果,我真的不知道其他方法
ArrayList<String> ar = new ArrayList();
public void paint(Graphics g) {   
    super.paint();
    //Draw a rectangle width=250, height=100       
    g.drawRect(0,0,250,100);         
    //Set the color to blue      
    g.setColor(Color.blue);         
    //Write the message to the web page
    for(int i = 0; i<ar.size(); i++)
        g.drawString(ar.get(i), 10, 50); }
public void appletOut.println(String str){
   ar.add(str);
   repaint();
}