java浏览图片显示图片_浏览图像文件并使用Java Swing显示它

该博客讨论了一个Java应用程序,使用Swing组件创建GUI来显示图像。当用户点击'浏览'按钮时,可以选择一个图像文件,该文件将显示在GUI上。然而,存在一个问题:当用户再次点击'浏览'时,新选择的图像不会替换旧图像。博客作者提供了代码示例,并寻求解决方案。
摘要由CSDN通过智能技术生成

我的问题是,

单击“浏览”按钮后,它将显示要选择的目录中的所有文件,

然后所选图像正确显示在GUI中.但是当我单击“浏览”按钮时

第二次,它只显示旧图像而不是显示新图像.请帮帮我.

作为参考,我上传了UI.

package GUI;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.Graphics2D;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.GroupLayout;

import javax.swing.GroupLayout.Alignment;

@SuppressWarnings("serial")

public class MainAppFrame extends JFrame {

private JPanel contentPane;

File targetFile;

BufferedImage targetImg;

public JPanel panel,panel_1;

private static final int baseSize = 128;

private static final String basePath =![enter image description here][1]

"C:\Documents and Settings\Administrator\Desktop\Images";

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

MainAppFrame frame = new MainAppFrame();

frame.setVisible(true);

frame.setResizable(false);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public MainAppFrame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 550, 400);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(new BorderLayout(0, 0));

panel = new JPanel();

panel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

contentPane.add(panel, BorderLayout.WEST);

JButton btnBrowse = new JButton("Browse");

btnBrowse.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

browseButtonActionPerformed(e);

}

});

JLabel lblSelectTargetPicture = new JLabel("Select target picture..");

JButton btnDetect = new JButton("Detect");

btnDetect.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

}

});

JButton btnAddDigit = new JButton("Add Digit");

btnAddDigit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

}

});

JButton button = new JButton("Recognize");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

}

});

panel_1 = new JPanel();

panel_1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

GroupLayout gl_panel = new GroupLayout(panel);

gl_panel.setHorizontalGroup(

gl_panel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_panel.createSequentialGroup()

.addGap(6)

.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_panel.createSequentialGroup()

.addComponent(lblSelectTargetPicture)

.addGap(6)

.addComponent(btnBrowse))

.addGroup(gl_panel.createSequentialGroup()

.addGap(10)

.addComponent(btnDetect)

.addGap(18)

.addComponent(btnAddDigit))))

.addGroup(gl_panel.createSequentialGroup()

.addGap(50)

.addComponent(button))

.addGroup(gl_panel.createSequentialGroup()

.addContainerGap()

.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE))

);

gl_panel.setVerticalGroup(

gl_panel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_panel.createSequentialGroup()

.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_panel.createSequentialGroup()

.addGap(7)

.addComponent(lblSelectTargetPicture))

.addGroup(gl_panel.createSequentialGroup()

.addGap(3)

.addComponent(btnBrowse)))

.addGap(18)

.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)

.addGap(22)

.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)

.addComponent(btnDetect)

.addComponent(btnAddDigit))

.addGap(18)

.addComponent(button)

.addContainerGap())

);

panel.setLayout(gl_panel);

}

public BufferedImage rescale(BufferedImage originalImage)

{

BufferedImage resizedImage = new BufferedImage(baseSize, baseSize, BufferedImage.TYPE_INT_RGB);

Graphics2D g = resizedImage.createGraphics();

g.drawImage(originalImage, 0, 0, baseSize, baseSize, null);

g.dispose();

return resizedImage;

}

public void setTarget(File reference)

{

try {

targetFile = reference;

targetImg = rescale(ImageIO.read(reference));

} catch (IOException ex) {

Logger.getLogger(MainAppFrame.class.getName()).log(Level.SEVERE, null, ex);

}

panel_1.setLayout(new BorderLayout(0, 0));

panel_1.add(new JLabel(new ImageIcon(targetImg)));

setVisible(true);

}

private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {

JFileChooser fc = new JFileChooser(basePath);

fc.setFileFilter(new JPEGImageFileFilter());

int res = fc.showOpenDialog(null);

// We have an image!

try {

if (res == JFileChooser.APPROVE_OPTION) {

File file = fc.getSelectedFile();

setTarget(file);

} // Oops!

else {

JOptionPane.showMessageDialog(null,

"You must select one image to be the reference.", "Aborting...",

JOptionPane.WARNING_MESSAGE);

}

} catch (Exception iOException) {

}

}

}

//JPEGImageFileFilter.java

package GUI;

import java.io.File;

import javax.swing.filechooser.FileFilter;

/*

* This class implements a generic file name filter that allows the listing/selection

* of JPEG files.

*/

public class JPEGImageFileFilter extends FileFilter implements java.io.FileFilter

{

public boolean accept(File f)

{

if (f.getName().toLowerCase().endsWith(".jpeg")) return true;

if (f.getName().toLowerCase().endsWith(".jpg")) return true;

if(f.isDirectory())return true;

return false;

}

public String getDescription()

{

return "JPEG files";

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值