如何在单击按钮时更新 JFrame 的内容。我正在构建一个图像查看器,其中将传入一个 photopath 列表,用户必须单击下一个或上一个按钮来回查看图像。如何更改图像并更新 Jframe 下面是我当前的实现。我试图处置当前框架并制作一个新框架,但这不是我要寻找的... package 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);
}
}