我想讓滾動條工作。使用滾動條
該代碼顯示我選擇的圖像。我剛剛閱讀(在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);
}
}
}
我們不neccessarily需要看到完整的源代碼,只是張貼相關的代碼 – Jimmt
我認爲相關位是滾動面板的東西我剛加入 - – user1501247
frame.getContentPane()加(panel_3,BorderLayout的。南); scrollPane = new JScrollPane(); frame.getContentPane()。add(scrollPane,BorderLayout.CENTER); panel_2 = new ImagePanel(); scrollPane.setViewportView(panel_2); – user1501247