问题
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