java中怎样设置窗体背景图片,可以附上代码吗?
2015-04-09 · 知道合伙人互联网行家
关注
展开全部
//不就是在窗口上设置一个背景图片么?怎么那么多不同的声音?
//代码如下 运行就看到结果了,你准备好个图片 放到当前路径里就可以了
import java.util.*;
import javax.swing.*;
public class Test extends JFrame {
private JPanel pan;
private JLabel labName;
private JTextField tfName;
private JLabel labPass;
private JPasswordField tfPass;
private JButton butConfirm;
private ImageIcon ii;
private JLabel lab;
Test(){
this.setBounds(200, 200, 300, 200);
pan = new JPanel();
labName = new JLabel("用户名");
tfName = new JTextField(20);
labPass = new JLabel("密 码");
tfPass = new JPasswordField(20);
butConfirm = new JButton("SAVE");
pan.add(labName);
pan.add(tfName);
pan.add(labPass);
pan.add(tfPass);
pan.add(butConfirm);
ii = new ImageIcon("1.jpg");//你只需要把这里的图片1.jpg给改了就可以了
lab = new JLabel(ii);
lab.setBounds(0, 0,ii.getIconWidth(), ii.getIconHeight());
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(lab, new Integer(Integer.MIN_VALUE));
this.setContentPane(pan);
pan.setOpaque(false);
this.setVisible(true);
}
public static void main(String args[]){
new Test();
}
}
//代码如下 运行就看到结果了,你准备好个图片 放到当前路径里就可以了
import java.util.*;
import javax.swing.*;
public class Test extends JFrame {
private JPanel pan;
private JLabel labName;
private JTextField tfName;
private JLabel labPass;
private JPasswordField tfPass;
private JButton butConfirm;
private ImageIcon ii;
private JLabel lab;
Test(){
this.setBounds(200, 200, 300, 200);
pan = new JPanel();
labName = new JLabel("用户名");
tfName = new JTextField(20);
labPass = new JLabel("密 码");
tfPass = new JPasswordField(20);
butConfirm = new JButton("SAVE");
pan.add(labName);
pan.add(tfName);
pan.add(labPass);
pan.add(tfPass);
pan.add(butConfirm);
ii = new ImageIcon("1.jpg");//你只需要把这里的图片1.jpg给改了就可以了
lab = new JLabel(ii);
lab.setBounds(0, 0,ii.getIconWidth(), ii.getIconHeight());
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(lab, new Integer(Integer.MIN_VALUE));
this.setContentPane(pan);
pan.setOpaque(false);
this.setVisible(true);
}
public static void main(String args[]){
new Test();
}
}
展开全部
参考代码:
import java.util.*;
import javax.swing.*;
public class Test extends JFrame
{
private JPanel pan;
private ImageIcon ii;
private JLabel lab;
public Test()
{
this.setBounds(200, 200, 400, 300);
pan = new JPanel();
ii = new ImageIcon("1.png"); //把这里的图片1.png改成要设置的图片名即可
lab = new JLabel(ii);
lab.setBounds(0, 0,ii.getIconWidth(), ii.getIconHeight());
this.getLayeredPane().add(lab, new Integer(Integer.MIN_VALUE));
this.setContentPane(pan);
pan.setOpaque(false);
this.setVisible(true);
}
public static void main(String args[])
{
new Test();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询