好吧,我在這里有一個小問題,我想將影像添加到 JOptionPane 但我不能,因為我將它轉換為字串!我不知道怎么解決!幫助!這是代碼:
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
public class TextBox {
ImageIcon icon0 = new ImageIcon("Picture2_2.png");
String user_input = JOptionPane.showInputDialog(null, "Please enter a card number!", "?Tu puedes!", JOptionPane.INFORMATION_MESSAGE,icon0,null,"");
public static void main (String [] args)
{
int g = -1;
while (g<0)
{
TextBox n = new TextBox();
if(n.user_input.length() > 0 )
{
numbersreader t= new numbersreader();
t.checkLuhn (n.user_input);
g ;
}else
{
ImageIcon icon1 = new ImageIcon("Picture2_2.png");
JOptionPane.showInputDialog(null, "Please enter a card number!", "?Ya valio!", JOptionPane.INFORMATION_MESSAGE,icon1, null, "");
}
}
}
}
如果不是因為這個,代碼確實有效,下面是一個有效的代碼版本,但我真的想把那張圖片放在那個 JOptionPane 中,一定有辦法!
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
public class TextBox {
String user_input = JOptionPane.showInputDialog(null, "Please enter a card number!", "?Tu puedes!", JOptionPane.INFORMATION_MESSAGE);
public static void main (String [] args)
{
int g = -1;
while (g<0)
{
TextBox n = new TextBox();
if(n.user_input.length() > 0 )
{
numbersreader t= new numbersreader();
t.checkLuhn (n.user_input);
g ;
}else
{
ImageIcon icon1 = new ImageIcon("Picture2_2.png");
JOptionPane.showInputDialog(null, "Please enter a card number!", "?Ya valio!", JOptionPane.INFORMATION_MESSAGE,icon1, null, "");
}
}
}
}
uj5u.com熱心網友回復:
當我嘗試編譯您的代碼時,編譯器告訴我“錯誤:不兼容的型別:物件無法轉換為字串”。
那是因為JOptionPane.showInputDialog()回傳了一些物件,但是在第 6 行你嘗試將回傳值分配給一個String變數。
將第 6 行更改為
String user_input = (String) JOptionPane.showInputDialog(null, "Please enter a card number!", "?Tu puedes!", JOptionPane.INFORMATION_MESSAGE,icon0,null,"");
(并提供一個 png 檔案“Picture2_2.png”和一個numbersreader帶有方法的類void checkLuhn(String s) {})我可以編譯和運行你的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/428114.html
上一篇:按名稱搜索JPanel
