简体   繁体   English

调整JFrame的大小以适合JPanel

[英]Resizing JFrame to fit JPanel

I have a GUI, which consists of a JFrame and Menu bar. 我有一个GUI,其中包含JFrame和菜单栏。 Inside the JFrame is my custom Panel (extending JPanel), which is initially hidden. 在JFrame内是我的自定义面板(扩展了JPanel),该面板最初是隐藏的。 The user chooses an image (JFileChooser), this image is passed to the panel and drawn (using paintIcon). 用户选择一个图像(JFileChooser),该图像传递到面板并绘制(使用paintIcon)。 The panel is resized to fit the image and then set visible. 调整面板大小以适合图像,然后将其设置为可见。 I want to resize my JFrame to fit the panel, but I cannot seem to get it to work. 我想调整JFrame的大小以适合面板,但是似乎无法正常工作。 I have tried using the pack() method, but this just makes a tiny GUI, that is the size of the JMenu! 我尝试使用pack()方法,但这只是一个很小的GUI,即JMenu的大小! I have also tried resizing both the frame and the JPanel using the getIconWidth() and getIconHeight() properties of the ImageIcon, but while this correctly sizes the panel it does not size the JFrame. 我也尝试过使用ImageIcon的getIconWidth()和getIconHeight()属性来调整框架和JPanel的大小,但是虽然可以正确调整面板的大小,但无法调整JFrame的大小。 Any ideas as to how I would do this correctly? 关于如何正确执行此操作的任何想法?

The block of code where I am setting the size of the JFrame is here: 我在其中设置JFrame大小的代码块在这里:

@Override
public void actionPerformed(ActionEvent e) 
{

    JFileChooser imgChooser = new JFileChooser();
    JMenuItem evtSource = (JMenuItem) e.getSource();
    String srcText = evtSource.getText();

    if (srcText.equals("Add Image..."))
    {
        imgChooser = new JFileChooser();
        imgChooser.showOpenDialog(frmMain);

        chosenImage = imgChooser.getSelectedFile();
        try
        {
            loadedImage = ImageIO.read(chosenImage);
        }
        catch (IOException ex)
        {
            String errorMsg = ex.getMessage();
            JOptionPane.showMessageDialog(frmMain, "Error while loading file: " + errorMsg, "Error!", JOptionPane.ERROR_MESSAGE);
        }

        panelImage = new ImageIcon(loadedImage);

        frmMain.setSize(panelImage.getIconWidth(), panelImage.getIconHeight() + mbMenu.getHeight());
        displayImage.loadImage(panelImage);
        displayImage.setVisible(true);

    }
}

The code from the relevant sections of the "displayImage" custom panel is below: 以下是“ displayImage”自定义面板相关部分中的代码:

public void paintComponent(Graphics g) 
{
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        if (imgIsLoaded)
        {
            panelImage.paintIcon(this,g2,0,0);
            resizePanel(panelImage.getIconWidth(), panelImage.getIconHeight());

        }
}

public void loadImage(ImageIcon i)
{
    panelImage = i;
    imgIsLoaded = true;
}

public void resizePanel(int w, int h)
{
    this.setSize(w, h);
}

Override getPreferredSize() in the panel to return a default value (eg 400x400) if it has no image, and the size of the image otherwise. 如果没有图像,则覆盖面板中的getPreferredSize()以返回默认值(例如400x400),否则返回图像的大小。

After setting an image, call frame.pack() . 设置图像后,调用frame.pack()

BTW - if all the panel does is paint the image, I'd opt for a JLabel instead. 顺便说一句-如果面板所做的只是绘制图像,我会选择JLabel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM