總網頁瀏覽量

2015年10月27日 星期二

[Java] Dialog 的範例 showInputDialog

參考資料:
https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

import javax.swing.JOptionPane;

public class Dialog_001{
    public static void main(String args[]){
        String name = JOptionPane.showInputDialog("Name: ","AVC");
        //第二格就變程預設值, 可以字串也可以整數,試了連浮點數都行
        String message = String.format("Welcome, %s, to me",name);
        JOptionPane.showMessageDialog(null,message);
    }
}
 ------------------------------------------------------------------------------------------
 
 ------------------------------------------------------------------------------------------

Icons used by JOptionPane
Icon description Java look and feel Windows look and feel
question The Java look and feel icon for dialogs that ask questions The Windows look and feel icon for dialogs that ask questions
information The Java look and feel icon for informational dialogs The Windows look and feel icon for informational dialogs
warning The Java look and feel icon for warning dialogs The Windows look and feel icon for warning dialogs
error The Java look and feel icon for error dialogs The Windows look and feel icon for error dialogs


showMessageDialog方法中, 針對不同的用途用不同參數來顯示User要顯示的東西.


 import javax.swing.JOptionPane;

public class Dialog_001{
    public static void main(String args[]){
        String name = JOptionPane.showInputDialog("Name: ","Ha");
        String message = String.format("Input is : %s.",name);
        JOptionPane.showMessageDialog(null,message,"Title",JOptionPane.PLAIN_MESSAGE);
    }
}

showMessageDialog 的第三個和第四個參數,
要就都要有, 不然就都不要有,不然會編譯錯誤:
Dialog_001.java:9: error: no suitable method found for showMessageDialog(<null>,String,String)
        JOptionPane.showMessageDialog(null,message,"Title");
                   ^
    method JOptionPane.showMessageDialog(Component,Object) is not applicable
      (actual and formal argument lists differ in length)
    method JOptionPane.showMessageDialog(Component,Object,String,int) is not applicable
      (actual and formal argument lists differ in length)
    method JOptionPane.showMessageDialog(Component,Object,String,int,Icon) is not applicable
      (actual and formal argument lists differ in length)
1 error

第三個參數代表在按下確定之後,
顯示出來的視窗標題, 不寫的話就只會顯示 Message,
而第四個參數則是表示視窗要用哪種方式顯示.

都沒填就會顯示:








JOptionPane.WARNING_MESSAGE:





JOptionPane.ERROR_MESSAGE:


JOptionPane.PLAIN_MESSAGE:
 

JOptionPane.INFORMATION_MESSAGE,icon

在這裡 icon是第五個參數,
要使用時要先 import javax.swing.ImageIcon ;
用 ImageIcon icon = new ImageIcon("C:\\Documents\\b.jpg");
在參數部分試圖片檔案的完整路徑,
用過jpg跟png都可以.

 


















沒有留言:

張貼留言