У меня есть простое приложение, в котором я хочу показать большое изображение на прокручиваемой панели.Я использую NavigableImagePanel из http://today.java.net/pub/a/today/2007/03/27/navigable-image-panel.html
Во-первых, я получаю результат -
В настоящее время изображение представляет собой очень маленькую панель рядом с верхними кнопками.Это с BorderLayout.CENTER
Код для NavigableImagePanel:
http://pastebin.com/1wHRwMJU
и мой код OpenImage.java:
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class OpenImage extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 9066218264791891436L;
Image img;
public OpenImage() throws IOException{
super("Resize and Rotate");
setDefaultCloseOperation(EXIT_ON_CLOSE);
BorderLayout grid = new BorderLayout();
//this.setLayout(grid);
setSize(900,700);
setVisible(true);
//Row 1 holds some important buttons
//FlowLayout layout1 = new FlowLayout();
//MigLayout mig = new MigLayout();
JPanel row1 = new JPanel();
//LayoutManager grid = new BoxLayout(row1, BoxLayout.X_AXIS);
//row1.setLayout(grid);
//row1.setLayout(BorderLayout.NORTH);
//row1.setMaximumSize(new Dimension(100,100));
BorderLayout border = new BorderLayout();
//row1.setPreferredSize(new Dimension(0, 400));
JButton open = new JButton ("Open");
open.addActionListener(this);
JButton rotate = new JButton("Rotate");
rotate.addActionListener(this);
JButton resize = new JButton("Resize");
resize.addActionListener(this);
JButton exit = new JButton ("Exit");
exit.addActionListener(this);
row1.add(open);
row1.add(rotate);
row1.add(resize);
row1.add(exit);
//This section has a workable picture panel, but it is too large.
//ImagePanel imagepanel = new ImagePanel();
BorderLayout grid1 = new BorderLayout();
Container cp = getContentPane();
cp.setLayout(grid1);
//add(row1);
//JScrollPane row2 = new JScrollPane(imagepanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
BufferedImage img = ImageIO.read(new File("/home/adam/snow.jpg"));
NavigableImagePanel imagepanel = new NavigableImagePanel(img);
JPanel row2 = new JPanel();
row2.add(imagepanel);
//row2.repaint();
JButton save = new JButton("Save");
JPanel row3 = new JPanel();
row3.add(save);
cp.add(BorderLayout.NORTH, row1);
cp.add(BorderLayout.CENTER, row2);
cp.add(BorderLayout.SOUTH, row3);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command == "Exit"){
System.exit(0);
}
if (command == "Open"){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
}
}
//This method below is now being deprecat
public static void main(String [] args){
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
try {
JFrame frame = new OpenImage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}