java - 仅使用一种方法重置与 Swing 不同
问题描述
我有几种表格,我想用一种方法重置。我试过这个并且它有效,但我想避免使用 instanceof 链。
reset.addActionListener(e -> Listener.clear(a,b,c,d));
static void clear(JComponent ... fields) throws IllegalArgumentException{
Stream.of(fields).forEach(Listener::clear);
}
static void clear(JComponent field) throws IllegalArgumentException{
if(field instanceof JTextComponent a) a.setText("");
else if(field instanceof DatePicker a) a.setDate(null);
else if(field instanceof DateTimePicker a) a.setDateTimeStrict(null);
else if(field instanceof JComboBox a) a.setSelectedIndex(-1);
else if(field instanceof JSpinner a) a.setValue(1);
else throw new IllegalArgumentException("Clear not valid");
}
我该如何解决?
解决方案
推荐阅读
- r - 从 quanteda dfm 中提取原始文本以用于 stm
- javascript - 如何为我的所有 JSON 对象键生成选择选项?
- r - 为因子分析计算 omega:NA 结果
- javascript - removeCue() 函数后如何刷新 TextTrack
- sql-server - 在 SELECT 语句中编写完整路径会提高 SQL 的性能吗?
- html - 如何跨 JSON (AngularJS) 创建集合
- applescript - Applescript 处理程序,重复 i 从 1 到 this_list 的编号
- powerbi - Power BI 中的日期列分为 4 列
- node.js - 为什么我们需要 node 应用程序根目录的 package.json 中的 main 属性,如果它是为了告诉包和模块的入口点?
- python - 如何计算子目录中的文件数?