Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Picture Alignment in JOptionPane

  1. #1
    Visions is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Exclamation Picture Alignment in JOptionPane

    I am making a dice game and am using pictures to display both the user's and the computer's dice roll, but I can't get the computer's dice picture to appear on the right side.

    Picture Alignment in JOptionPane-dicegame.png

    Here is my code:
    Java Code:
    diceLabel.setIcon(new ImageIcon("<html><DIV align='right'>C:\\Users\\default.default-VAIO\\Downloads\\
    Dice Game\\DiceFace" + compRoll + ".png</DIV></html>"));

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Picture Alignment in JOptionPane

    I haven't come across an ImageIcon constructor that takes a HTML String. Where did you get that code?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Visions is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Picture Alignment in JOptionPane

    I just wrote it up because I thought it might fit, since I had to use html code to do the breaks in the text.

  4. #4
    Visions is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Picture Alignment in JOptionPane

    I got it to do this, but I want the computer's die to be on the right side of the text.

    Attachment 4143

    I used this code:

    Java Code:
    if(compRoll > userRoll) {
            	JLabel diceLabel = new JLabel("<html>Your roll: " + userRoll + "<br />Computer's Roll: " + compRoll + "<br />You lost to the computer.</html>");
            	ImageIcon icon = new ImageIcon("C:\\Users\\default.default-VAIO\\Downloads\\Dice Game\\DiceFace" + compRoll + ".png");
            	diceLabel = new JLabel("<html>Your roll: " + userRoll + "<br />Computer's Roll: " + compRoll + "<br />You lost to the computer.</html>", icon, JLabel.RIGHT);
            	diceLabel.setIcon(new ImageIcon("C:\\Users\\default.default-VAIO\\Downloads\\Dice Game\\DiceFace" + userRoll + ".png"));
                JOptionPane.showMessageDialog(null, diceLabel, "Results", JOptionPane.INFORMATION_MESSAGE, icon);
            }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Picture Alignment in JOptionPane

    How about using the JLabel's API. There you'll find methods such as setHorizontalTextPosition(...) that can likely help you.

  6. #6
    Visions is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Picture Alignment in JOptionPane

    I tried that, but the comp's image is staying right next to the user's image

  7. #7
    Visions is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Picture Alignment in JOptionPane

    This is the closest i've been able to come.. I at least want them on the same line.
    Picture Alignment in JOptionPane-dice-game-3.png

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Picture Alignment in JOptionPane

    You can put 3 JLabels in a JPanel and pass that as the message argument, you know.

    To get things where you want them to appear, see this Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 05-24-2012, 04:44 PM
  2. GUI alignment
    By africanhacker in forum New To Java
    Replies: 5
    Last Post: 03-13-2011, 09:22 PM
  3. pls... help.... alignment....
    By eiramae in forum Java Applets
    Replies: 0
    Last Post: 02-26-2011, 06:47 AM
  4. Help with alignment
    By firecat318 in forum AWT / Swing
    Replies: 3
    Last Post: 05-25-2010, 03:01 AM
  5. Label Alignment Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •