Layering Objects (That extend JComponet) on JApplet

坚强是说给别人听的谎言 提交于 2019-11-28 10:28:48

问题


I currently have a JApplet within which I add two objects that both extend JComponet. Object A is basically a large square and object B is a small square, I need Object B to always be in front of Object A, however I cant work out how to set layering within the JApplet to do this. Current I am using the follow code, which adds both the items and displays them how I want however sometimes Object A is front of Object B.

public void init() {
    add(myapplet, BorderLayout.CENTER);
    resize(200, 400);
    B = new Block(Color.green, 10, 10);
    myapplet.add(B);
    A = new Block(Color.red, 100, 100);
    myapplet.add(A);
    myapplet.addMouseListener(this);
    startTimer();
}

回答1:


You might look at JLayeredPane, seen here, or OverlayLayout, seen here. Either should work in an applet, but this hybrid approach may offer additional flexibility.




回答2:


You may want to look at this method: Container.setComponentZOrder(Component comp, int index)



来源:https://stackoverflow.com/questions/13764942/layering-objects-that-extend-jcomponet-on-japplet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!