我从我的项目src文件中抓取了imageicon,如下所示:
icon = new ImageIcon(ViewDetailsV.class.getResource("/brandnew/images/male_avatar.jpg"));
Run Code Online (Sandbox Code Playgroud)
然后我想放入这一行,以便调整大小到标签的大小:
lblPhotoField.setIcon(new ImageIcon(ScaledImage(icon, lblPhotoField.getWidth(), lblPhotoField.getHeight()));
Run Code Online (Sandbox Code Playgroud)
但我需要"图标"作为图像,而不是图像图像,因为ScaledImage采用Image参数而不是ImageIcon.
private Image ScaledImage(Image img, int w, int h){
BufferedImage resizedImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0,0,w,h,null);
g2.dispose();
return resizedImage;
}
Run Code Online (Sandbox Code Playgroud)
是的,我确实尝试将返回值更改为ImageIcon,但它给了我错误.所以我现在专注于如何将ImageIcon转换为Image,这样我就可以将它放在方法行中,获取图标并将我的标签设置为:
lblPhotoField.setIcon(icon);
Run Code Online (Sandbox Code Playgroud) 我想计算字符串中的单词数。就像如果给定一个字符串:
string str = "Hello! How are you?";
Run Code Online (Sandbox Code Playgroud)
那么输出将是:
Number of words in string “Hello! How are you?” is 4.
Run Code Online (Sandbox Code Playgroud)
我正在使用 for 循环,这些是我当前的代码。
string wordCountStr = "";
int noOfWords = 0;
private void btn_Computate4_Click(object sender, EventArgs e)
{
wordCountStr = tb_Qns4Input.Text.ToString(); //tb_Qns4Input is a textbox.
for (int i = 0; i< wordCountStr.Length; i++)
{
//I don't know how to code here.
}
lbl_ResultQns4.Text = "Number of words in string " + wordCountStr + " is " + noOfWords;
}
Run Code Online (Sandbox Code Playgroud)
哦,是的,我正在使用 …