将照片添加为JFrame背景 [英] Adding a photo as a JFrame background
本文介绍了将照片添加为JFrame背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static void main(String[] args) {
myForm.setSize(500, 400);
myForm.setLocation(0, 0);
myForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myForm.setVisible(true);
// try {
// myForm.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\Heshamm\\Desktop\\light.jpg"))) ) );
//} catch (IOException e1) {
//JOptionPane.showMessageDialog(null, "Error !!!", null, JOptionPane.PLAIN_MESSAGE);
//}
draw.mai(myForm, jp1, button, tf[0], tf[1], tf[2], tf[3]);
这是另一个类中的mai()函数
And this is the mai() function in another class
public void mai(JFrame myForm, JPanel jp1, JButton[] button, JTextField tf1, JTextField tf2, JTextField tf3, JTextField tf4)
{
myForm.getContentPane().removeAll();
jp1.removeAll();
jp1.add(button[0]);
jp1.add(button[1]);
jp1.add(button[2]);
jp1.add(button[3]);
jp1.add(button[9]);
jp1.add(button[4]);
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
Hotel.update();
}
好的,我有这个代码,我希望显示img作为按钮背后的背景,但是当我取消注释试试其中的内容时,框架只显示没有按钮的背景,我该如何解决?
Okay so, I have this code, and I wanted the img to be displayed as a background behind the buttons, but when I uncomment try and whats inside it, the frame only displays the background without the buttons, how can I fix it?
这里是update()函数。
And here's the update() function.
public static void update()
{
cbChoice="";
myForm.add(jp1);
myForm.repaint();
myForm.revalidate();
}
提前致谢。
推荐答案
您可以将图像添加到JLabel并像背景一样拉伸JLabel。
You can add an image to JLabel and strech the JLabel like background.
有一个简单的例子:
ImageIcon icon = createImageIcon("images/middle.gif");
. . .
JLabel label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
label1.setSize(frame width, frame height);
这篇关于将照片添加为JFrame背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文