[英]Move a JLabel to Front
I am placing two images. 我正在放置两张图片。 One is supposed to be the background picture and the other one a picture of a stick-figure.
一个应该是背景图片,另一个是棒图的图片。 I'd like to get the stick figure in front of the background.
我想在背景前得到棒图。 I can accomplish this by inserting the code to put the background picture after the code to display the stick figure.
我可以通过插入代码将背景图片放在代码后显示棒图来完成此操作。 I was wondering if there was anyway to accomplish the same thing, by inserting the stick figure code after the background code, so I could keep placing new JLabels on top of the background.
我想知道是否还有通过在背景代码之后插入棒图代码来完成同样的事情,所以我可以继续在背景上放置新的JLabel。
The code that works: 有效的代码:
guy.setBounds(0,0,100,100);
panel.add(guy);
backgroundPic.setBounds(0,0,550,550);
panel.add(backgroundPic);
setVisible(true);
The code that I'd like to work: 我想要的代码:
backgroundPic.setBounds(0,0,550,550);
panel.add(backgroundPic);
guy.setBounds(0,0,100,100);
panel.add(guy);
setVisible(true);
If you are only worrying about one background image, then I would suggest to extend JPanel, override paintComponent and paint the background image. 如果你只是担心一个背景图像,那么我建议扩展JPanel,覆盖paintComponent并绘制背景图像。
If you have to care about the Z-order of several components, then JLayeredPane is your best alternative. 如果你必须关心几个组件的Z顺序,那么JLayeredPane是你最好的选择。
Here is a small snippet showing the first option: 这是一个显示第一个选项的小片段:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test2 {
private static class PanelWithBackground extends JPanel {
private Image backgroundImage;
private Point backgroundLocation = new Point();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (getBackgroundImage() != null) {
g.drawImage(getBackgroundImage(), backgroundLocation.x, backgroundLocation.y, this);
}
}
public Image getBackgroundImage() {
return backgroundImage;
}
public void setBackgroundImage(Image backgroundImage) {
this.backgroundImage = backgroundImage;
repaint();
}
public Point getBackgroundLocation() {
return backgroundLocation;
}
public void setBackgroundLocation(Point backgroundLocation) {
this.backgroundLocation = backgroundLocation;
repaint();
}
}
protected static void initUI() throws MalformedURLException {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PanelWithBackground panelWithBackground = new PanelWithBackground();
panelWithBackground.setLayout(null);
panelWithBackground.setBackgroundImage(new ImageIcon(new URL(
"http://2.bp.blogspot.com/-PqKuKt5mRmc/Tvi-K-4FVVI/AAAAAAAACKg/YwzkME5gGvk/s1600/black+background.jpg")).getImage());
JLabel guy = new JLabel(new ImageIcon(new URL(
"http://www.clipproject.info/Cliparts_Free/Menschen_Free/Clipart-Cartoon-Design-04.gif")));
// Next 2 lines should rather be performed by a LayoutManager
panelWithBackground.setPreferredSize(new Dimension(1024, 768));
guy.setBounds(50, 200, guy.getPreferredSize().width, guy.getPreferredSize().height);
panelWithBackground.add(guy);
frame.add(panelWithBackground);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
initUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
You can use the Z order, to indicate the order the components should be rendered: 您可以使用Z顺序来指示组件应该呈现的顺序:
java.awt.Container.getComponentZOrder(Component)
According documentation: 根据文件:
Returns the z-order index of the component inside the container.
返回容器内组件的z顺序索引。 The higher a component is in the z-order hierarchy, the lower its index.
组件在z顺序层次结构中越高,其索引越低。 The component with the lowest z-order index is painted last, above all other child components.
具有最低z顺序索引的组件最后绘制,高于所有其他子组件。
Parameters: comp the component being queried
参数: comp要查询的组件
Returns: the z-order index of the component;
返回:组件的z顺序索引; otherwise returns -1 if the component is null or doesn't belong to the container
如果组件为null或不属于容器,则返回-1
I wont claim to be a java expert nor will i claim this is correct, just a guess, but try and remove the 我不会声称自己是一个java专家也不会声称这是正确的,只是一个猜测,但尝试删除
.setBounds()
.setBounds()
and if that has no effect, try using 如果没有效果,请尝试使用
.setSize()
.setSize()
best of luck :) 祝你好运:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.