如何调用Applet的扩展类中的paint方法? [英] How to call a paint method inside Applet extended class?
问题描述
我有2类文件名为PaintMe.java和Starter.java。
PaintMe.java包括:
进口java.applet.Applet中;
进口java.awt中的*。公共类PaintMe扩展的Applet {
公共无效漆(图形G){
g.setColor(Color.red);
g.drawString(HELLOOO,15,25);
}
}
Starter.java包括:
进口java.applet.Applet中;
进口java.awt.Graphics;
公共类起动器{
公共静态无效的主要(字串[] args){
PaintMe圈=新PaintMe();
ring.paint();
}
}
所以问题是,我怎么能画我的字符串调用从Starter.java油漆的方法?
要得到它来编译,更改
ring.paint();
...到...
ring.repaint();
注释
- 请不要在这千年的使用AWT code。使用Swing(它提供了一个
JApplet的
)。 - 请不要开始从applet的
主(字符串[])
。 Applet是由JRE启动时,它被嵌入在一个网页(或推出了采用 JWS )。的GUI可以在一个面板进行设计,即再放入一个自由浮动的应用程序或小应用程序。即称为混合。但无论框架和小程序分别添加了图形用户界面,这是(最常见),不同的类要么。 - ,因为它的存在主要是没用的。除非小程序被添加到一个容器中,可见,在code将成功运行,但在稍后结束而不显示任何内容。
更新1
..试过了,但它仍然没有吸取我的字符串中的小程序窗口。
块引用>试试这个。
来源
//<小程序code ='PaintMeWIDTH = 300 HEIGHT = 50>< /小程序>
进口java.applet.Applet中;
进口java.awt中的*。公共类PaintMe扩展的Applet {
公共无效漆(图形G){
g.setColor(Color.red);
g.drawString(HELLOOO,15,25);
}
}提示
> javac的PaintMe.java
> appletviewer中PaintMe.java屏幕截图
更新2
..我需要把它从Starter.java类开始。
块引用>我认为这是一个愚蠢的要求,好像JWS(如前所述&放大器;注释中的链接)发起,查看这个GUI一个
的JFrame
是最好的方式。 OTOH,这里是一个(非常)天真实施起动机
类,它会显示在屏幕上的小程序。它混合AWT和Swing(坏),它并不试图实施任何形式的applet上下文的,并且不调用该applet
的init
/启动
/停止
/摧毁
方法,但也足以让applet的导通屏幕从其它类。
进口java.awt.Dimension中;
进口javax.swing.JOptionPane中;公共类起动器{
公共静态无效的主要(字串[] args){
PaintMe圈=新PaintMe();
ring.set preferredSize(新尺寸(250,30));
JOptionPane.showMessageDialog(NULL,环);
}
}I have 2 class files called PaintMe.java and Starter.java. PaintMe.java contains:
import java.applet.Applet; import java.awt.*; public class PaintMe extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("HELLOOO", 15, 25); } }
Starter.java contains:
import java.applet.Applet; import java.awt.Graphics; public class Starter { public static void main(String[] args) { PaintMe ring = new PaintMe(); ring.paint(); } }
So question is, how can I paint my string with calling a paint method from Starter.java?
解决方案To get it to compile, change
ring.paint();
..to..
ring.repaint();
Notes
- Don't code using AWT in this millennium. Use Swing (which offers a
JApplet
).- Don't start an applet from the
main(String[])
. An applet is started by the JRE when it is embedded in a web page (or launched using JWS). A GUI can be designed in a panel, that is then put in a free-floating application or applet. That is known as a hybrid. But both frame and applet separately add the GUI, which is (most often) a different class to either.- The main as it exists is useless. Unless the applet is added to a container and made visible, the code will run successfully but end in a few moments without displaying anything.
Update 1
..tried that, but it still doesn't draw my string in the applet window.
Try this.
Source
// <applet code='PaintMe' width=300 height=50></applet> import java.applet.Applet; import java.awt.*; public class PaintMe extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("HELLOOO", 15, 25); } }
Prompt
> javac PaintMe.java > appletviewer PaintMe.java
Screenshot
Update 2
..I need to have it started from Starter.java class.
I think that is a silly requirement, and it seems like JWS (as mentioned & linked in comments) launching a
JFrame
is the best way to view this GUI. OTOH, here is a (very) naive implementation of theStarter
class that will show that applet on-screen.It mixes AWT and Swing (bad), it does not attempt to implement any sort of applet context, and does not call the applet
init
/start
/stop
/destroy
methods, but is enough to get the applet on-screen from another class.import java.awt.Dimension; import javax.swing.JOptionPane; public class Starter { public static void main(String[] args) { PaintMe ring = new PaintMe(); ring.setPreferredSize(new Dimension(250,30)); JOptionPane.showMessageDialog(null, ring); } }
这篇关于如何调用Applet的扩展类中的paint方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!