我试图在JFrame中添加图片和文本。使用JFrame和JLabel
当我添加图片和文字时,只有图片出现。我的猜测是文字隐藏在它后面。当我将try/catch块注释掉时,文本应该出现在中心。任何人都可以帮助我让文字出现在图片前面吗?
我删除了PIC的路径自己,这是没有错的
public class Vindu {
public Vindu(){
JFrame F = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
label.setAlignmentX(0);
label.setAlignmentY(0);
F.add(label);
try{
F.setContentPane(new JLabel (new ImageIcon
(ImageIO.readnewfile))));
}catch (IOException e){
System.out.print("wrong place");
}
F.setResizable(false);
F.setSize(600, 400);
F.setVisible(true);
}
}
这有效,因为我得到的文字和图片,但文本是在右边,而不是在中心。 – mynameishi
上面的代码片段与Java 6一起工作良好。你的环境是什么?你运行的确切代码片段也会有所帮助。 – dimplex