【发布时间】:2014-02-17 15:49:33
【问题描述】:
我有一个包含ImageIcon 的滚动面板。我想在 px 中添加绝对位置的JLabel。我有什么:
代码:
public class HelloWorld {
HelloWorld() {
JFrame jfrm = new JFrame("Hello, World!");
jfrm.setSize(640, 480);
JPanel mapPanel = new JPanel();
mapPanel.setLayout(null);
mapPanel.setSize(600,400);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon imageIcon = new ImageIcon("src\\SwingExp\\img\\map.gif");
JScrollPane scrollPane = new JScrollPane(new JLabel(imageIcon));
scrollPane.setBounds(5,5,600,400);
JLabel usaLabel = new JLabel("U.S.A.");
usaLabel.setBounds(210,200,50,25);
mapPanel.add(usaLabel);
mapPanel.add(scrollPane);
jfrm.add(mapPanel);
jfrm.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new HelloWorld();
}
});
}
}
但如果我稍微滚动一下,美国标签将从地图上消失:
JLabel 位置必须是绝对的,如您所见。如何避免这种情况?
【问题讨论】:
标签: java swing layout jscrollpane jlabel