简体   繁体   English

将摆动组件与小程序绘制方法一起使用

[英]Using swing components with an applets paint method

I have a functioning Java applet which can be embedded into a web page and wish to now add some swing components for additional functions. 我有一个可以运行的Java applet,可以将其嵌入到网页中,现在希望添加一些附加组件来实现附加功能。 However whenever I add a component like a JLabel, it simply does not appear on the viewport/canvas unless I remove my entire paint method. 但是,每当我添加诸如JLabel之类的组件时,除非删除整个绘画方法,否则它就不会出现在视口/画布上。 The latter option allows me to add swing components but naturally I then cannot render any shapes. 后一个选项允许我添加挥杆组件,但是自然地我无法渲染任何形状。 It appears to resemble an eXclusive OR (XOR), either one but not the other. 它看起来像一个“异或”(XOR),一个都不是。

Is there anyway in a native Java applet to add swing components and still maintain the paint(Graphics g) method. 无论如何,在本机Java applet中是否有添加摇摆组件并仍然保持paint(Graphics g)方法的方法。 Please note that I am inheriting from Applet and not JApplet. 请注意,我继承自Applet,而不是JApplet。

If you override paint method in applet then there is no simple way. 如果您在applet中覆盖了paint方法,则没有简单的方法。

What you could do instead of overriding paint in applet. 您可以做什么,而不是在applet中覆盖油漆。

  1. Extend JComponent instead and do the custom drawing there. 而是扩展JComponent并在那里进行自定义绘图。
  2. create JPanel that contains all the needed swing components including the component from earlier step. 创建JPanel,其中包含所有必需的swing组件,包括先前步骤中的组件。
  3. Add that panel to applet which is using default paint method. 将该面板添加到使用默认绘制方法的applet中。

I suggest that you add the components to a separate panel that doesn't override paint . 我建议您将组件添加到不覆盖paint的单独面板中。 By overridding paint , you are customizing the way the component is drawn, so the layout manager and the components it has to manage do not count, it's only the implementation of paint you wrote that dictates how stuff is rendered. 通过覆盖paint ,您可以自定义组件的绘制方式,因此布局管理器及其必须管理的组件不计算在内,只有您编写的paint的实现才可以决定填充的方式。

So, your applet would contain a panel with the components and a panel that does the custom painting. 因此,小程序将包含一个包含组件的面板和一个执行自定义绘制的面板。

在paint方法的结尾,调用super.paint(g)

I was able to visualise the components over the applet canvas by invoking 通过调用,我能够可视化applet画布上的组件

paintComponents(g);

Thus the components are thereafter painted AFTER the actual paint(Graphics g) method completes its epoch. 因此,随后在实际的paint(Graphics g)方法完成其时代之后,对组件进行喷涂。

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

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