大家好,有个炸弹,麻烦各位排一下

zhangyangli 2006-01-27 05:48:36
大家好,我的目的是想做一个可以在JPanel里通过点击按钮激活文件对话框来打开图片文件的一个小程序

,但是有个异常,请麻烦各位看下:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class BackgroundPanel extends JPanel implements ActionListener {
ImageIcon icon;
JButton b1;
JFileChooser fileChooser = null;
String str = null;
Graphics g = getGraphics();

public BackgroundPanel() {
b1 = new JButton("打开文件");
b1.addActionListener(this);
this.add(b1);

fileChooser = new JFileChooser("E:\\");

}

public void actionPerformed(ActionEvent e)
{
File file = null;
int result;

if (e.getActionCommand().equals("打开文件"))
{
fileChooser.setApproveButtonText("打开吗");
fileChooser.setDialogTitle("确定");
result = fileChooser.showOpenDialog(this);

if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
str = file.getName();
icon = new ImageIcon(BackgroundPanel.class.getResource(str));
// this.setPreferredSize(new Dimension(467, 362));
PaintDemo(g);
}

}
}
public void PaintDemo(Graphics g) {
g.drawImage(icon.getImage(), 0, 0, this);
}
}

class my extends JFrame {

BackgroundPanel bp;

public my() {
bp = new BackgroundPanel();
this.getContentPane().add(bp);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args) {
new my();
}
}
...全文
85 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
loujing 2006-02-05
  • 打赏
  • 举报
回复
测试了一下,程序本身没有问题,是在打开文件的时候出现异常吗?

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧