如何通过单击按钮更新JFrame的内容。我正在构建一个图像查看器,其中将传入一个photopath列表,用户必须单击下一个或上一个按钮才能来回查看图像。如何更改图像和更新Jframe以下是我当前的实现。我试图处理当前的框架并制作一个新框架,但这不是我想要的...... 包cs213.photoAlbum.simpleview;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import model.photoNode;
public class ImageViewer {
private JFrame frame = new JFrame("Image Viewer");
private Container pane = frame.getContentPane();
private JPanel imgViewPanel = new JPanel();
JSlider slider = new JSlider(0, 100, 100);
JLabel percent = new JLabel("100%");
String imageFile = "";
private class ImageView extends JScrollPane {
JPanel panel = new JPanel();
Dimension originalSize = new Dimension();
Image originalImage;
JLabel iconLabel;
public ImageView(ImageIcon icon) {
this.originalImage = icon.getImage();
panel.setLayout(new BorderLayout());
iconLabel = new JLabel(icon);
panel.add(iconLabel);
setViewportView(panel);
originalSize.width = icon.getIconWidth();
originalSize.height = icon.getIconHeight();
}
public void update() {
int min = slider.getMinimum();
int max = slider.getMaximum();
int span = max - min;
int value = slider.getValue();
double multiplier = (double) value / span;
multiplier = multiplier == 0.0 ? 0.01 : multiplier;
Image scaled = originalImage.getScaledInstance((int) (originalSize.width * multiplier), (int) (originalSize.height * multiplier), Image.SCALE_FAST);
iconLabel.setIcon(new ImageIcon(scaled));
}
}
ImageView imageView;
public ImageViewer(String imgFile, final ArrayList<photoNode> list) {
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
});
createAndShowGUI();
imageFile = imgFile;
ImageIcon icon = new ImageIcon(imageFile);
imageView = new ImageView(icon);
JPanel panel = new JPanel();
panel.add(new JLabel("Set Image Size: "));
panel.add(slider);
panel.add(percent);
pane.add(panel, BorderLayout.NORTH);
imgViewPanel.add(imageView);
pane.add(imgViewPanel, BorderLayout.CENTER);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (!slider.getValueIsAdjusting()) {
percent.setText(slider.getValue() + "%");
imageView.update();
}
}
});
JButton next = new JButton("Next");
JButton prev = new JButton("Prev");
pane.add(next, BorderLayout.EAST);
pane.add(prev, BorderLayout.WEST);
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
imgViewPanel.remove(imageView);
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).fileName.equalsIgnoreCase(imageFile)) {
if (i + 1 < list.size()) {
//frame.dispose();
ImageIcon icon = new ImageIcon(list.get(i + 1).fileName);
ImageView imageView = new ImageView(icon);
imgViewPanel.add(imageView);
pane.add(imgViewPanel, BorderLayout.CENTER);
imgViewPanel.revalidate();
break;
}
}
}
}
}
});
}
private void createAndShowGUI() { // Creating the GUI...
frame.getContentPane().setBackground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setTitle("PhotoAlbum55");
frame.pack();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
}
}
答案 0 :(得分:2)
交换图像的最简单方法是在JLabel中将它们显示为ImageIcons,只需在JLabel上调用setIcon(...)
传入另一个ImageIcon。如果图像足够小,您可以创建一组图标,可能是List<Icon>
,然后在下一个或上一个按下按钮时向后或向前移动。