大家好,我的目的是想做一个可以在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();
}
}
解决方案 »
- java泛型问题
- 关于自增运算优先级的问题,请赐教!
- 请问如何编写一个java程序来判断字符串类型是否匹配?
- 'i = i + 1"与"i += 1"有什么区别?
- 关于数据库连接池问题请教
- 怎样获取网页的最后更新时间????
- 简单聊天室实现时,我想把TextField中敲的字,按回车后,显示在TextArea中,怎么显示的只是当前的文字呀?
- SWT&JFace中如何实现控件的拖放?
- 怎么样让我的JFrame的大小设置为:显示器屏幕大小减去Windows任务栏后的大小?
- 大侠:JBuilder 6的注册码 ??
- JTable中绑定JComboBox,并设置其样式
- java中如何找到应用程序的存放位置?
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;/**
* @author dido leo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public 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.getAbsolutePath();
icon = new ImageIcon(str);
// this.setPreferredSize(new Dimension(467, 362));
PaintDemo();
}
}
}
public void PaintDemo() {
repaint();
} public void paintComponent(Graphics g){
//
this.g=g;
if(icon!=null)
g.drawImage(icon.getImage(), 0, 0, this);
}}
button.onclick(){
jPane.add(new JLabel(new ImageIcon("filepath")),BorderLayout.CENTER);
}