我给一个Jpanel类重绘了背景。
然后再frame上添加这个对象,显示正常。
但是在这个Jpanel类里面添加新的组件的时候,添加一个button重绘的背景就没了。。
感觉这不合逻辑,但是真的没找到错。
重绘背景的Jpanel类,其中 BackgroundPanel继承Jpanel,在BackgroundPanel类中重写了paintComponent方法。public class MainPanel extends BackgroundPanel{ private static final long serialVersionUID = 2752472217575142298L;
private UpdateSizePanel updateSizePanel;//修改图片尺寸的panel
private CompressionSpacePanel compressionSpacePanel;//压缩图片大小的panel
private MoveImgPanel moveImgPanel;//移动图片位置的panel
private UpdateSizeBtn updateSizeBtn;//进入修改图片尺寸的button
private CompressionSpaceBtn compressionSpaceBtn;//进入压缩图片大小的button
private MoveImgBtn moveImgBtn;//进入移动图片位置的button
public MainPanel(){
super();
setVisible(true);
setLayout(null);
init();
}
public void init(){
// updateSizePanel=new UpdateSizePanel();
// compressionSpacePanel=new CompressionSpacePanel();
// moveImgPanel=new MoveImgPanel();
// this.add(updateSizePanel);
// this.add(compressionSpacePanel);
// this.add(moveImgPanel);

updateSizeBtn=new UpdateSizeBtn();
// compressionSpaceBtn=new CompressionSpaceBtn("压缩图片");
// moveImgBtn=new MoveImgBtn();

this.add(updateSizeBtn);//这里如果选择添加该按钮,背景就没有了。
// this.add(compressionSpaceBtn);
// this.add(moveImgBtn);
}
按钮类:
继承BackgroundButton类,该类给每个按钮都添加了背景,使用的是super(Icon);
public class UpdateSizeBtn extends BackgroundButton{ private static final long serialVersionUID = -4874921281587699812L;
private JLabel label;
public UpdateSizeBtn(){
super();
setBounds(50, 100, 100, 30);
setLayout(null);
init();
}
private void init(){
label=new JLabel();
label.setBounds(0, 0, 100, 30);
label.setText("  修改尺寸");
this.add(label);

}
}

解决方案 »

  1.   

    JPanel背景类。public class BackgroundPanel extends JPanel{ private static final long serialVersionUID = -4716216260810781252L;
    protected Image image; public BackgroundPanel() {
    setBounds(0, 0, 400, 400);
    setBackground();
    } public void setBackground() {
    setImage(Toolkit.getDefaultToolkit().getImage("src"+File.separator+"image"+File.separator+"xiao.jpg"));
    } public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.WHITE);
    if (image != null) {
    int height = image.getHeight(this);
    int width = image.getWidth(this); if (height != -1 && height > getHeight())
    height = getHeight(); if (width != -1 && width > getWidth())
    width = getWidth();
    int x = (int) (((double) (getWidth() - width)) / 2.0);
    int y = (int) (((double) (getHeight() - height)) / 2.0);
    g.drawImage(image, x, y, width, height, this);
    }
    } public Image getImage() {
    return image;
    } public void setImage(Image image) {
    this.image = image;
    }

    }
      

  2.   

    我特意试了一下,背景+ 带背景的按钮没发现你说的问题。import javax.swing.*;
    import java.awt.*;
    import javax.imageio.*;
    import java.io.*;
    import java.awt.image.*;class BkButton extends JButton
    {
    String caption;
    BufferedImage img = null;
    {
    try
    {
    img = ImageIO.read(new File("c:/2.jpg"));
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    } BkButton(String s)
    {
    super(s);
    caption = s;
    } @Override
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawImage(img,0,0,img.getWidth(),img.getHeight(),null);
    g.setColor(Color.YELLOW);
    g.drawString(caption,20,20);
    }
    }class BkPanel extends JPanel
    {
    BufferedImage img = null;
    {
    try
    {
    img = ImageIO.read(new File("c:/1.jpg"));
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    } @Override
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawImage(img,0,0,img.getWidth(),img.getHeight(),null);
    }}public class BkGroundTest extends JFrame
    {
    JPanel panel;
    BkGroundTest()
    {
    panel = new BkPanel();
    add(panel); JButton btn = new BkButton("button");
    panel.add(btn);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(0,0,800,600);
    setVisible(true);

    } public static void main(String[] args) 
    {
    new BkGroundTest();
    System.out.println("Hello World!");
    }
    }
      

  3.   

    我这里纠结的就是一旦new一个对象出来。背景就没了。。
    住Frame里面我改了下,
    改成
    public MainFrame() {
    setBounds(200, 200, 400, 400);
    setResizable(false);//页面大小不可更改
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//页面关闭后程序关闭
    setVisible(true);
    init();
    }
    private void init(){
    mainpanel=new MainPanel();
    //UpdateSizeBtn updateSizeBtn=new UpdateSizeBtn();
    //mainpanel.add(updateSizeBtn);
    add(mainpanel);
    }
    多次测试后发现,一旦实例化UpdateSizeBtn这个对象,背景就没了。public class UpdateSizeBtn extends BackgroundButton{ private static final long serialVersionUID = -4874921281587699812L;
    private JLabel label;
    public UpdateSizeBtn(){
    super();
    setBounds(50, 100, 100, 30);
    setLayout(null);
    init();
    }
    private void init(){
    label=new JLabel();
    label.setBounds(0, 0, 100, 30);
    label.setText("  修改尺寸");
    this.add(label);

    }
    }
    public class BackgroundButton extends JButton{ private static final long serialVersionUID = -4699295098670269297L;
    private static ImageIcon button=new ImageIcon(BackgroundButton.class.getResource("/image/button.jpg"));
    public BackgroundButton(){
    super(button);
    }
      

  4.   

    我测试了你的 UpdateSizeBtn 结果是,添加这个按钮后,Panel的背景还在,按钮看不到。解决办法是,就可以看到带图片和文字的 JButton
         public UpdateSizeBtn(){
            super();
            setBounds(50, 100, 100, 30);
            //这句有问题。
            //setLayout(null);
            init();
        }
    完整源码如下:
    import javax.swing.*;
    import java.awt.*;
    import javax.imageio.*;
    import java.io.*;
    import java.awt.image.*; class BackgroundButton extends JButton
    {
     
        private static final long serialVersionUID = -4699295098670269297L;
        private static ImageIcon button= new ImageIcon("c:/test2.jpg");
    //new ImageIcon(BackgroundButton.class.getResource("c:/test2.jpg"));
        public BackgroundButton(){

            super(button);

    System.out.println("aaaa");
        }
    } class UpdateSizeBtn extends BackgroundButton{
     
        private static final long serialVersionUID = -4874921281587699812L;
        private JLabel label;
        public UpdateSizeBtn(){
            super();
            setBounds(50, 100, 100, 30);
            //setLayout(null);
            init();
        }
        private void init(){
            label=new JLabel();
            label.setBounds(0, 0, getWidth(), getHeight());
            label.setText("  修改尺寸");
            add(label);
             
        }
    }class BkButton extends JButton
    {
    String caption;
    BufferedImage img = null;
    {
    try
    {
    img = ImageIO.read(new File("c:/2.jpg"));
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    } BkButton(String s)
    {
    super(s);
    caption = s;
    } @Override
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawImage(img,0,0,img.getWidth(),img.getHeight(),null);
    g.setColor(Color.YELLOW);
    g.drawString(caption,20,20);
    }
    }class BkPanel extends JPanel
    {
    BufferedImage img = null;
    {
    try
    {
    img = ImageIO.read(new File("c:/1.jpg"));
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    } @Override
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawImage(img,0,0,img.getWidth(),img.getHeight(),null);
    }}public class BkGroundTest extends JFrame
    {
    JPanel panel;
    BkGroundTest()
    {
    panel = new BkPanel();
    add(panel); JButton btn = new BkButton("button");
    panel.add(btn);
    btn.add(new JLabel("buutonaa")); UpdateSizeBtn btn2 = new UpdateSizeBtn();
    panel.add(btn2);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(0,0,800,600);
    setVisible(true);
    } public static void main(String[] args) 
    {
    new BkGroundTest();
    System.out.println("Hello World!");
    }
    }