如何将 ImageIcon 添加到对象?
How to add an ImageIcon to an object?
我的对象 'Team' 带有一些分数变量、名称 (String) 和徽标。徽标的类型为 ImageIcon:
public class Team {
public String name;
public ImageIcon logo;
public int points;
public int plusGoals;
public int minGoals;
public int goalsTotal;
public Team (String name, ImageIcon logo, int points, int plusGoals, int minGoals, int goalsTotal){
this.name = name;
this.logo = logo;
this.points = points;
this.plusGoals = plusGoals;
this.minGoals = minGoals;
goalsTotal = plusGoals - minGoals;
当我想创建一个新对象时,我输入了对象属性的值,但我不知道如何添加 ImageIcon 路径。
所以:
Team Blabla = new Team("Blabla", ..., 0, 0, 0, 0);
我试过这些东西,但它们不起作用:
Team Blabla = new Team("Blabla", C:\Users\path.png, 0, 0, 0, 0);
Team Blabla = new Team("Blabla", "C:\Users\path.png", 0, 0, 0, 0);
Team Blabla = new Team("Blabla", ImageIcon("C:\Users\path.png"), 0, 0, 0, 0);
如何在这一行直接添加图片路径?
你可以这样修改:
public Team(String name, String location, int points, int plusGoals,
int minGoals, int goalsTotal) {
this.logo = new ImageIcon(location); // using ImageIcon(URL location)
}
Note: Here we are using ImagIcon class Constructor ->
ImageIcon(URL location) which Creates an ImageIcon from the specified URL.
工作代码
import javax.swing.ImageIcon;
class Team {
public String name;
public ImageIcon logo;
public int points;
public int plusGoals;
public int minGoals;
public int goalsTotal;
public Team(String name, String location, int points, int plusGoals,
int minGoals, int goalsTotal) {
this.logo = new ImageIcon(location); // using ImageIcon(URL location)
this.name = name;
this.points = points;
this.plusGoals = plusGoals;
this.minGoals = minGoals;
goalsTotal = plusGoals - minGoals;
}
public void print() {
System.out.println("\n" + name + "\n" + logo + "\n" + points + "\n"
+ plusGoals + "\n" + minGoals + "\n" + goalsTotal);
}
}
public class imageicon {
public static void main(String[] args) {
Team obj = new Team("a", "C:\Users\path.png", 1, 2, 3, 4);
obj.print();
}
}
我的对象 'Team' 带有一些分数变量、名称 (String) 和徽标。徽标的类型为 ImageIcon:
public class Team {
public String name;
public ImageIcon logo;
public int points;
public int plusGoals;
public int minGoals;
public int goalsTotal;
public Team (String name, ImageIcon logo, int points, int plusGoals, int minGoals, int goalsTotal){
this.name = name;
this.logo = logo;
this.points = points;
this.plusGoals = plusGoals;
this.minGoals = minGoals;
goalsTotal = plusGoals - minGoals;
当我想创建一个新对象时,我输入了对象属性的值,但我不知道如何添加 ImageIcon 路径。
所以:
Team Blabla = new Team("Blabla", ..., 0, 0, 0, 0);
我试过这些东西,但它们不起作用:
Team Blabla = new Team("Blabla", C:\Users\path.png, 0, 0, 0, 0);
Team Blabla = new Team("Blabla", "C:\Users\path.png", 0, 0, 0, 0);
Team Blabla = new Team("Blabla", ImageIcon("C:\Users\path.png"), 0, 0, 0, 0);
如何在这一行直接添加图片路径?
你可以这样修改:
public Team(String name, String location, int points, int plusGoals,
int minGoals, int goalsTotal) {
this.logo = new ImageIcon(location); // using ImageIcon(URL location)
}
Note: Here we are using ImagIcon class Constructor -> ImageIcon(URL location) which Creates an ImageIcon from the specified URL.
工作代码
import javax.swing.ImageIcon;
class Team {
public String name;
public ImageIcon logo;
public int points;
public int plusGoals;
public int minGoals;
public int goalsTotal;
public Team(String name, String location, int points, int plusGoals,
int minGoals, int goalsTotal) {
this.logo = new ImageIcon(location); // using ImageIcon(URL location)
this.name = name;
this.points = points;
this.plusGoals = plusGoals;
this.minGoals = minGoals;
goalsTotal = plusGoals - minGoals;
}
public void print() {
System.out.println("\n" + name + "\n" + logo + "\n" + points + "\n"
+ plusGoals + "\n" + minGoals + "\n" + goalsTotal);
}
}
public class imageicon {
public static void main(String[] args) {
Team obj = new Team("a", "C:\Users\path.png", 1, 2, 3, 4);
obj.print();
}
}