我想知道如何将图像设置为Java应用程序的背景。我知道在android中它非常直接,而windows builder pro有很多令人惊叹的工具来构建Java gui,所以想知道我是否有办法做到这一点?提前致谢!我的应用程序看起来灰很糟糕......如何在eclipse或windows builder pro中将Java应用程序中的图像设置为背景
回答
你可以通过调用设置您的组件的颜色:
.setBackground(myColor);
一些组件如Jlabel之下要求你在它的颜色更改生效称之为:
.setOpaque(true);
希望这对我有所帮助。
他的问题的标题包含单词“图像”,所以我不认为你的答案解决了他的问题。 – 2012-01-07 22:51:12
您不能准确地将背景设置为图像。你需要做的是在绘画过程中在图形上绘制图像。所以你需要继承JPanel并覆盖paintComponent()方法,并在那里绘制图像。
public class ImagePanel extends JPanel {
private Image bgImage;
public Image getBackgroundImage() {
return this.bgImage;
}
public void setBackgroundImage(Image image) {
this.bgImage = image;
}
protected paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, bgImage.getWidth(null), bgImage.getHeight(null), null);
}
}
是的,这是谷歌会告诉他,如果原来的海报烦恼投入努力。 1+。要么是这样做,要么在使用JLabel作为contentPane后使其不透明,给它一个体面的布局管理器,并为其提供一个带有所需图像的ImageIcon。 – 2012-01-07 23:09:07
感谢chubbard的回应! paintComponent方法的第二行中的图像应该是bgImage?如果我将它更改为bgImage,我会得到一个提示,指出getWidth()和getHeight()需要一个ImageObserver作为它们的参数 - 我该如何放置它?另外,我该如何设置bgImage?谢谢 – user1058210 2012-01-07 23:10:07
只需传递null即可,因为您预计图像在绘制之前已经被加载。 – chubbsondubs 2012-01-07 23:22:50
从技术上讲,你可以在整个屏幕上,然后在图标选项CHANE特定背景添加标签:
JLabel lblNewLabel = new JLabel("n");
lblNewLabel.setIcon(new ImageIcon("gene.jpg"));
lblNewLabel.setBounds(0, 0, 434, 261);
frame.getContentPane().add(lblNewLabel);
例如,在源
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Let's start!");
btnNewButton.setFont(new Font("David", Font.ITALIC, 12));
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "Now we will have to add some code here right =]?");
}
}
);
btnNewButton.setBounds(158, 120, 89, 23);
frame.getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("n");
lblNewLabel.setIcon(new ImageIcon("gene.jpg"));
lblNewLabel.setBounds(0, 0, 434, 261);
frame.getContentPane().add(lblNewLabel);
}
- 1. 将图像设置为Android应用程序中的背景
- 2. 如何在Windows Store应用程序中将应用程序的背景全局设置为图像?
- 3. 如何使用Interface Builder在视图中设置背景图像
- 4. 在Java中设置背景图像
- 5. Windows Phone应用程序背景图像
- 6. 如何在Android中为整个应用程序设置背景?
- 7. 如何显示iPhone应用程序的背景图像或前景图像
- 8. 如何在Delphi XE中将背景图像设置为TListview?
- 9. Java小程序中的背景图像
- 10. 如何在Java的Swing GUI中将图像设置为Frame的背景?
- 11. 如何将背景图像设置为android中的textview?
- 12. 如何在GridLayout中将设置的图像按钮背景设置为透明
- 13. 将背景图像设置为链接?
- 14. 如何将应用程序背景设置为与当前背景相同?
- 15. 如何为linearlyout设置图像背景?
- 16. 将Flash Builder 4.5移动应用程序VGroup背景颜色设置为黑色
- 17. 如何使用引导程序将图像设置为容器的背景
- 18. 将背景图像设置为文字?
- 19. 在Java Swing应用程序中显示背景图像
- 20. 如何在Wordpress中将背景图像设置为张贴特色图像
- 21. 设置Android应用程序背景图像
- 22. 如何在城域应用程序中设置页面背景
- 23. 如何在Windows Phone应用程序的项目模板中设置一个堆栈面板的背景图像
- 24. 从应用程序设置iPhone的背景图像
- 25. 如何在RAP应用程序中添加背景图像?
- 26. 在Java Breakout游戏中为JPanel设置背景图像
- 27. 如何将图像设置为图形的背景?
- 28. 为UIButton设置一个背景图像
- 29. 设置一个随机应用程序背景图像iOS 6
- 30. 使用WriteableBitMapExWPhone将WriteableBitmap图像设置为windows phone 7中的背景
你目前使用的是什么GUI库?摇摆?或者,这仍然是你问的Android?你应该为图书馆添加一个标签。 – 2012-01-07 22:36:20
对不起,我正在使用swing组件(JPanel,JFrame等) – user1058210 2012-01-07 22:42:23
然后写了很多关于如何为Swing应用程序制作背景图片的文章。我很惊讶谷歌在这方面没有帮助你。 – 2012-01-07 22:45:44