繁体   English   中英

Java Applet和控制台

[英]Java Applet and Console

我是构建Java applet的新手,我想显示它在Java applet的控制台中显示的内容,我知道您应该使用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();的方法。 我设置的方式是拥有一个字符串数组列表。 当您想要向applet输出中写入内容时,可以将新的String添加到ArrayList中,然后调用repaint()。 然后,在paint()中,

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();
}

最后,我怀疑这条路线是否真的最适合您要完成的任务。 我建议研究文本字段和滚动条,看看是否更适合您的参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM