Java,我怎样才能弹出一个对话框作为一个图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15258467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:00:57  来源:igfitidea点击:

Java, how can I popup a dialog box as only an image?

javaimageswingjdialog

提问by vejmartin

I'm trying to find a way to replace all the contents of a JDialog to simply an image. It's for the about page of a project I'm working on and I want when the user clicks on the About section, an image to popup in the style of a JDialog(and to disappear when focus is lost). Example: http://www.tecmint.com/wp-content/uploads/2012/08/About-Skype.jpgThere Skype displays only an image they've created as their "About" page. How can I make an "image dialog" in Java(swing)?

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

回答by camickr

How can I make an "image dialog" in Java(swing)?

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

Use an undecorated JDialog with a JLabel containing an ImageIcon:

将未修饰的 JDialog 与包含 ImageIcon 的 JLabel 一起使用:

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

回答by whiskeyspider

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

回答by MeryXmas

Here you go, I have annotated the code for you

给你,我已经为你注释了代码

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]] The code below will do what you're looking for.

[[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);



  }
}