为什么在添加图像时JFrame会自行调整大小

时间:2015-10-24 16:53:03

标签: java swing jframe

帧的大小设置为500x500,但是当我向其添加图像时,帧会调整大小到图像的大小。我试过用这个

jf.setPreferredSize(new Dimension(500, 500));

然后图像不会出现。

import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.event.*;
public class Test implements ActionListener {
ImageIcon imgIcon;
  JFrame jf ;
  JPanel jp_image,jp_option;
  BufferedImage img;
  Test(){
        jf = new JFrame("photo editor");
        jf.setPreferredSize(new Dimension(500, 500));
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jp_image = new JPanel();
        jp_option = new JPanel();

        jp_image.setPreferredSize(new Dimension(200, 500));
        jp_option.setPreferredSize(new Dimension(300, 500));

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, jp_option,  jp_image);
        jf.getContentPane().add(splitPane);
        jf.setSize(1900, 1000);
        JMenuBar mbar = new JMenuBar();
        jf.setJMenuBar(mbar);
        JMenu File = new JMenu("File");
        JMenuItem New;
        File.add(New = new JMenuItem("New..."));
        mbar.add(File);
        jf.setVisible(true);
        New.addActionListener(this); 
  }


  public void actionPerformed(ActionEvent ae){
      String arg = ae.getActionCommand();
        if(arg.equals("New...")){
                File file = null;
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(" jpg,gif,jpeg,png", "jpg", "gif" , "jpeg" , "png");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("File Choosed: " + chooser.getCurrentDirectory() + "\\" +chooser.getSelectedFile().getName());
                    file = chooser.getSelectedFile();
                }           
                try{
                    img = ImageIO.read(file);
                    jp_image.add(new JLabel(new ImageIcon(img)));
                    jp_image.setSize(300, 500); 
                    jf.setPreferredSize(new Dimension(500, 500));// no image appears
                    //jf.pack();    the image appears but the frame resizes     
                }
                catch(Exception e){
                    System.out.println("No Image Selected");
                }
        }


    }

  public static void main(String args[]){
      Test t = new Test();
  }
}

如何在不调整框架大小的情况下在面板中设置图像。

1 个答案:

答案 0 :(得分:3)

您正在向GUI添加组件,但没有告诉组件的容器布局管理器您正在执行此操作。添加JLabel后,请务必在容器上调用repaint()revalidate()。例如:

img = ImageIO.read(file);
jp_image.add(new JLabel(new ImageIcon(img)));
jp_image.setSize(300, 500);
jf.setPreferredSize(new Dimension(500, 500));// no image appears
jp_image.revalidate();
jp_image.repaint();

更好的是 - 在GUI创建时只添加一次的JLabel,并在获取图像时不重新添加它,而只需设置其Icon。例如:

public class Test implements ActionListener {
    ImageIcon imgIcon;
    JFrame jf;
    JPanel jp_image, jp_option;
    BufferedImage img;
    private JLabel imageLabel = new JLabel();

    Test() {
        // ...

        jp_image.add(imageLabel); //!!

        // ...
    }

    public void actionPerformed(ActionEvent ae) {
        String arg = ae.getActionCommand();
        if (arg.equals("New...")) {
            // ...

            try {
                img = ImageIO.read(file);
                imageLabel.setIcon(new ImageIcon(img)); //!!
            } catch (Exception e) {
                e.printStackTrace();
            }
        }