如何在JPanel中添加一个背景图片,月简单越好
怎么在JPanel中添加一个背景图片,月简单越好
大家好,请问怎么在JPanel中添加一个北京图片,越简单越好。
------解决方案--------------------
好像没有直接的办法, 继承JPanel实现一个BgImagePanel吧, 要不了多少代码
------解决方案--------------------
使用 SwingX 提供的 JXPanel + ImagePainter
------解决方案--------------------
重写paint方法。里面去drawImage(...)
此方法文档上有详细描述。
------解决方案--------------------
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image,0,0,this);
}
------解决方案--------------------
public class PrintImage extends Frame{
/**
*
*/
private static final long serialVersionUID = 1564025218977273340L;
/**
* 读取网页图片
*/
private ImageIcon img;
public void paint(Graphics g){
g.drawImage(img.getImage(), 20, 20, this);//显示图像
}
public void processWindowsEvent(WindowEvent e){
//处理windows窗口事件
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
public static void main(String[] args) throws IOException{
String path = "F:\\m_1259201088627.jpg";
PrintImage urlt = new PrintImage();
urlt.img = new ImageIcon(path);
urlt.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
urlt.setSize(600,600);//设定窗口的大小
urlt.setVisible(true);//Shows or hides this Window depending on the value of parameter
}
}
------解决方案--------------------
大家好,请问怎么在JPanel中添加一个北京图片,越简单越好。
------解决方案--------------------
好像没有直接的办法, 继承JPanel实现一个BgImagePanel吧, 要不了多少代码
------解决方案--------------------
使用 SwingX 提供的 JXPanel + ImagePainter
------解决方案--------------------
重写paint方法。里面去drawImage(...)
此方法文档上有详细描述。
------解决方案--------------------
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image,0,0,this);
}
------解决方案--------------------
public class PrintImage extends Frame{
/**
*
*/
private static final long serialVersionUID = 1564025218977273340L;
/**
* 读取网页图片
*/
private ImageIcon img;
public void paint(Graphics g){
g.drawImage(img.getImage(), 20, 20, this);//显示图像
}
public void processWindowsEvent(WindowEvent e){
//处理windows窗口事件
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
public static void main(String[] args) throws IOException{
String path = "F:\\m_1259201088627.jpg";
PrintImage urlt = new PrintImage();
urlt.img = new ImageIcon(path);
urlt.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
urlt.setSize(600,600);//设定窗口的大小
urlt.setVisible(true);//Shows or hides this Window depending on the value of parameter
}
}
------解决方案--------------------
- Java code
/** * <br>Created on 2010-11-11 * @author Alex.Huang */ package ext.test.jws.gui; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** * <br>Created on 2010-11-11 * @author Alex.Huang */ public class PanelImageTest extends JPanel { private Image image; public PanelImageTest() { super(); setOpaque(true); image = Toolkit.getDefaultToolkit().getImage( "D:/My Document/My Pictures/Wallpaper Images/NOAA/line0053.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 static void main(String[] args) { JFrame frame=new JFrame(); PanelImageTest panel=new PanelImageTest(); JButton b=new JButton("按钮"); panel.add(b); frame.add(panel); frame.setVisible(true); } }
------解决方案--------------------
- Java code
import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Background extends JFrame{ ImageIcon image; JLabel imageLabel; JPanel backgroundPane; public Background() { image = new ImageIcon(Background.class.getClassLoader().getResource("images/background.gif")); backgroundPane = new JPanel(); imageLabel = new JLabel(image); backgroundPane.add(imageLabel); this.add(backgroundPane); this.setBounds(200, 300, 381, 330); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) { g.drawImage(image.getImage(), 0, 20, this); } public static void main(String[] args) { Background bg = new Background(); } }