这是我写的代码,我想把creatImage()实现的方法在JLabel上显示,但是总也弄不好.望各位高手帮忙修改一下!谢谢啦import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.*;public class loginFrame extends JDialog {
JLabel jl_rand1 = new JLabel("验证码:"); public loginFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
addListener();
} public loginFrame(JFrame owner) throws HeadlessException {
super(owner,true);
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
addListener();
} private void jbInit() throws Exception { /****UI显示设定****/
jp_center.add(jl_rand); this.add("North", jp_north);
this.add("Center", jp_center);
this.add("South", jp_south);
this.add("West", jp_west);
this.add("East", jp_east); /****窗口显示设定****/ //=====窗口获取屏幕大小
int screenHeigth = Toolkit.getDefaultToolkit().getScreenSize().height; //高
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width; //宽
//===========END================= this.setTitle("用户登录");
this.setSize(360, 205);
this.setLocation((screenWidth - 360) / 2, (screenHeigth - 205) / 2);
this.setVisible(false);
this.setResizable(false); //禁止改变大小
this.getRootPane().setWindowDecorationStyle(JRootPane.
INFORMATION_DIALOG);
this.setUndecorated(true);
this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
} public BufferedImage creatImage(){
// 在内存中创建图象
int width = 48, height = 18;
int fontSize = 18;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); // 获取图形上下文
Graphics g = this.getGraphics(); // 生成随机类
Random random = new Random(); // 设定背景色
g.setColor(new Color(186, 227, 244));
g.fillRect(0, 0, width, height); // 设定字体
g.setFont(new Font("Times New Roman", Font.BOLD, fontSize)); // 画边框
// g.setColor(new Color());
// g.drawRect(0,0,width-1,height-1); // 随机产生155条干扰线
g.setColor(getRandColor(180, 250));
for(int i = 0; i < 50; i++){
int x = random.nextInt(width - 5);
int y = random.nextInt(height - 5);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
} // 取随机产生的认证码(4位数字)
// String rand = request.getParameter("rand");
// rand = rand.substring(0,rand.indexOf(".")); int fontNum = 4;
int w0 = 4;
int fontw = (width - (fontNum + 1) * w0) / fontNum;
for(int i = 0; i < fontNum; i++){
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 将认证码显示到图象中
g.setColor(new Color(random.nextInt(10), random.nextInt(20), random.nextInt(30)));
g.drawString(rand, w0 + (fontw + w0) * i, height - 2);
}
// 图象生效
g.dispose();
return image;
} // 给定范围获得随机颜色
private Color getRandColor(int fc, int bc){
Random random = new Random();
if(fc > 255)
fc = 255;
if(bc > 255)
bc = 255; int n = bc - fc;
int r = fc + random.nextInt(n);
int g = fc + random.nextInt(n);
int b = fc + random.nextInt(n);
return new Color(r, g, b);
} public String getSRand(){
return sRand;
}
//=====绘制END======
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.*;public class loginFrame extends JDialog {
JLabel jl_rand1 = new JLabel("验证码:"); public loginFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
addListener();
} public loginFrame(JFrame owner) throws HeadlessException {
super(owner,true);
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
addListener();
} private void jbInit() throws Exception { /****UI显示设定****/
jp_center.add(jl_rand); this.add("North", jp_north);
this.add("Center", jp_center);
this.add("South", jp_south);
this.add("West", jp_west);
this.add("East", jp_east); /****窗口显示设定****/ //=====窗口获取屏幕大小
int screenHeigth = Toolkit.getDefaultToolkit().getScreenSize().height; //高
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width; //宽
//===========END================= this.setTitle("用户登录");
this.setSize(360, 205);
this.setLocation((screenWidth - 360) / 2, (screenHeigth - 205) / 2);
this.setVisible(false);
this.setResizable(false); //禁止改变大小
this.getRootPane().setWindowDecorationStyle(JRootPane.
INFORMATION_DIALOG);
this.setUndecorated(true);
this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
} public BufferedImage creatImage(){
// 在内存中创建图象
int width = 48, height = 18;
int fontSize = 18;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); // 获取图形上下文
Graphics g = this.getGraphics(); // 生成随机类
Random random = new Random(); // 设定背景色
g.setColor(new Color(186, 227, 244));
g.fillRect(0, 0, width, height); // 设定字体
g.setFont(new Font("Times New Roman", Font.BOLD, fontSize)); // 画边框
// g.setColor(new Color());
// g.drawRect(0,0,width-1,height-1); // 随机产生155条干扰线
g.setColor(getRandColor(180, 250));
for(int i = 0; i < 50; i++){
int x = random.nextInt(width - 5);
int y = random.nextInt(height - 5);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
} // 取随机产生的认证码(4位数字)
// String rand = request.getParameter("rand");
// rand = rand.substring(0,rand.indexOf(".")); int fontNum = 4;
int w0 = 4;
int fontw = (width - (fontNum + 1) * w0) / fontNum;
for(int i = 0; i < fontNum; i++){
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 将认证码显示到图象中
g.setColor(new Color(random.nextInt(10), random.nextInt(20), random.nextInt(30)));
g.drawString(rand, w0 + (fontw + w0) * i, height - 2);
}
// 图象生效
g.dispose();
return image;
} // 给定范围获得随机颜色
private Color getRandColor(int fc, int bc){
Random random = new Random();
if(fc > 255)
fc = 255;
if(bc > 255)
bc = 255; int n = bc - fc;
int r = fc + random.nextInt(n);
int g = fc + random.nextInt(n);
int b = fc + random.nextInt(n);
return new Color(r, g, b);
} public String getSRand(){
return sRand;
}
//=====绘制END======
}
...