samedi 27 août 2016

How to check JFrame icon is set successfully?

thanks for reading as always.

Overview: I have a simple Icon class as below:

public class FrameIcons {

ImageIcon Img = new ImageIcon();

private String iconFilePath;
private String missingIconException = "Failed to load application icon";
private String missingIconTitle = "No Icon could be found";



public String getMissingIconException() {
    return missingIconException;
}

public void setIconFilePath(String Filepath) {
    this.iconFilePath = Filepath;
}

public String getIconFilePath() {
    return iconFilePath;
}

public String getMissingIconTitle() {
    return missingIconTitle;
}

I am writing a test class for this class and I am wondering the easiest way for me to confirm that the icon specified there is the correct one?

Here is my test class:

    public static void main(String[] args) {
    System.out.println("Test01 - Successful Icon Test");
    FrameIcons iconTest = new FrameIcons();
    iconTest.setIconFilePath("res/icon.png");
    JFrame Frame = new JFrame();
    Frame.setSize(200, 200);
    Frame.setVisible(true);
    Frame.setLocationRelativeTo(null);

    try {
         Frame.setIconImage(ImageIO.read(new FileInputStream(iconTest.getIconFilePath())));
    } catch (IOException ioReadIcon) {
        ioReadIcon.printStackTrace();
        JOptionPane.showMessageDialog(null, iconTest.getMissingIconException() + ioReadIcon, iconTest.getMissingIconTitle() ,JOptionPane.WARNING_MESSAGE);
    }


}

Say I had a number of different .pngs I could set as my icon here, how do I verify it has successfully displayed in the JFrame?

Thanks!

Aucun commentaire:

Enregistrer un commentaire