问题描述:

I have a simple app where I want to show a large image in a scrollable panel. I'm using NavigableImagePanel from http://today.java.net/pub/a/today/2007/03/27/navigable-image-panel.html

Firstly the result I'm getting -

The image is currently a very small panel near the top buttons. This is with BorderLayout.CENTER

The code for NavigableImagePanel:

http://pastebin.com/1wHRwMJU

and my OpenImage.java code:

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();

}

}

}

网友答案:
  1. Code that you post here missed there lots of Java imports, can't see here any Java imports from Swing packages, and import for Image.
  2. setVisible(true); must be last code line in the constructor.
  3. In all case setBackground() can help you to find any problem.
  4. In all case to try use pack() instead of setSize(900,700);
  5. Since I set imagepanel.setPreferredSize(new Dimension(600, 400));, that's wrong JComponents to have to returns preferred size, then pack() will work correctly.
  6. No idea from where you get this code, so hardly could be works without exceptions.
  7. just cleanup useless mess

from code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class OpenImage extends JFrame implements ActionListener {

    private static final long serialVersionUID = 9066218264791891436L;
    private Image img;

    public OpenImage() {
        super("Resize and Rotate");

        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);

        JPanel row1 = new JPanel();
        row1.setBackground(Color.red);
        row1.setLayout(new FlowLayout());
        row1.setBorder(new LineBorder(Color.black, 1));
        row1.add(open);
        row1.add(rotate);
        row1.add(resize);
        row1.add(exit);

        JPanel imagepanel = new JPanel();
        imagepanel.setLayout(new BorderLayout());
        imagepanel.setBackground(Color.blue);
        imagepanel.setBorder(new LineBorder(Color.black, 1));
        imagepanel.setPreferredSize(new Dimension(600, 400));

        JPanel row2 = new JPanel();
        row2.setLayout(new BorderLayout(10, 10));
        row2.setBorder(new LineBorder(Color.black, 1));
        row2.add(imagepanel, BorderLayout.CENTER);
        row2.setBackground(Color.red);
        JButton save = new JButton("Save");

        JPanel row3 = new JPanel();
        row3.setBorder(new LineBorder(Color.black, 1));
        row3.setBackground(Color.green);
        row3.setLayout(new FlowLayout());
        row3.add(save);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));
        add(BorderLayout.NORTH, row1);
        add(BorderLayout.CENTER, row2);
        add(BorderLayout.SOUTH, row3);
        pack();
        setVisible(true);
    }

    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);
        }
    }

    public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                OpenImage openImage = new OpenImage();
            }
        });
    }
}
相关阅读:
Top