问题描述
我有 2 个名为 PaintMe.java 和 Starter.java 的类文件.PaintMe.java 包含:
import java.applet.Applet;导入 java.awt.*;公共类 PaintMe 扩展 Applet {公共无效油漆(图形g){g.setColor(Color.red);g.drawString("你好", 15, 25);}}
Starter.java 包含:
import java.applet.Applet;导入 java.awt.Graphics;公共类初学者{公共静态无效主(字符串 [] args){PaintMe ring = new PaintMe();环.paint();}}
那么问题是,如何通过从 Starter.java 调用绘制方法来绘制字符串?
要编译,请更改
ring.paint();
...到...
ring.repaint();
注意事项
- 在这个千禧年不要使用 AWT 进行编码.使用 Swing(它提供了一个
JApplet
).
不要从
main(String[])
启动小程序.小程序在嵌入网页时由 JRE 启动(或使用 JWS 启动).可以在面板中设计 GUI,然后将其放入自由浮动的应用程序或小程序中.这被称为混合动力车.但是框架和小程序都分别添加了 GUI,这(通常)是不同的类.现有的主要是无用的.除非将小程序添加到容器中并使其可见,否则代码会成功运行,但会在片刻结束时不显示任何内容.
更新 1
..试过了,但它仍然没有在小程序窗口中绘制我的字符串.
试试这个.
来源
//导入 java.applet.Applet;导入 java.awt.*;公共类 PaintMe 扩展 Applet {公共无效油漆(图形g){g.setColor(Color.red);g.drawString("你好", 15, 25);}}
提示
>javac PaintMe.java>小程序查看器 PaintMe.java
截图
更新 2
..我需要从 Starter.java 类启动它.
我认为这是一个愚蠢的要求,似乎 JWS(如在评论中提到和链接)启动
JFrame
是查看此 GUI 的最佳方式.OTOH,这是 Starter
类的(非常)幼稚的实现,它将在屏幕上显示该小程序.
它混合了 AWT 和 Swing(不好),它不尝试实现任何类型的小程序上下文,也不调用小程序
init
/start
/stop
/destroy
方法,但足以从另一个类在屏幕上获取小程序.
import java.awt.Dimension;导入 javax.swing.JOptionPane;公共类初学者{公共静态无效主(字符串 [] args){PaintMe ring = new PaintMe();ring.setPreferredSize(new Dimension(250,30));JOptionPane.showMessageDialog(null, ring);}}
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
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 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 the Starter
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 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!