简体   繁体   English

Java Swing - 如何使用泛型在Swing组件上创建utils类

[英]Java Swing - How to create utils classes on Swing components with generics

I have a Utils class such that: 我有一个Utils类,以便:

public static void setComboBoxDefaultWidth(JComboBox cb)
{
    cb.setPrototypeDisplayValue("mmmmmmmmmmmmmmmmmmmmmmmmmm");
}

The problem is that this results in a compiler warning: 问题是这会导致编译器警告:

JComboBox is a raw type. JComboBox是原始类型。 References to generic type JComboBox should be parameterized 应参数化对泛型类型JComboBox的引用

I'm not sure how to parameterize the generic so that the calling method and the function both work (no compiler errors). 我不确定如何参数化泛型,以便调用方法和函数都工作(没有编译器错误)。 I can resolve one but not both for whatever reason... 无论出于何种原因,我都可以解决一个问题

The fact is that JComboBox wasn't generic in Java6 but it became generic with 7 just because the design was flawed (since getItemAt() returned an Object type you had to manually cast it). 事实上, JComboBox在Java6中并不是通用的,但它因为设计存在缺陷而变得通用7(因为getItemAt()返回了一个你必须手动转换它的Object类型)。

The method is declared as 该方法声明为

public void setPrototypeDisplayValue(E prototypeDisplayValue)

This means that, you must have a specific instance of a specific class to call it, and the type must correspond to the one declared for your combo box: 这意味着,您必须具有特定类的特定实例才能调用它,并且类型必须与为组合框声明的类型相对应:

public void setComboBoxDefaultWidth(JComboBox<String> cb) {
  cb.setPrototypeDisplayValue("mmmmmmmmmmmmmmmmmmmmmmmmmm");
}

You are forced to do it because you are passing a String to the method so the JComboBox must contain String s. 您被迫这样做是因为您将String传递给方法,因此JComboBox必须包含String

The above solution is what you need to do when the method that you want to invoke requires a parameter of the generics type. 当您要调用的方法需要泛型类型的参数时,您需要执行上述解决方案。 Otherwise you couldn't specify it (without narrwing how many the target) and you would have to use the wildcard ? 否则你无法指定它(没有缩小目标的数量),你将不得不使用通配符? exists: if a method doesn't care about what is the specific type of the generic class you just need to specify that the JComboBox has a generic type without worrying about what the type is: exists:如果方法不关心泛型类的特定类型,您只需要指定JComboBox具有泛型类型而不必担心类型是什么:

public static void setComboBoxDefaultWidth(JComboBox<?> cb) {
    cb.setLightWeightPopupEnabled(true);
}

The syntax <?> just literally means unknown type , the parameter is indeed a JComboBox of unknown type items. 语法<?>只是字面意思是未知类型 ,参数确实是未知类型项的JComboBox

The prototype display value must of the same type as the one typing your JComboBox. 原型显示值必须与键入JComboBox的类型相同。

Given a JComboBox<E> , you can invoke the method setPrototypeDisplayValue(E prototypeDisplayValue) only if you know the type. 给定JComboBox<E> ,只有在知道类型的情况下才能调用方法setPrototypeDisplayValue(E prototypeDisplayValue) Therefore, using a wildcard (?) is not an option. 因此,使用通配符(?)不是一种选择。 Currently, all you can do is to have: 目前,您所能做的就是拥有:

public static void setComboBoxDefaultWidth(JComboBox<String> cb) {
    cb.setPrototypeDisplayValue("mmmmmmmmmmmmmmmmmmmmmmmmmm");
}

Another option you could have would be this: 您可以拥有的另一个选择是:

public static <E> void setComboBoxDefaultWidth(JComboBox<E> cb, E prototypeValue) {
    cb.setPrototypeDisplayValue(prototypeValue);
}

but it is pointless. 但这毫无意义。

If your JComboBox s always contains String, the syntax would be: 如果您的JComboBox始终包含String,则语法为:

public static void setComboBoxDefaultWidth(JComboBox<String> cb)
{
    cb.setPrototypeDisplayValue("mmmmmmmmmmmmmmmmmmmmmmmmmm");
}

如果你想为任何类型的Generic保留JComboBox,你不能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM