2013-03-06 58 views
4

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

回答

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 


    } 
} 

[[老] 下面的代碼會做你要找的內容。

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



    } 
} 
+0

看起來不錯,但我能做些什麼來去除的JOptionPane的窗口邊框,使圖像是唯一剩下的東西? – vejmartin 2013-03-06 22:00:32

+0

我已經爲你編輯了答案。 – MeryXmas 2013-03-06 22:31:23

+0

'ImageIcon image = new ImageIcon(diceGame.class.getResource(「image.png」));''從哪裏來,我的意思是明確的diceGame。爲什麼它不是簡單的「img.class〜」,因爲該類被稱爲? – N30 2014-05-07 11:09:13

5

我怎樣才能讓在Java中(搖擺) 「圖像對話」?

使用未修飾的JDialog包含一個ImageIcon一個JLabel:

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