简体   繁体   English

在JApplet中嵌入JPanel(始终在顶部)

[英]Embed JPanel in JApplet (always on top)

I have a hybrid swing application which can be run as applet or java application with the following structure: 我有一个混合的swing应用程序,它可以作为applet或java应用程序运行,结构如下:

public class Gui extends JApplet {

    public void init() {...}

    public void paint(Graphics g) {
       ...
       g.drawImage(image, 0, 0, this);
    }

    public static void main(String args[]) {

       JFrame frame = new JFrame();
       JDialog dialog = new JDialog(frame);

       Gui gui = new Gui();
       gui.init();
       gui.start();

       dialog.add("Center", gui);

       ...
    }

}

The whole gui consists of one drawing area which is continously updated. 整个gui包含一个绘图区域,该区域会不断更新。 I would like to embed a panel which is always on top and independent of the underlying drawing process. 我想嵌入一个面板,该面板始终位于顶部,并且独立于基础绘制过程。

I tried using JLayeredPane and add it between JDialog and JApplet, but Japplet cant be added to the Pane because it is a top level container. 我尝试使用JLayeredPane并将其添加到JDialog和JApplet之间,但是无法将Japplet添加到窗格中,因为它是顶级容器。

I also tried realising it with the glasspane but no success at all. 我也尝试用玻璃板实现它,但没有成功。

Is there a solution without refactoring to much since the structure should be kept as far as possible. 有没有一种解决方案而不需要过多重构,因为应该将结构保持得尽可能远。

Don't override paint() of a top level container, like JApplet or JFrame. 不要覆盖顶级容器(如JApplet或JFrame)的paint()。 Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent). 通过重写JPanel(或JComponent)的paintComponent()方法来完成自定义绘制。 Then you add the panel to the frame. 然后将面板添加到框架。

dialog.add("Center", gui);

The is not the way to add a component to a panel. 这不是将组件添加到面板的方法。 Read the Container API to find the proper add(...) method to use. 阅读容器API,以找到要使用的正确add(...)方法。 Also don't hardcode string values. 也不要硬编码字符串值。 Every layout manager contains variables that can be used as the constraint values. 每个布局管理器都包含可用作约束值的变量。

I would like to embed a panel which is always on top and independent of the underlying drawing process. 我想嵌入一个面板,该面板始终位于顶部,并且独立于基础绘制过程。

Not sure this makes sense. 不确定这是否有意义。 If the panel is always on top, then it would cover the painting. 如果面板始终位于顶部,则它将覆盖绘画。

I tried using JLayeredPane 我尝试使用JLayeredPane

That sounds like the proper approach. 这听起来像是正确的方法。 You add the layered pane to the frame or applet. 您将分层窗格添加到框架或小程序。 Then you can have a background painting panel and another transparent panel on top. 然后,您可以在顶部有一个背景绘画面板和另一个透明面板。 Read the Swing tutorial on Using Layered Panes for a working example. 阅读有关使用分层窗格的Swing教程, 获得一个可行的示例。

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

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