JAVA在面板中添加背景图
package SMT.view;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.sql.Statement;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import SMT.model.YH;
import SMT.sql.Sqlconn;
public class MainFrame extends JFrame implements ActionListener {
private JDesktopPane desktopPane;
private JMenuBar menubar;
private JButton btnUser,btnCom,btnPur,btnSale,btnAdd,btnMoney,btnBef,btnExit;
private JPanel imagePanel;
private ImageIcon background;
public MainFrame(){
desktopPane = new JDesktopPane();
menubar = new JMenuBar();
btnUser = new JButton("用户管理");
btnCom = new JButton("仓库管理");
btnPur = new JButton("采购管理");
btnSale = new JButton("销售管理");
btnAdd = new JButton("销售统计");
btnMoney = new JButton("金额统计");
btnBef = new JButton("备份管理");
btnExit = new JButton(" 注 销 ");
menubar.add(btnUser);
menubar.add(btnCom);
menubar.add(btnPur);
menubar.add(btnSale);
menubar.add(btnAdd);
menubar.add(btnMoney);
menubar.add(btnBef);
menubar.add(btnExit);
btnUser.addActionListener(this);
btnCom.addActionListener(this);
btnPur.addActionListener(this);
btnSale.addActionListener(this);
btnAdd.addActionListener(this);
btnMoney.addActionListener(this);
btnBef.addActionListener(this);
btnExit.addActionListener(this);
this.getContentPane().add(desktopPane,BorderLayout.CENTER);
///this.getContentPane().add(toolbar,BorderLayout.NORTH);
this.setJMenuBar(menubar);
this.setSize(686, 500);
this.setBounds(200, 100, 686, 500);
this.setResizable(false);
this.setVisible(true);
this.setTitle("081超市管理系统");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnUser)){
if(YHManage.count==0){
YHManage yhManage=new YHManage();
yhManage.showData();
desktopPane.add(yhManage);
}
else
{JOptionPane.showMessageDialog(null,"该窗口已经打开");}
}
if(e.getSource().equals(btnExit)){
int i=JOptionPane.showConfirmDialog(null, "是否确定退出系统", "提示:", 0,3);
if(i==0){
this.dispose();
}
}
if(e.getSource().equals(btnBef)){
Beifen beifen = new Beifen();
desktopPane.add(beifen);
beifen.toFront();
}
}
}
在这里面添加个背景图片.....急需代码
[解决办法]
建个label标签,用label.setIcon();方法
例子:public static ImageIcon bombIcon = new ImageIcon("images/Bomb.gif");
numberOfUnflagedLabel.setIcon(bombIcon); //设置图标图片
楼主自己试一试