我在做一个简单的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);
}
}
}
要求是用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);
}
}
}
下面的代码则是 把图片显示在一个下拉列表里 ,注意图片的路径,引用要正确,代码我是测试通过了的,当然,写的不是太好 !
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();
}
}
public void paintComponent(Graphics g){
g.drawImage(img, 0, 0, null);
}还有,你读图最好不要用toolkit.getImage();
改用ImageIO.read()方法。读完新图像后直接调用repaint()重绘。
不过JComboBox局限在于每次只能显示一个图片,能不能有同时显示很多个图片的容器
JList行吗?能有个例子参考下吗 谢谢
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"));
.....
}
.....
}