简体   繁体   English

Java Swing图像不显示?

[英]Java Swing Image Not Displaying?

I have written an applet in Java as a part of my programming class that takes a person's birthday and finds the day of the week on which they were born. 我已经用Java编写了一个applet,作为我编程课程的一部分,该程序花了一个人的生日,并找出了他们出生的星期几。 As per the assignment specifications, we are to put this applet on our Amazon EC2 virtual servers as well. 根据分配规范,我们还将这个小程序也放置在我们的Amazon EC2虚拟服务器上。

Now, when the person is selected from a JTable, the program takes their information as well as the path to an image file also located in the JTable beside their info. 现在,当从JTable中选择该人时,程序将获取他们的信息以及指向同样位于JTable中信息旁边的图像文件的路径。 So, for example, you could have the selection consisting of: 因此,例如,您可以选择以下内容:

| | John Doe | 约翰·杜 17 | 17 | 02 | 02 | 2013 | 2013 | /images/John.jpg | /images/John.jpg |

When I run this on my local machine, everything works as expected - the date is calculated and the image is displayed. 当我在本地计算机上运行此程序时,一切都会按预期进行-计算日期并显示图像。 However, when I put it on my server, one of two things happens: 但是,当我将其放在服务器上时,会发生以下两种情况之一:

  1. If I put the "display date" code before the "display image" code, then when I press the "Calculate" button, only the text displays and the image does not. 如果我将“显示日期”代码放在“显示图像”代码之前,那么当我按下“计算”按钮时,仅显示文本,而图像则不显示。
  2. If I put the "display image" code before the "display date" code, nothing happens when I press the "Calculate" button. 如果我将“显示图像”代码放在“显示日期”代码之前,那么当我按下“计算”按钮时,什么也不会发生。

What might be happening here? 这里可能会发生什么? My images are still in the "images/Name.jpg" path, and I even tried using the entire path (" https://myUsername.course.ca/assignment/images/Name.jpg "). 我的图像仍位于“ images / Name.jpg”路径中,甚至尝试使用整个路径(“ https://myUsername.course.ca/assignment/images/Name.jpg ”)。 Neither works! 都不行! Would there be any obvious reason for this odd behaviour? 这种奇怪的行为会有明显的原因吗?

/**
 * Method that handles the user pressing the "Calculate" button
 */
private class btnCalculateHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        int result;

        name = (String)table.getValueAt(table.getSelectedRow(), 0);

        day = Integer.parseInt((String)table.getValueAt(table.getSelectedRow(), 1));
        month = Integer.parseInt((String)table.getValueAt(table.getSelectedRow(), 2));
        year = Integer.parseInt((String)table.getValueAt(table.getSelectedRow(), 3));

        result = calculateDayOfWeek(day, month, year);

        writeToFile();

        ImageIcon imgPerson = new javax.swing.ImageIcon((String)table.getValueAt(table.getSelectedRow(), 4));
        Image person = imgPerson.getImage();
        Image personResized = person.getScaledInstance(75, 100, java.awt.Image.SCALE_SMOOTH);
        ImageIcon imgPersonResized = new ImageIcon(personResized);
        image.setIcon(imgPersonResized);

        outputValue.setText(name + " was born on a " + days[result] + ".");
    }
}

The first problem I see is this.... 我看到的第一个问题是...

ImageIcon imgPerson = new javax.swing.ImageIcon((String)table.getValueAt(table.getSelectedRow(), 4))

ImageIcon(String) is used to specify a file name of the image. ImageIcon(String)用于指定图像的文件名。 This should be used for loading images of a local disk, not a network path. 这应该用于加载本地磁盘而不是网络路径的映像。

If the images are loaded relative to the to the applet, you would use Applet#getImage(URL, String) passing it a reference of Applet#getDocumentBase() 如果图像是相对于Applet加载的,则可以使用Applet#getImage(URL, String)传递一个Applet#getDocumentBase()的引用。

Something like getImage(getDocumentBase(), (String)table.getValueAt(table.getSelectedRow(), 4)) 类似于getImage(getDocumentBase(), (String)table.getValueAt(table.getSelectedRow(), 4))

A better choice would be to use ImageIO . 更好的选择是使用ImageIO The main reason for this is that it won't use a background thread to load the image and will throw a IOException if something goes wrong, making it easier to diagnose any problems... 这样做的主要原因是,它不会使用后台线程来加载图像,并且如果出现问题,将抛出IOException ,从而更容易诊断任何问题...

Something like... 就像是...

BufferedImage image = ImageIO.read(new URL(getDocumentBase(), (String)table.getValueAt(table.getSelectedRow(), 4)));

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

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