2017-09-15 62 views
0

我試圖做一個splitpane GUI對象在Java中的鏈接所描述的:http://www.cs.fsu.edu/~jtbauer/cis3931/tutorial/ui/swing/example-swing/SplitPaneDemo.javaJava:如何將文件中的圖像添加到ResourceBundle中?

但我真的不明白這個部分:所以基本上是要求我

try { 
     imageResource = ResourceBundle.getBundle("imagenames"); 
     String imageListString = imageResource.getString("images"); 
     imageList = parseList(imageListString); 
    } catch (MissingResourceException e) { 
     System.err.println("Can't find list of image names."); 
     System.exit(-1); 
} 

從名爲'圖像名稱'的文件中創建一個資源包,其中包含我想要使用的圖像列表?我不明白resourcebundle是如何工作的,我在網上看到的例子都使用對象或數組。我如何使用resourcebundle從文件中獲取圖像以便使用?

+0

也許你應該開始烯與DCR = 0&q = java的+蝕+資源+捆綁&拼寫= 1&SA = X&VED = 0ahUKEwii9pzrhKbWAhXFmpQKHS_wBJIQBQglKAA&BIW = 840&波黑= 885) – MadProgrammer

回答

0

我想有一個屬性文件「imagenames」,其中包含鍵值「images」,其值由空格分隔「」。

images=image1name.png img2name.png img3name.png ... 

您可以看到,在parseList方法中,將字符串解析爲圖像名稱數組。

protected static Vector parseList(String theStringList) { 
    Vector v = new Vector(10); 
     StringTokenizer tokenizer = new StringTokenizer(theStringList, " "); 
     while (tokenizer.hasMoreTokens()) { 
      String image = tokenizer.nextToken(); 
      v.addElement(image); 
     } 
    return v; 
    } 

和例如在SplitPaneDemo方法中:

// Set up the picture label and put it in a scroll pane 
     currentImage = new ImageIcon("images/" + 
        (String)imageList.firstElement()); 

您從圖像列表中獲取「圖像」dir +圖像名稱中的第一張圖像。

currentImage = new ImageIcon("images/image1name.png")

總,你應該建立一個目錄,添加圖片,創建屬性文件「imagenames」與按鍵「圖像」和values = yourImgName1 yourImgName2 ....例如分離由空格

images=yourImgName1 yourImgName2 ... 

並使用您的示例從目錄中獲取圖像。

工作原理看做一個快速[谷歌](https://www.google.com.au/search?safe=off&client=safari&rls= http://www.avajava.com/tutorials/lessons/how-do-i-read-a-properties-file-with-a-resource-bundle.html

​​

相關問題