java gui中怎么向窗体中添加一张图片,要求不用icon

并设定该图片大小为500*300像素。
2、怎么在面板中添加背景图片
3、面板中设置文本框,长度不超过10个字符

代码如下:

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  
   
public class GraExp extends JFrame  
{   
CInstead c1=new CInstead();  
Container c;  
JLabel lbl1=new JLabel("姓名:" );  
JLabel lbl2=new JLabel("密码:" );  
JTextField tf1=new JTextField(10),  
tf2=new JTextField(10);  
   
public GraExp()  
{   
setContentPane(c1);  
c=getContentPane();  
c.setLayout(new FlowLayout(FlowLayout.LEFT));  
c.add(lbl1);  
c.add(tf1);  
c.add(lbl2);  
c.add(tf2);  
   
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  
setSize(new Dimension(400,300));  
show();  
}   
public static void main(String[] args)  
{   
GraExp ge=new GraExp();  
}   
class CInstead extends JPanel  
{   
ImageIcon icon;  
Image img;  
public CInstead()  
{   
icon=new ImageIcon(GraExp.class.getResource("333.png" ));  
img=icon.getImage();  
}   
public void paintComponent(Graphics g)  
{   
super.paintComponent(g);  
g.drawImage(img,0,0,null );  
}   
}   
}

程序分析: 
JComponent.paint先绘制组件,然后绘制组件的边框,再绘制组件的子组件。调用的顺序确保组件、边框和子组件都是可视的。如果组件有一个 UI代表,则JComponent.paintComponent调用该代表的Update方法,该方法为不透明组件擦除背景,然后绘制组件。 
CInstead是一个不透明的组件,如果重载paint方法,其背景图是无法被擦除的,因此,即使更新了组件的所有包含组件,在界面上是看不到的。所以必须重载paintComponent方法,在绘制子组件前先擦除背景。 
对双缓存组件,paint方法负责把组件绘制到屏外缓存中,然后把屏外缓存拷贝到组件的屏上代表中,正因为如此,不建议为Swing组件重载paint,如果需要重新定义如何绘制组件,那么就重载paintComponent()。 

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-10-25

那用drawImage,需要的包,你自己包含以下。

package com.Swing;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

public class BackgroundPanel extends JPanel{
private Image image;
public BackgroundPanel(){
setOpaque(false);
setLayout(null);
}
public void setImage(Image image){
this.image = image;
}
/**
 * 画出背景
 */
protected void paintComponent(Graphics g){
if(image != null){
int width = getWidth();
int height = getHeight();
g.drawImage(image, 0, 0, width,height ,this);
}
super.paintComponent(g);
}
}



//这是在主类中的代码
backgroundPanel = new BackgroundPanel(); //创建背景面板
backgroundPanel.setImage(new ImageIcon("Image\\BackGround.jpg").getImage()); //设置背景图片
getContentPane().add(backgroundPanel, BorderLayout.CENTER);

第2个回答  推荐于2016-02-11

先写个panel用来贴图片的,然后再用另一个主frame加载这个panel即可。

package com.card.frame.image;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

public class ImagePanel extends JPanel{
private Image image;
public ImagePanel(Image img){
this.image = img;
Dimension dime = new Dimension(image.getWidth(null),image.getHeight(null));
this.setPreferredSize(dime);
this.setMaximumSize(dime);
this.setMinimumSize(dime);
this.setSize(dime);
this.setLayout(null);
}

public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image, 0,0,null);
}
}

//用来存放图片panel已经写好,现在来写主frame

package com.card.frame.frame;

import com.card.frame.image.ImagePanel;

import java.awt.*;

import javax.swing.*;

public class MyJFrame extends JFrame{

public MyJFrame(String title,int width,int height){
super(title);
this.setSize(width,height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dime = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dime.width/2-width/2, dime.height/2-height/2);
}

    public static void main(String[] args){
        MyJFrame mj = new MyJFrame("背景图片",500,300);//这里是frame大小,注意:因为写的是按照图片实际大小来填充panel,因此你需要看清楚图片为 多少*多少 例如:500*300的大小,这里就填写500,300.建议你先缩小图片后,在来填写。
        ImagePanel ip = new ImagePanel(new ImageIcon("C:\\Documents and Settings\\Administrator\\桌面\\WP_20140915_002.jpg").getImage());//这里是图片路径

        JTextField jt = new JTextField(10);
        jt.setBounds(new Rectangle(20, 20, 140, 20));//这里是文本框位置

        ip.add(jt);

        mj.add(ip);
        mj.setVisible(true);
    }

}

本回答被提问者和网友采纳
相似回答