Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to put images on JOptionPane

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to put images on JOptionPane

    I have written a program for a quiz. I want to do a picture round but I am wondering is there anyway of placing an image in the JOptionPane.showInputDialog window.

    Here is my code:

    import java.util.*;
    import javax.swing.*;

    class Quiz {

    public static void main(String args[]){

    String question[] = new String[10];//Declare Question Array
    String ans[] = new String[10];// Declare Answer Array
    int correct=0;//Declare correct for keeping count of correct answers
    int incorrect=0;//Declare incorrect for keeping count of incorrect answers
    String input;//Declare input for answers given by user
    int n;//Declare n to be used as the random number below

    Random r = new Random();//declaring r as a random number

    question[0]= "Where is the Sea of Tranquillity?";//moon
    question[1] = "Freddie Bulgara was better known as which lead singer?"; //freddie mercury
    question[2] = "Robert Zimmerman is better known as whom in the music world?"; //bob dylan
    question[3]= "How many teeth in a full adult set?";//32
    question[4] = "Whose secretary was Miss Moneypenny?";//M
    question[5] = "Who was the first person to reach the South Pole?"; //Roald Amundsen
    question[6] = "The zloty is the currency of what country?";//Poland
    question[7] = "What famous Australian landmark was opened in 1932?"; //Sydney Harbour Bridge
    question[8] = "Siam is the former name for which country?";//Thailand
    question[9] = "What do the letters DVD stand for?";//Digital Versatile Disk

    ans[0]="Moon";
    ans[1]="Freddie Mercury";
    ans[2]="Bob Dylan";
    ans[3]= "32";
    ans[4]="M";
    ans[5]="Roald Amundsen";
    ans[6]="Poland";
    ans[7]="Sydney Harbour Bridge";
    ans[8]="Thailand";
    ans[9]="Digital Versatile Disk";

    for (int i =0; i<5; i++){
    n=r.nextInt(10);//Declare n as the random number between 0-9
    input=JOptionPane.showInputDialog(null, question[n]);

    String input1= input.trim();//Trim answer

    if(input1.equalsIgnoreCase(ans[n])){
    correct++;
    }

    else {
    JOptionPane.showMessageDialog(null, "Sorry that was incorrect\n The answer we were looking for was "+ans[n]);
    incorrect++;
    }

    }

    int output1=correct*20;

    JOptionPane.showMessageDialog(null, correct+ " right answers\n" +incorrect+ " incorrect answers\n " + output1+"% Success Rate ");
    }
    }



    For example I want to change question[1] to show a picture of Freddie Mercury and the question "Name this singer?".


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to put images on JOptionPane

    Please use highlight tags when posting code.

    Recommended reading: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2014, 02:28 PM
  2. how to use this variable ? (JOptionPane)
    By erdy_rezki in forum Java Theory & Questions
    Replies: 5
    Last Post: April 24th, 2012, 11:07 PM
  3. While JOptionPane equal Yes????
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 08:57 PM
  4. Display in JOptionPane
    By t-rank in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 19th, 2010, 12:09 AM
  5. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM