在FileChooser中选中一张图片,然后把图片显示在界面

alex_yanchong 2008-03-06 01:40:08
我在做一个简单的Java Swing界面:
要求是用FileChooser根据路径选择一张图片,然后选中后把图片在界面上显示出来

现在我FileChooser部分实现没问题,选择的图片名字和路径都能显示

在根据静态路径载入图片的试验也成功了,现在问题是

Toolkit toolkit = Toolkit.getDefaultToolkit();

img = toolkit.getImage(path); //这个path的值是file chooser得到的 经过反斜杠处理 都没问题

这两行代码我是写在file chooser的相应事件里的

在这两行执行后 , 我怎么刷新frame 让图片显示呢(外面的paint方法没起作用)



所有代码
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import javax.imageio.*;

public class Demo extends JFrame
{
public static void main(String args[])
{
Demo demo = new Demo();
demo.setTitle("Uploader");
demo.setVisible(true);
}

private JFileChooser filechooser;
private JButton add;
private JButton upload;
private JButton left;
private JButton right;
private JLabel rotate;
private JLabel date;
private JComboBox list;
private String EXIF_date[] = {"File Modify Date","Today Date","Specify Other Date"};
private Image img;
public static String path;

public Demo()
{
this.setSize(500,500);
Container contentPane = getContentPane();
contentPane.setLayout(null);

add = new JButton("Add");
add.setBounds(30,20,100,20);
contentPane.add(add);
add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
fileOpenManager(arg0);
}
});

upload = new JButton("Upload");
upload.setBounds(360,20,100,20);
contentPane.add(upload);

left = new JButton("Left");
left.setBounds(420,400,70,20);
contentPane.add(left);

right = new JButton("Right");
right.setBounds(420,430,70,20);
contentPane.add(right);

date = new JLabel("Dates of files");
date.setBounds(30,400,150,20);
contentPane.add(date);

rotate = new JLabel("Rotate");
rotate.setBounds(370,400,70,20);
contentPane.add(rotate);

list = new JComboBox(date);
list.setSelectedIndex(2);
list.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
String value = (String)cb.getSelectedItem();
//Output the selected value
System.out.println(value);
}
});
list.setBounds(200,400,150,20);
contentPane.add(list);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}

public void paint(Graphics g)
{
g.drawImage(img, 0, 0, null);
}

public void fileOpenManager(ActionEvent arg0)
{
if (filechooser == null)
{
String[] pics = new String[] {"gif", "jpg"};

filechooser = new JFileChooser();

filechooser.setMultiSelectionEnabled(false);
filechooser.setAcceptAllFileFilterUsed(false);
}
int option = filechooser.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION)
{
//Output the chosen file name
System.out.println("You chose " +
filechooser.getSelectedFile().getName());

path = filechooser.getSelectedFile().getPath();
path = path.replaceAll("\\\\", "/");

System.out.println("You chose " + path);

Toolkit toolkit = Toolkit.getDefaultToolkit();

img = toolkit.getImage(path);
}
}
}
...全文
475 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
3D先生 2008-03-13
  • 打赏
  • 举报
回复
随便写一下:

class ImagePanel extends JPanel{
public String path
public ImagePanel(String p){
path=p;
}
pubblic void paint(Graphics g){
///绘制背静图片
}
}

public class PreImage extends JFrame{

public Jpanel panel;
public PreImage(){
panel=new JPanle();
panel.add(new ImagePanel("path1"));
panel.add(new ImagePanel("path2"));
panel.add(new ImagePanel("path3"));
.....
}
.....
}
xxgamexx 2008-03-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chenweionline 的回复:]
绘图的代码不要写在paint()里,
public void paintComponent(Graphics g){
g.drawImage(img, 0, 0, null);
}

还有,你读图最好不要用toolkit.getImage();
改用ImageIO.read()方法。

读完新图像后直接调用repaint()重绘。
[/Quote]

能解释下吗? 谢谢拉
chenweionline 2008-03-06
  • 打赏
  • 举报
回复
绘图的代码不要写在paint()里,
public void paintComponent(Graphics g){
g.drawImage(img, 0, 0, null);
}

还有,你读图最好不要用toolkit.getImage();
改用ImageIO.read()方法。

读完新图像后直接调用repaint()重绘。
alex_yanchong 2008-03-06
  • 打赏
  • 举报
回复
再问一下,上面1楼那个方法不错,谢谢
不过JComboBox局限在于每次只能显示一个图片,能不能有同时显示很多个图片的容器
JList行吗?

能有个例子参考下吗 谢谢
hengxxh 2008-03-06
  • 打赏
  • 举报
回复
顺手改了一个,根据你的描述,你可以通过文件选择器选择到文件,通过静态路径可以访问到图片,
下面的代码则是 把图片显示在一个下拉列表里 ,注意图片的路径,引用要正确,代码我是测试通过了的,当然,写的不是太好 !


package main;

import java.awt.Dimension;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Demo extends JFrame {

public Demo() {
super("图片显示");
ImagePanel p = new ImagePanel();
this.getContentPane().add(p);
this.setSize(new Dimension(500,500));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}

class ImagePanel extends JPanel {

public ImagePanel() {
super();
initComponent();
}

private void initComponent() {
this.setLayout(null);
this.setPreferredSize(new Dimension(400, 500));

// ****************************************************************
JComboBox comBox4 = new JComboBox();
Object[] items4 = new Object[] {
new javax.swing.ImageIcon("images/01.jpg"),
new javax.swing.ImageIcon("images/02.jpg"),
new javax.swing.ImageIcon("images/03.jpg"),
new javax.swing.ImageIcon("images/04.jpg"),
new javax.swing.ImageIcon("images/05.jpg"),
new javax.swing.ImageIcon("images/06.jpg"),
new javax.swing.ImageIcon("images/07.jpg"),
new javax.swing.ImageIcon("images/08.jpg"),
new javax.swing.ImageIcon("images/09.jpg"),
new javax.swing.ImageIcon("images/10.jpg"),
new javax.swing.ImageIcon("images/11.jpg") };
DefaultComboBoxModel comBoxModel4 = new DefaultComboBoxModel(items4);
comBox4.setModel(comBoxModel4);
JLabel label4 = new JLabel("选择图片");
label4.setBounds(20, 170, 100, 30);
comBox4.setBounds(130, 170, 150, 100);
this.add(label4);
this.add(comBox4);
}
}

public static void main(String args[]) {
Demo d = new Demo();
}
}

62,623

社区成员

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

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