我放置了两个图像。一张应该是背景图片,另一张是棒状图形的图片。我想要背景前面的棒状图。我可以通过插入代码来实现这一点,该代码将背景图片放在代码之后以显示条形图。我想知道是否有什么方法可以完成同样的事情,通过在背景代码之后插入条形图代码,这样我就可以继续在背景上放置新的JLabels。
可以工作的代码:
guy.setBounds(0,0,100,100);
panel.add(guy);
backgroundPic.setBounds(0,0,550,550);
panel.add(backgroundPic);
setVisible(true);我想要运行的代码:
backgroundPic.setBounds(0,0,550,550);
panel.add(backgroundPic);
guy.setBounds(0,0,100,100);
panel.add(guy);
setVisible(true);发布于 2012-06-09 21:12:52
如果你只关心一个背景图像,那么我建议扩展JPanel,覆盖paintComponent并绘制背景图像。
如果您必须关心几个组件的Z顺序,那么JLayeredPane是您的最佳选择。
下面是显示第一个选项的一小段代码:
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();
}
}
});
}
}发布于 2012-06-09 20:37:06
可以使用Z顺序来指示组件应呈现的顺序:
java.awt.Container.getComponentZOrder(Component)根据文档:
返回容器内组件的z顺序索引。组件在z顺序层次结构中的位置越高,其索引就越低。具有最低z顺序索引的组件最后绘制,高于所有其他子组件。
参数:comp被查询的组件
返回:组件的z顺序索引;否则,如果组件为空或不属于容器,则返回-1
发布于 2012-06-09 20:38:24
我不会声称自己是java专家,也不会声称这是正确的,只是猜测,但请尝试删除
.setBounds()
如果没有效果,可以尝试使用
.setSize()
祝你好运:)
https://stackoverflow.com/questions/10961023
复制相似问题