Java,如何将对话框弹出为仅图像?

时间:2013-03-06 21:30:02

标签: java image swing jdialog

我正在尝试找到一种方法将JDialog的所有内容替换为简单的图像。 这是我正在处理的项目的大约页面,我希望当用户点击“关于”部分时,一个图像以JDialog的样式弹出(并在焦点丢失时消失)。 示例:http://www.tecmint.com/wp-content/uploads/2012/08/About-Skype.jpg Skype只显示他们创建的图像作为“关于”页面。 如何在Java(swing)中创建“图像对话框”?

3 个答案:

答案 0 :(得分:6)

  

如何在Java(swing)中创建“图像对话框”?

使用未修饰的JDialog和包含ImageIcon的JLabel:

JDialog dialog = new JDialog();
dialog.setUndecorated(true);
JLabel label = new JLabel( new ImageIcon(...) );
dialog.add( label );
dialog.pack();
dialog.setVisible(true);

答案 1 :(得分:3)

BufferedImage image = ImageIO.read(new File("myfile.png"));
JLabel picLabel = new JLabel(new ImageIcon(image));
JOptionPane.showMessageDialog(null, picLabel, "About", JOptionPane.PLAIN_MESSAGE, null);

答案 2 :(得分:3)

在这里,我为您注释了代码

import javax.swing.JOptionPane; //imports
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.Dimension;

public class img{

  public static void main(String[] args){

    JFrame f = new JFrame(); //creates jframe f

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //this is your screen size

    f.setUndecorated(true); //removes the surrounding border

    ImageIcon image = new ImageIcon(diceGame.class.getResource("image.png")); //imports the image

    JLabel lbl = new JLabel(image); //puts the image into a jlabel

    f.getContentPane().add(lbl); //puts label inside the jframe

    f.setSize(image.getIconWidth(), image.getIconHeight()); //gets h and w of image and sets jframe to the size

    int x = (screenSize.width - f.getSize().width)/2; //These two lines are the dimensions
    int y = (screenSize.height - f.getSize().height)/2;//of the center of the screen

    f.setLocation(x, y); //sets the location of the jframe
    f.setVisible(true); //makes the jframe visible


  }
}

[[OLD]] 下面的代码将执行您正在寻找的内容。

import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class img{

  public static void main(String[] args){

    JLabel lbl = new JLabel(new ImageIcon(diceGame.class.getResource("image.png")));
    JOptionPane.showMessageDialog(null, lbl, "ImageDialog", 
                                 JOptionPane.PLAIN_MESSAGE, null);



  }
}