我正在尝试让滚动条工作。
代码显示我选择的图像。而且我刚刚(在stackoverflow上)阅读了有关将图像添加到面板,然后将面板添加到滚动窗格,以及将滚动窗格添加到框架的信息。所以我试着这样做。
但我仍然无法显示滚动条。即使图像大于 JFrame 窗口。
任何人都可以帮忙吗?我试过重新验证,但它没有用,所以我把它拿出来了。
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MakeResizedImage {
private JFrame frame;
private JFileChooser fc;
private File file;
private int r;
private JTextField textField;
private Image img;
JScrollPane scrollPane;
ImagePanel panel_2;
public static void main(String[] args) {
MakeResizedImage MRI = new MakeResizedImage();
MRI.BuildJFrameGui();
}
private MakeResizedImage() {
}
private void BuildJFrameGui() {
JPanel panel = new JPanel();
panel.setLayout(null);
frame = new JFrame("View/Resize a png, jpg or gif image");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("Image:");
lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
panel_1.add(lblNewLabel);
textField = new JTextField();
panel_1.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Width:");
panel_1.add(lblNewLabel_1);
Dimension Dim = new Dimension(45, 20);
JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0),
null, new Integer(1)));
spinner.setPreferredSize(Dim);
panel_1.add(spinner);
JLabel lblHeight = new JLabel("Height:");
panel_1.add(lblHeight);
JSpinner spinner_1 = new JSpinner();
spinner_1.setModel(new SpinnerNumberModel(new Integer(0),
new Integer(0), null, new Integer(1)));
spinner_1.setPreferredSize(Dim);
panel_1.add(spinner_1);
JPanel panel_3 = new JPanel();
JButton btnNewButton = new JButton("Open");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
chooseFile();
}
});
panel_3.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Cancel");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
panel_3.add(btnNewButton_1);
JButton btnSave = new JButton("Save");
panel_3.add(btnSave);
frame.getContentPane().add(panel_3, BorderLayout.SOUTH);
scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
panel_2 = new ImagePanel();
scrollPane.setViewportView(panel_2);
frame.setVisible(true);
}
public void chooseFile() {
if (aFileChosen()) {
file = fc.getSelectedFile();
if (file.exists()) {
textField.setText(fc.getName());
try {
img = ImageIO.read(file);
panel_2.setImg(img);
} catch (IOException e) {
}
}
} else
JOptionPane.showMessageDialog(frame, "No file was chosen");
}
public boolean aFileChosen() {
fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"PNG, JPG & GIF Images", "png", "jpg", "gif");
fc.setFileFilter(filter);
fc.setApproveButtonText("Choose");
fc.setApproveButtonToolTipText("Selects the image you want to resize");
r = fc.showOpenDialog(frame);
if (r == JFileChooser.APPROVE_OPTION)
return true;
return false;
}
class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
private Image img;
ImagePanel() {
};
ImagePanel(Image img) {
this.img = img;
}
public void setImg(Image img) {
this.img = img;
repaint();
}
@Override
public Dimension getPreferredSize() {
return (new Dimension(300, 300));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Clears the previously drawn image.
g.clearRect(0, 0, getWidth(), getHeight());
//and draws the new one...
g.drawImage(img, 0, 0, this);
}
}
}