簡體   English   中英

Java,如何將對話框彈出為僅圖像?

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

我正試圖找到一種方法將JDialog的所有內容替換為簡單的圖像。 這是我正在處理的項目的大約頁面,我希望當用戶點擊“關於”部分時,一個圖像以JDialog的樣式彈出(並在焦點丟失時消失)。 示例: http//www.tecmint.com/wp-content/uploads/2012/08/About-Skype.jpg Skype只顯示他們創建的圖像作為“關於”頁面。 如何在Java(swing)中創建“圖像對話框”?

如何在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);
BufferedImage image = ImageIO.read(new File("myfile.png"));
JLabel picLabel = new JLabel(new ImageIcon(image));
JOptionPane.showMessageDialog(null, picLabel, "About", JOptionPane.PLAIN_MESSAGE, null);

在這里,我為您注釋了代碼

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



  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM