在设定大小的 JPanel 中显示从 JFileChooser 选择的图像(图像必须重新缩放)

发布于 2024-11-25 01:45:54 字数 2685 浏览 2 评论 0原文

我正在尝试将图像放入设定大小的 JPanel(图片面板---黑色边框线)中(必须适合 JPANEl“图片面板”)。当我单击上传按钮时,我能够在新的 JFrame 中看到 JFilechooser 并选择我想要的图片,但是在单击“打开”按钮后没有任何反应。

import classes.BackgroundPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileNameExtensionFilter;


public class Test
{

    public static void main(String[] args) {
        final JFileChooser chooser = new JFileChooser();
        JButton button = new JButton();
        button.setText("Upload");
        JFrame frame = new JFrame("My Frame");
        final JFrame imageFrame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFileChooser fc = new JFileChooser();
        final Test_Image t = new Test_Image();
        JPanel panel = new JPanel();
        JPanel picturePanel = new JPanel();
     //  chooser.showOpenDialog(null);
          Dimension d = new Dimension(1261, 765);
          Dimension d2 = new Dimension(1300, 900);
          picturePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          Dimension d3 = new Dimension(343, 247);
          picturePanel.setSize(d3);
            //picturePanel.setSize(d);
          panel.add(button);
          panel.setSize(d3);


        //panel.setVisible(true);
       //panel.add(picturePanel);

       button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                if(chooser.showOpenDialog(imageFrame) == JFileChooser.APPROVE_OPTION) {

                    try {
                        Image bi = ImageIO.read(
                        chooser.getSelectedFile());
                        BackgroundPanel bp = new BackgroundPanel(bi);
                        if (bi != null)
                        bp.setImage(bi);
                        else
                        JOptionPane.showMessageDialog(imageFrame,
                        "File is not an image!");
                        } catch (IOException ioe) {
                        JOptionPane.showMessageDialog(imageFrame,
                        "Error Reading File!");
}                
                }                              

            }
        });      


      frame.setSize(d2);
      frame.add(picturePanel).setLocation(100, 100);
      frame.add(panel);
      frame.setVisible(true);

    }

}

I am trying to fit an image in a set sized JPanel (picture panel--- black border line) (MUST FIT THE JPANEl "Picture panel"). When i click the upload button, i am able to see the JFilechooser in a new JFrame and select the picture i want, however after the 'open' button click nothing happens.

import classes.BackgroundPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileNameExtensionFilter;


public class Test
{

    public static void main(String[] args) {
        final JFileChooser chooser = new JFileChooser();
        JButton button = new JButton();
        button.setText("Upload");
        JFrame frame = new JFrame("My Frame");
        final JFrame imageFrame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFileChooser fc = new JFileChooser();
        final Test_Image t = new Test_Image();
        JPanel panel = new JPanel();
        JPanel picturePanel = new JPanel();
     //  chooser.showOpenDialog(null);
          Dimension d = new Dimension(1261, 765);
          Dimension d2 = new Dimension(1300, 900);
          picturePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          Dimension d3 = new Dimension(343, 247);
          picturePanel.setSize(d3);
            //picturePanel.setSize(d);
          panel.add(button);
          panel.setSize(d3);


        //panel.setVisible(true);
       //panel.add(picturePanel);

       button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                if(chooser.showOpenDialog(imageFrame) == JFileChooser.APPROVE_OPTION) {

                    try {
                        Image bi = ImageIO.read(
                        chooser.getSelectedFile());
                        BackgroundPanel bp = new BackgroundPanel(bi);
                        if (bi != null)
                        bp.setImage(bi);
                        else
                        JOptionPane.showMessageDialog(imageFrame,
                        "File is not an image!");
                        } catch (IOException ioe) {
                        JOptionPane.showMessageDialog(imageFrame,
                        "Error Reading File!");
}                
                }                              

            }
        });      


      frame.setSize(d2);
      frame.add(picturePanel).setLocation(100, 100);
      frame.add(panel);
      frame.setVisible(true);

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日暮斜阳 2024-12-02 01:45:54
BackgroundPanel bp = new BackgroundPanel(bi); 

您不要将BackgroundPane 添加到框架中的任何位置。动态添加组件的基本代码是:

panel.add( someComponent );
panel.revalidate();
panel.repaint();

如果您已经将BackgroundPanel 添加到框架中,那么您应该能够在面板上调用setImage() 方法。因此,现在您需要更改代码以在从文件选择器选择路径后使用 ImageIO.read(...) 读取图像。

BackgroundPanel bp = new BackgroundPanel(bi); 

You don't add the BackgroundPane to the frame anywhere. The basic code for dynamically adding components is:

panel.add( someComponent );
panel.revalidate();
panel.repaint();

If you have already added the BackgroundPanel to the frame then you should be able to just invoke the setImage() method on the panel. So now you need to change your code to use ImageIO.read(...) to read in the image after you've selected the path from the file chooser.

甜味拾荒者 2024-12-02 01:45:54

再说一次,我没有看到您将BackgroundPanel 添加到picturePanel JPanel 的任何地方。您需要将picturePanel的布局设置为BorderLayout,然后将BackgroundPanel、bp添加到picturePanel的BorderLayout.CENTER位置,然后调用revalidate和repaint,如camickr所示。

即使我们已请求,我们也看不到您的BackgroundPanel 类的代码。再次,我希望您在其paintComponent 方法中绘制图像并使用正确的drawImage 重载,但在您显示代码之前,我们无法确定。

Again, I don't see anywhere where you add the BackgroundPanel to the picturePanel JPanel. You will need to set the picturePanel's layout to BorderLayout and then add the BackgroundPanel, bp to the picturePanel in the BorderLayout.CENTER position, and then call revalidate and repaint as camickr shows you.

We also don't see the code for your BackgroundPanel class even though we've requested it. Again, I hope that you're drawing the image in its paintComponent method and using the proper drawImage overload, but til you show the code, we won't know for sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文