大家好,我的目的是想做一个可以在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();
}
}
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();
}
}
解决方案 »
- 关于多线程的一个问题
- repaint和paintComponent的问题,急~~~
- java图形界面的坐标问题
- 在JTable中,拖动一列到其它的位置,再点击排序,这时它是对原来在这个位置的列进行排序,怎么样让它对这个新的列进行排序呢?
- java在路上
- 窗体调用问题
- 怎么在JDK中找到JSP中的request对象的帮助啊,我怎么没有找到啊,晕
- 急急急,在线等答案
- 紧急求助!!!!在java中调用c编写的exe文件时,如何让c的运行界面不出现?
- 听说vcafe可以将JAVA编译为EXE文件,那位尝试过,能否给详细介绍一下,如何操作,我始终没发现这个功能
- 自定义事件的发送与接收
- 如何在JPanel里通过点击按钮激活文件对话框来打开图片文件
if (result == JFileChooser.APPROVE_OPTION)
{
str = fileChooser.getCurrentDirectory().toString() + "\\"; //获得绝对目录
str += fileChooser.getSelectedFile().getName(); //获得绝对路径
icon = new ImageIcon(BackgroundPanel.class.getResource(str));
PaintDemo(g);
}
ImageIcon icon = null;
JButton b1 = null;
JFileChooser fileChooser = null;
String str = null;
Graphics g = this.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)
{
str = fileChooser.getSelectedFile().getAbsolutePath();
System.out.println(str);
icon = new ImageIcon(str); repaint(); //调用paint()
}
}
} //覆盖paint()函数
public void paint(Graphics g) {
if (icon!=null){
Image image = icon.getImage();
g.drawImage(image, 0, 0, icon.getImageObserver());
}
}
}现在肯定行了
楼上大师。你忘了这一句,要不就黑屏了