Pet*_*ete 2 java javafx compiler-warnings
我正在学习JavaFX,特别是尝试使用TableColumn和TableView类来实现一个表.我的程序编译没有错误,但在编译时我收到一个不祥的警告:
C:\JavaFX_Sandbox>javac robotApp.java
Note: robotApp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\JavaFX_Sandbox>
Run Code Online (Sandbox Code Playgroud)
这足以让我足够强大,以至于我没有勇气尝试实际运行我的程序.我在StackOverflow和其他一些网站上做了一些挖掘,并发现这个警告的最可能原因是我正在使用原始数据类型.但如果是这样,我无法发现它们.如果没有,我担心这个消息可能意味着什么.
尽快描述我的程序:我创建了一个书呆子robot.java类:
public class robot{
private String name;
private int intelligence;
public robot(String a, int b){
this.name = a;
this.intelligence = b;
}
}
Run Code Online (Sandbox Code Playgroud)
没有什么花哨.为了存储所有这些机器人,我创建了一个robotMgr.java类,基本上是一个美化的ArrayList来保存它们:
import java.util.ArrayList;
import java.util.List;
import javafx.collections.*;
public class robotMgr{
private ArrayList<robot> robotList;
private ObservableList<robot> oRobotList;
robotMgr(){
robotList = new ArrayList<robot>();
robotList.add(new robot("R2-D2", 7));
robotList.add(new robot("Cylon", 3));
robotList.add(new robot("The Terminator", 5));
robotList.add(new robot("WALL-E", 6));
populateOList();
}
public void populateOList(){
for(int i=0; i<robotList.size(); i++)
oRobotList.add(robotList.get(i));
}
public List<robot> getRobotList(){
return robotList;
}
public ObservableList<robot> getORobotList(){
return oRobotList;
}
}
Run Code Online (Sandbox Code Playgroud)
(我在ArrayList中进行了更多的数据操作,但ObservableList是必需的,因为TableColumn类需要从ObservableList中提供表元素.)
现在为主事件:一个JavaFX程序,它获取上面的数据并将其显示在一个简单的两列表中:
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.collections.*;
public class robotApp extends Application{
public static void main(String[] args){
launch(args);
}
TableColumn<robot, String> colName;
TableColumn<robot, Integer> colIntelligenge;
TableView<robot> robotTable;
BorderPane borderPane;
Scene scene;
Stage stage;
@Override public void start(Stage primaryStage){
robotMgr myBots = new robotMgr();
colName = new TableColumn<robot, String>("Name");
colName.setMinWidth(100);
colName.setCellValueFactory(new PropertyValueFactory<robot, String>("Name"));
colIntelligenge = new TableColumn<robot, Integer>("Intelligence");
colIntelligenge.setMinWidth(100);
colIntelligenge.setCellValueFactory(new PropertyValueFactory<robot, Integer>("Intelligence"));
robotTable = new TableView<robot>();
robotTable.getColumns().addAll(colName, colIntelligenge);
robotTable.setItems(myBots.getORobotList());
borderPane = new BorderPane();
borderPane.setCenter(robotTable);
scene = new Scene(borderPane, 600, 600);
stage = primaryStage;
stage.setScene(scene);
stage.setTitle("Robot App");
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
一切看起来都不错.除了我上面提到的警告信息之外,一切都编译得很好.奇怪的是,如果我注释掉"robotTable.getColumns().addAll(colName,colIntelligenge);",消息就会消失.只行.
所以......我完全不知所措.如果错误消息警告我使用原始数据类型,我不知道它们在我的程序中的位置.如果错误消息是关于其他事情的警告,我不知道那可能是什么.另外:为什么addAll()方法会导致警告?我不能为我的生活找到那个.
问题是机器人的实例变量都是私有的吗?如果是这样,它不会在编译时创建特定的语法错误吗?或者ObservableList是问题吗?理解OL是接口,而不是数据结构...但我不知道这会如何让我在这里绊倒.
如果有人能提供一些帮助或建议,我将非常感激.
非常感谢!-RAO
您的代码运行起来非常安全(它有错误,您在运行它时会发现它们,但这些错误与您提出的类型安全无关).
基本上,问题是addAll()您调用的方法采用"varargs"参数,其类型是泛型类型.对于a TableView<S>,TableView.getColumns()返回an 然后调用ObservableList<TableColumn<S,?>>的ObservableList<E> addAll()方法接受一个vararg参数:addAll(E... elements).所以你最终调用了一个带签名的方法addAll(TableColumn<Robot, ?>... columns).
通过将Varargs转换为数组来处理Varargs,因此TableColumn<Robot, ?>[]在调用addAll方法时隐式创建一个类型数组.由于数组和参数化类型不能很好地协同工作,因此编译器无法确保此数组的类型安全性.因此,它发出警告.见,例如这里为编译器为什么不能在这里确保类型安全的例子.
该addAll()方法的作者可能应该对其进行编码,以便确保类型安全并对其进行注释以避免此警告(如果可能的话:我需要考虑它比现在更长的时间).
要避免警告,您只需调用该add方法两次:
robotTable.getColumns().add(colName);
robotTable.getColumns().add(colIntelligenge);
Run Code Online (Sandbox Code Playgroud)
或者,您可以创建List表列并将其传递给addAll(...)采用集合的(其他)方法,而不是varargs参数:
robotTable.getColumns().addAll(Arrays.asList(colName, colIntelligence));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1083 次 |
| 最近记录: |