JPanel,JFrame 设置背景图片??? JButton JLabel 上能只设置图片,不要文字方法是 setText()方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 和jButton1.setIcon(new ImageIcon(new java.net.URL("file:///D:/Documents and Settings/My Documents/18.gif"))); 重载paint()方法,我以前贴过怎么写 能够加载背景图了但是出现新问题,加了背景图后,原先定位在JFrame上的控件不能显示,比如JButton JLabel----JButton要在上面点击一下才能显示出来,为什么?? 请看代码:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class applet3 extends JFrame { Image img1; MediaTracker mt; Container contentPanel=this.getContentPane();public applet3() { this.setSize(400,300); this.setResizable(false); this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2); img1=this.getToolkit().getImage("1.jpg"); mt=new MediaTracker(this); mt.addImage(img1,0); ImageIcon icon=new ImageIcon("4.jpg"); contentPanel.setLayout(null); JButton button=new JButton(icon); JLabel label=new JLabel(icon); button.setBounds(0,0,80,30); label.setBounds(90,90,80,50); contentPanel.add(label); contentPanel.add(button); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true);}public void paint(Graphics g) { super.paint(g); g.drawImage(img1,0,0,this.getWidth(),this.getHeight(),this); }public static void main(String args[]) { new applet3();}}出现的效果是:先看到两个控件button label,然后出现背景图片,背景图片将控件覆盖,就只能看见背景图片???不知道怎么解决,还请大虾门多帮忙!!~~~ paint()里顺序写反了,应该是public void paint(Graphics g) { g.drawImage(img1,0,0,this.getWidth(),this.getHeight(),this); super.paint(g);}而且我建议你重载JPanel而不是JFrame,因为JFrame的paint()可能比较复杂,如果其中又重写了背景,那你的drawimage就给覆盖回去了,而JPanel我做过,没有问题 谢谢 Apocalypse(逍遥思辨) 拉~!解决了代码如下:import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class JPanelTemp extends JFrame{ public JPanelTemp() { Container con=this.getContentPane(); con.setLayout(null); ImagePanel panelTemp=new ImagePanel("chess.jpg"); panelTemp.setBounds(100,0,600,500); con.add(panelTemp); this.addWindowListener (new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } }); } public static void main(String args[]) { System.out.println("Starting App"); JPanelTemp f = new JPanelTemp(); f.setBounds(0,0,800,600); f.show(); }}class ImagePanel extends JPanel implements ActionListener{ private String strPicName; ImageIcon white=new ImageIcon("white.jpg"); public ImagePanel(String strName) { strPicName = strName; this.setLayout(null); JButton button=new JButton(white); //JLabel label=new JLabel(); button.setBounds(0,0,30,20); button.setFocusPainted(false); button.setBorderPainted(false); button.setContentAreaFilled(false); button.setMargin(new Insets(0,0,0,0)); //label.setBounds(120,80,80,50); //label.addActionListener(this); add(button); //add(label); } public void paintComponent(Graphics g) { loadPic(strPicName,g); } public void loadPic(String picName,Graphics g) { ImageIcon img=new ImageIcon(picName); int pWidth=this.getWidth(); int pHeight=this.getHeight(); int mWidth=img.getIconWidth() ; int mHeight=img.getIconHeight() ; int x,y; int width,height; if((mWidth<=pWidth)&&(mHeight<=pHeight)){ x=(pWidth-mWidth)/2; y=(pHeight-mHeight)/2; width=mWidth; height=mHeight; }else { width=pWidth; height=pHeight; float widthScale=(float)pWidth/(float)mWidth; float heightScale=(float)pHeight/(float)mHeight; if(widthScale<heightScale) height=(int)(mHeight*widthScale); else width=(int)(mWidth*heightScale); x=(pWidth-width)/2; y=(pHeight-height)/2; } //statusBar.setText("文件:"+ this +" 大小:"+mWidth+"X"+mHeight); g.clearRect(0,0,pWidth,pHeight); g.drawImage(img.getImage(),x,y,width,height,this); } public void actionPerformed(ActionEvent e) { //if(e.getSource()==label) { } }} 线程安全的账号提供者,大家看下 求助!Visual Editor插件下载、安装问题(Eclipse3.2.2) 及解决办法 java中如何获得系统目录 一个关于TTL的问题, 好像有点难 急!请问哪有swt的资料? 关于Scanner hasNext()对标准输入流的作用问题,关于阻塞,球高手! 控件数组怎么获得事件? 有没有人做过下载并安装,完全自动,怎么做? 遇到困难了 可以帮忙看看吗 在线等 小白请教这串json怎么解析呢? 一个字符串转换成2进制问题 问个初级的问题,J2SE,J2EE,J2ME相互之间的差别是什么,希望在线的朋友给个提示
但是出现新问题,加了背景图后,原先定位在JFrame上的控件不能显示,比如JButton JLabel----JButton要在上面点击一下才能显示出来,为什么??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class applet3 extends JFrame
{
Image img1;
MediaTracker mt;
Container contentPanel=this.getContentPane();public applet3() {
this.setSize(400,300);
this.setResizable(false);
this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);
img1=this.getToolkit().getImage("1.jpg");
mt=new MediaTracker(this);
mt.addImage(img1,0);
ImageIcon icon=new ImageIcon("4.jpg");
contentPanel.setLayout(null);
JButton button=new JButton(icon);
JLabel label=new JLabel(icon);
button.setBounds(0,0,80,30);
label.setBounds(90,90,80,50);
contentPanel.add(label);
contentPanel.add(button);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}public void paint(Graphics g) {
super.paint(g);
g.drawImage(img1,0,0,this.getWidth(),this.getHeight(),this);
}public static void main(String args[]) {
new applet3();
}
}出现的效果是:先看到两个控件button label,然后出现背景图片,背景图片将控件覆盖,就只能看见背景图片???不知道怎么解决,还请大虾门多帮忙!!~~~
paint()里顺序写反了,应该是
public void paint(Graphics g) {
g.drawImage(img1,0,0,this.getWidth(),this.getHeight(),this);
super.paint(g);
}
而且我建议你重载JPanel而不是JFrame,因为JFrame的paint()可能比较复杂,如果其中又重写了背景,那你的drawimage就给覆盖回去了,而JPanel我做过,没有问题
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class JPanelTemp extends JFrame
{
public JPanelTemp()
{
Container con=this.getContentPane();
con.setLayout(null);
ImagePanel panelTemp=new ImagePanel("chess.jpg");
panelTemp.setBounds(100,0,600,500);
con.add(panelTemp);
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String args[])
{
System.out.println("Starting App");
JPanelTemp f = new JPanelTemp();
f.setBounds(0,0,800,600);
f.show();
}
}class ImagePanel extends JPanel implements ActionListener
{
private String strPicName;
ImageIcon white=new ImageIcon("white.jpg");
public ImagePanel(String strName)
{
strPicName = strName;
this.setLayout(null);
JButton button=new JButton(white);
//JLabel label=new JLabel();
button.setBounds(0,0,30,20);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setMargin(new Insets(0,0,0,0));
//label.setBounds(120,80,80,50);
//label.addActionListener(this);
add(button);
//add(label);
}
public void paintComponent(Graphics g)
{
loadPic(strPicName,g);
}
public void loadPic(String picName,Graphics g)
{
ImageIcon img=new ImageIcon(picName);
int pWidth=this.getWidth();
int pHeight=this.getHeight();
int mWidth=img.getIconWidth() ;
int mHeight=img.getIconHeight() ;
int x,y;
int width,height;
if((mWidth<=pWidth)&&(mHeight<=pHeight)){
x=(pWidth-mWidth)/2; y=(pHeight-mHeight)/2;
width=mWidth; height=mHeight;
}else {
width=pWidth; height=pHeight;
float widthScale=(float)pWidth/(float)mWidth; float heightScale=(float)pHeight/(float)mHeight;
if(widthScale<heightScale)
height=(int)(mHeight*widthScale);
else
width=(int)(mWidth*heightScale);
x=(pWidth-width)/2;
y=(pHeight-height)/2;
}
//statusBar.setText("文件:"+ this +" 大小:"+mWidth+"X"+mHeight);
g.clearRect(0,0,pWidth,pHeight);
g.drawImage(img.getImage(),x,y,width,height,this);
}
public void actionPerformed(ActionEvent e)
{
//if(e.getSource()==label)
{
}
}
}