在网上也找了许多关于swing背景图片插入的方法,但是发现大多只有一个JPanel,而我的程序中整个界面有几个panel组成,按照网上的方法做了之后,就出现了很多问题
问题1:背景色不显示
问题2:背景色覆盖所有组件
问题3:背景色有了但是排版出现问题我想肯定有方法能够解决多panel的背景图片插入问题。以下是我的代码:
package com.ebelter.health.home;import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;import com.ebelter.health.test.MyPanel;/**
 * 主页界面
 * 
 * @author ASUS
 * 
 */
public class Home extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;
GridBagLayout gbl;
GridBagConstraints c;
JButton button;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JFrame frame = new JFrame();
JPanel panelNorth = new JPanel();
JPanel panelCenter = new JPanel();
JPanel panel = new JPanel();
JLabel label;
private ImageIcon ii;
private JLabel lab;// Image image = new ImageIcon("images/homeBack.jpg").getImage();
// ImageIcon labIma = new ImageIcon("images/homeBack.jpg"); /**
 * @param args
 */ public Home() {
//背景图片插入
ii = new ImageIcon("images/homeBack.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(panel);
panel.setOpaque(false);


this.initCenter(panelCenter);
this.initNorth(panelNorth); this.setSize(600, 600);
panel.setLayout(new BorderLayout());
panel.add(panelNorth, BorderLayout.NORTH);
panel.add(panelCenter, BorderLayout.CENTER);
this.setContentPane(panel); this.setTitle("Belter");
this.setVisible(true); }

// public static void setBackgroundImage(JFrame frame, ImageIcon img) {   
//     JLabel imgLabel = new JLabel(img);   
//     imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());   
//        
//     // 下面函数的参数必须为Integer对象   
//     frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));   
//     Container cp = frame.getContentPane();   
//     ((JPanel)cp).setOpaque(false);  // 设置内容面板透明,使图片显示出来   
// }

public void initBackground(JPanel panel) {


//Image image = new ImageIcon("image/a.jpg").getImage(); //ImageIcon labIma = new ImageIcon("images/homeBack.jpg");

// label = new JLabel();
// label.setIcon(labIma);
// panel.add(label);
// panel = new MyPanel(image);


//Image image = new ImageIcon("images/homeBack.jpg").getImage();
ImageIcon labIma = new ImageIcon("images/homeBack.jpg");
JLabel imgLabel = new JLabel(labIma);
imgLabel.setBounds(0, 0, labIma.getIconWidth(), labIma.getIconHeight());
frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));   
    Container cp = frame.getContentPane();   
    ((JPanel)cp).setOpaque(false);
} public void initNorth(JPanel panel) {



button = new JButton();
ImageIcon belterIcon = new ImageIcon("images/home_belter.jpg");
button.setIcon(belterIcon);
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
panel.setLayout(fl);
panel.add(button);
initBackground(panel);
} public void initCenter(JPanel panel) { JLabel label;
panel.setLayout(new GridBagLayout());
c = new GridBagConstraints(); button = new JButton();
ImageIcon weightIcon = new ImageIcon("images/home_weight_test.jpg");
button.setIcon(weightIcon);
c.fill = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridy = 1;
// c.gridwidth = 1;
// c.gridheight = 1;
c.insets = new Insets(10, 10, 10, 10);
panel.add(button, c); button1 = new JButton("体重测试");
c.fill = GridBagConstraints.NONE;
// c.weightx = 0.5;
c.gridx = 0;
c.gridy = 2;
panel.add(button1, c); button1.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
HealthTest htWIndow = new HealthTest();
// Home hm = new Home();
// hm.panel.setVisible(false);
// htWIndow.frame.setVisible(true);
} }
}); button = new JButton();
ImageIcon fatIcon = new ImageIcon("images/home_fat_test.jpg");
button.setIcon(fatIcon);
c.fill = GridBagConstraints.NONE;
// c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button, c); label = new JLabel("健康贴士");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 3;
c.gridheight = 1;
panel.add(label, c); JTextArea textArea = new JTextArea("文本");
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridy = 2;
c.gridwidth = 3;
c.gridheight = 3;
panel.add(textArea, c); button2 = new JButton("脂肪测试");
c.fill = GridBagConstraints.NONE;
// c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button2, c); button2.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }
}); button = new JButton();
ImageIcon pressIcon = new ImageIcon("images/home_press_test.jpg");
button.setIcon(pressIcon);
c.fill = GridBagConstraints.NONE;
// c.weightx = 0.5;
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button, c); button3 = new JButton("血压测试");
c.fill = GridBagConstraints.NONE;
// c.weightx = 0.5;
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button3, c); button3.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub }
}); button = new JButton();
ImageIcon pedometersIcon = new ImageIcon(
"images/home_pedometers_test.jpg");
button.setIcon(pedometersIcon);
c.fill = GridBagConstraints.NONE;
// c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button, c); button4 = new JButton("计步器测试");
c.fill = GridBagConstraints.NONE;
// c.weightx = 0.5;
c.gridx = 1;
c.gridy = 4;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button4, c); button4.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// 实现跳转功能 }
});


} public static void main(String[] args) {
Home homeDemo = new Home();
}}