十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
仅仅是给窗口添加背景的话是很简单的,添加上以下语句(自己去添加变量哈):\x0d\x0a\x0d\x0alabel = new JLabel(background); //background为ImageIcon\x0d\x0a// 把标签的大小位置设置为图片刚好填充整个面板 \x0d\x0alabel.setBounds(0, 0, this.getWidth(), this.getHeight());\x0d\x0a//添加图片到frame的第二层(把背景图片添加到分层窗格的最底层作为背景)\x0d\x0athis.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));\x0d\x0a//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明\x0d\x0ajPanel=(JPanel)this.getContentPane();\x0d\x0a//设置透明\x0d\x0ajPanel.setOpaque(false);\x0d\x0a\x0d\x0a然后你上面那个JPanel p也设置成透明就可以了
创新互联自2013年创立以来,是专业互联网技术服务公司,拥有项目成都做网站、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元洛宁做网站,已为上家服务,为洛宁各地企业和个人服务,联系电话:18980820575
首先继承JPanel类,然后重写paintComponent方法
给你个例子,这是重写的方法
public void paintComponent(Graphics gs) {
Graphics2D g = (Graphics2D) gs;
super.paintComponent(g);
//画背景图片
g.drawImage(gameBG.getImage(), 0, 0, 800, 560, this);
}
记住,要先画图片,再画文字,否则会被覆盖
lz 你好
代码修改如下:
(有注释)
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
//自定义一个面板类
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
//绘制一张背景图片 view.jpg是图片的路径 lz自己设定为自己想要添加的图片
Image image = new ImageIcon("view.jpg").getImage();
g.drawImage(image, 0, 0, this);
}
}
public class denglu extends JFrame {
//设置一个含有背景图片的面板
private MyPanel panel;
private JLabel lab_zhanghao = new JLabel("账号:");
private JTextField jta_text = new JTextField();
Font lab = new Font("楷体",1,30);
private JLabel lat_password = new JLabel("密码:");
Font lat = new Font("楷体",1,30);
private JTextField jtb_text = new JTextField();
private JButton btn_register = new JButton("注册");
private JButton btn_land = new JButton("登陆");
Font btn = new Font("黑体",2,30);
public static int pd = 0;
public static String ak1, ak2;
private Container contentPane = this.getContentPane();
public denglu () {
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle("登陆");
this.setResizable(false);
btn_register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pd = 1;
ak1 = jta_text .getText();
ak2 = jtb_text .getText();
String fileName = "E:\\users.dat";
try {
Writer writer = new FileWriter(fileName);
} catch (IOException e1) {
e1.printStackTrace();
}
jta_text.setText("");
jtb_text.setText("");
JOptionPane.showMessageDialog(null,"注册成功! 您的用户名是"+ak1+"密码是"+ ak2);
}
});
btn_land.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(pd==1) {
if(ak1.equals(jta_text.getText()) ak2.equals(jtb_text.getText())) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null,"登陆失败!检查账号密码是否正确",null,
JOptionPane.ERROR_MESSAGE, null);
jta_text .setText("");
jtb_text .setText("");
}
}
}
});
init();
panel = new MyPanel();
//将所有的组件添加到panel面板中
panel.add(lab_zhanghao);
panel.add(lat_password);
panel.add(jta_text);
panel.add(jtb_text);
panel.add(btn_register);
panel.add(btn_land);
panel.setLayout(null);
getContentPane().add(panel);
}
private void init() {
lab_zhanghao.setSize(200,100);
lab_zhanghao. setLocation(50,20);
lab_zhanghao.setFont(lab);
lat_password.setSize(200,100);
lat_password.setLocation(50,80);
lat_password.setFont(lat);
jta_text.setSize(190,30);
jta_text.setLocation(160,55);
jtb_text.setSize(190,30);
jtb_text.setLocation(160,115);
btn_register.setSize(120, 40);
btn_register.setLocation(60, 180);
btn_land.setSize(120, 40);
btn_land.setLocation(230, 180);
btn_register.setFont(btn);
btn_land.setFont(btn);
}
public static void main(String[] args)
throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
new denglu().setVisible(true);
}
}
我这儿的运行效果:
希望能帮助你哈
//不就是在窗口上设置一个背景图片么?怎么那么多不同的声音?
//代码如下 运行就看到结果了,你准备好个图片 放到当前路径里就可以了
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();
}
}