2

我正在研究一些简单的应用程序来熟悉 Swing 并遇到问题。

我试图有一个包含图像(在面板中)的框架以及用于放大/缩小图像的按钮。

我已经能够使添加图像的框架正常工作(尽管存在一些框架大小问题,但这是另一回事),但是,当我调用相同的组件类将其添加到面板时,什么也没有出现。我希望你们中的一个可以帮助阐明情况。

代码:

图像框架 - 如图所示工作

class ImageFrame extends JFrame{

public ImageFrame(){
    setTitle("Java Image Machine");

    init();
    pack();
}   

public final void init(){
    //ZoomPanel zoomPanel = new ZoomPanel();
    //ImagePanel imagePanel = new ImagePanel();
    ImageComponent component = new ImageComponent();

    //this.add(zoomPanel, BorderLayout.CENTER);
    this.add(component);
    //this.add(imagePanel, BorderLayout.SOUTH);
}
}

但是,在直接调用 ImageComponent 的同时使用 ImagePanel 或添加 ZoomPanel,不会:

class ImagePanel extends JPanel{    
public ImagePanel(){
    //setBorder(BorderFactory.createLineBorder(Color.black));
    ImageComponent component = new ImageComponent();
    add(component);
}   
}

组件类:

class ImageComponent extends JComponent{
public ImageComponent(){

    try{
        image = ImageIO.read(new File("test1.bmp"));
    }
    catch ( IOException e ){
        e.printStackTrace();
    }

    System.out.println("W: " + image.getWidth(this) + " H: " + image.getHeight(this));
}

public void paintComponent( Graphics g ){
    super.paintComponent(g);
    if (image == null)
        return;

    width = image.getWidth(this);
    height = image.getHeight(this);

    //System.out.println("Image should be painted");
    g.drawImage(image, 0, 0, null);
}

private Image image;
public int width;
public int height;

}

4

2 回答 2

4

它对我来说很好(我刚刚测试了ImageComponent课程):

在此处输入图像描述

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Test {

    /**
     * Default constructor Test.class
     */
    public Test() {
        initComponents();
    }

    public static void main(String[] args) {

        /**
         * Create GUI and components on Event-Dispatch-Thread
         */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }

    /**
     * Initialize GUI and components (including ActionListeners etc)
     */
    private void initComponents() {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //add ImageComponent to JFrame instance
        jFrame.add(new ImageComponent());

        //pack frame (size JFrame to match preferred sizes of added components and set visible
        jFrame.pack();
        jFrame.setVisible(true);
    }
}

class ImageComponent extends JComponent {

    private Image image;
    public int width;
    public int height;

    public ImageComponent() {
        try {
            image = ImageUtils.scaleImage(300, 300, ImageIO.read(new URL("http://harmful.cat-v.org/software/_java/java-evil-edition.png")));
            //image =  ImageIO.read(new URL("http://harmful.cat-v.org/software/_java/java-evil-edition.png"));//uses images scale
        } catch (Exception e) {
            e.printStackTrace();
        }

        //so we can set the JPanel preferred size to the image width and height
        ImageIcon ii = new ImageIcon(image);
        width = ii.getIconWidth();
        height = ii.getIconHeight();
    }

    //so our panel is the same size as image
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (image == null) {
            return;
        }

        width = image.getWidth(this);
        height = image.getHeight(this);

        g.drawImage(image, 0, 0, null);
    }
}
//class used for scaling images
class ImageUtils {

    static Image scaleImage(int width, int height, BufferedImage filename) {
        BufferedImage bi;
        try {
            bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = (Graphics2D) bi.createGraphics();
            g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
            g2d.drawImage(filename, 0, 0, width, height, null);
        } catch (Exception e) {
            return null;
        }
        return bi;
    }
}

JPanel问题可能是文件的路径,或者宽度和高度与图片不同的事实,因此我们根据以下getPrefferedSize(...)内容覆盖JPanel并返回正确的大小Image

于 2012-10-25T17:34:39.337 回答
1

您是否尝试repaint()在添加适当的组件后添加调用?

IE

class ImagePanel extends JPanel{    
    public ImagePanel(){
        //setBorder(BorderFactory.createLineBorder(Color.black));
        ImageComponent component = new ImageComponent();
        add(component);
        repaint();
    }   
}

还要仔细检查您是否将 ImagePanel(包含 ImageComponent)添加到 ImageFrame,并调用 .setVisible(True)。

于 2012-10-25T17:22:02.970 回答