JPanel上能添加背景图片吗 JPanel里有个setBackground()方法可以改变JPanel的背景颜色 但是就换下颜色还是很丑陋我想在JPanel上添加个图片作为背景 请教各位高手怎么做啊 小弟菜鸟啊 ~~!!!!!在线等啊 求助各位大哥 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在JLabel上面添加图片,然后将JLabel添加到JPanel中就可以了 我晕这个不是背景图片呀 难道是把JLabel拉到足够大 fill整个面板吗? 那其他的东西不能放了啊 这里有个参考例子import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.ImageIcon;import java.awt.BorderLayout;import java.awt.Graphics;public class Frame1 extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); ImageIcon image=new ImageIcon(Frame1.class.getResource( "a.jpg")); Panel1 jp=new Panel1(image); public Frame1() { try { setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } /** * Component initialization. * * @throws java.lang.Exception */ private void jbInit() throws Exception { contentPane = (JPanel) getContentPane(); contentPane.setLayout(borderLayout1); contentPane.add(jp); setSize(new Dimension(400, 300)); setTitle("Frame Title"); }} class Panel1 extends JPanel { BorderLayout borderLayout1 = new BorderLayout(); private ImageIcon image = null; public Panel1(ImageIcon image) { try { this.image = image; jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(borderLayout1); } protected void paintComponent(Graphics g) { setOpaque(true); super.paintComponent(g); Dimension d = getSize(); for( int x = 0; x < d.width; x += image.getIconWidth() ) for( int y = 0; y < d.height; y += image.getIconHeight() ) g.drawImage( image.getImage(), x, y, null, null ); }} java要加背景图比较麻烦,需要重载paintComponent方法,下面以JPanel为例,其它按此继承就可 JPanel jContentPane = new getpane(800,600,"images\\0044.jpg"); jContentPane.setLayout(null); jContentPane.setOpaque(false);//此处要设为false jContentPane.setAlignmentY(0.5F); jContentPane.add(jLabel, null); jContentPane.add(getJTestYear(), null); this.initComboBox(); jContentPane.add(getJSearchByName(), null); jContentPane.add(getJSearchByID(), null); jContentPane.add(getJIDOrName(), null); jContentPane.add(jLabel1, null); jContentPane.add(getJPanel(), null); this.setContentPane(jContentPane); class getpane extends JPanel{private static final long serialVersionUID = 1L;int width=0,hight=0;String imgpath = "";public getpane(int width,int hight,String file){ this.width = width; this.hight = hight; imgpath = file;}protected void paintComponent(Graphics g) { ImageIcon icon = new ImageIcon(imgpath); Image img = icon.getImage(); g.drawImage(img, 0, 0, width, hight, this);} } Java接口 Im不是抽象的, 并且未覆盖A中的抽象方法doSomething() JCombobox如何实现编辑数据而去检索下拉列表中的值?求租啊 如何拖动JTree的节点名字至JTextArea中 键盘问题 如何接受从命令行输入的数? 有关初始化,付值什么时候有错误? 征求一算法,关于从数据库中得到数据显示问题(在线等待,解决立即给分!不难) 学习使用代理类,严重受挫,请帮忙! 请教一个JTextField问题,现丑了 有人使用NetBeans开发swing程序嘛? 新手求助 新手求救java 问题!!!!!!急!!!!!!!!!!!
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
import java.awt.Graphics;
public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
ImageIcon image=new ImageIcon(Frame1.class.getResource(
"a.jpg"));
Panel1 jp=new Panel1(image);
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
contentPane.add(jp);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
}
}
class Panel1 extends JPanel {
BorderLayout borderLayout1 = new BorderLayout();
private ImageIcon image = null;
public Panel1(ImageIcon image) {
try {
this.image = image;
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
this.setLayout(borderLayout1);
}
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Dimension d = getSize();
for( int x = 0; x < d.width; x += image.getIconWidth() )
for( int y = 0; y < d.height; y += image.getIconHeight() )
g.drawImage( image.getImage(), x, y, null, null );
}
}
JPanel jContentPane = new getpane(800,600,"images\\0044.jpg");
jContentPane.setLayout(null);
jContentPane.setOpaque(false);//此处要设为false
jContentPane.setAlignmentY(0.5F);
jContentPane.add(jLabel, null);
jContentPane.add(getJTestYear(), null);
this.initComboBox();
jContentPane.add(getJSearchByName(), null);
jContentPane.add(getJSearchByID(), null);
jContentPane.add(getJIDOrName(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJPanel(), null);
this.setContentPane(jContentPane);
class getpane extends JPanel{
private static final long serialVersionUID = 1L;
int width=0,hight=0;
String imgpath = "";
public getpane(int width,int hight,String file){
this.width = width;
this.hight = hight;
imgpath = file;
}
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon(imgpath);
Image img = icon.getImage();
g.drawImage(img, 0, 0, width, hight, this);
}
}