Java 在JComboBox中查看项目的最佳方式
我有一个Java 在JComboBox中查看项目的最佳方式,java,swing,actionlistener,jcombobox,itemlistener,Java,Swing,Actionlistener,Jcombobox,Itemlistener,我有一个JComboBox,里面有很多项目。我将一个项目侦听器添加到此组合框中,该组合框存储所选项目: comboBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { option = (String) e.getItem(); } }); 我有一个按钮,当它被点击时,程
JComboBox
,里面有很多项目。我将一个项目侦听器添加到此组合框中,该组合框存储所选项目:
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
option = (String) e.getItem();
}
});
我有一个按钮,当它被点击时,程序根据选择执行任务
按钮:
public void actionPerformed(ActionEvent e) {
if (option.toLowerCase().compareTo("contrast stretching") == 0) { /* do sth */ }
else if (option.toLowerCase().compareTo("mean") == 0){ /* do sth else */ }
// And many other else if statements
actionPerformed函数太长了。编写代码的最佳方式是什么?我不想使单个函数太长。您可以创建一个表示需要执行的任务的接口(例如,
任务
)。然后为组合框中的每个值创建此接口的实现(contractTask
,meansTask
,…)。最后,在您的actionPerformed
中,编写一个返回正确实现的工厂方法-它基本上是一个“开关”,返回任务的正确实例。然后你就可以运行这个任务了
任务
可能如下所示:
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
public class MeanTask implements Task {
public int execute(int a, int b) {
return (a + b) / 2;
}
}
private Task create(String option) {
if (option.toLowerCase().compareTo("mean") == 0) {
return new MeanTask();
}
else if ....
}
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
//call this method in your constructor
private void initTasks() {
this.tasks.put("option1", new Task() {
public int execute(int a, int b) {
return (a + b) / 2;
}
});
//init more tasks
//this.task.put(..., ...);
}
protected Task getTask(String option) {
return this.task(option);
}
其中一个实现可能如下所示:
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
public class MeanTask implements Task {
public int execute(int a, int b) {
return (a + b) / 2;
}
}
private Task create(String option) {
if (option.toLowerCase().compareTo("mean") == 0) {
return new MeanTask();
}
else if ....
}
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
//call this method in your constructor
private void initTasks() {
this.tasks.put("option1", new Task() {
public int execute(int a, int b) {
return (a + b) / 2;
}
});
//init more tasks
//this.task.put(..., ...);
}
protected Task getTask(String option) {
return this.task(option);
}
您的工厂将如下所示:
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
public class MeanTask implements Task {
public int execute(int a, int b) {
return (a + b) / 2;
}
}
private Task create(String option) {
if (option.toLowerCase().compareTo("mean") == 0) {
return new MeanTask();
}
else if ....
}
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
//call this method in your constructor
private void initTasks() {
this.tasks.put("option1", new Task() {
public int execute(int a, int b) {
return (a + b) / 2;
}
});
//init more tasks
//this.task.put(..., ...);
}
protected Task getTask(String option) {
return this.task(option);
}
这样做的好处是,每个任务的逻辑都很好地封装在自己的类中。您可以创建一个接口(例如,
任务
)来表示需要执行的任务。然后为组合框中的每个值创建此接口的实现(contractTask
,meansTask
,…)。最后,在您的actionPerformed
中,编写一个返回正确实现的工厂方法-它基本上是一个“开关”,返回任务的正确实例。然后你就可以运行这个任务了
任务
可能如下所示:
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
public class MeanTask implements Task {
public int execute(int a, int b) {
return (a + b) / 2;
}
}
private Task create(String option) {
if (option.toLowerCase().compareTo("mean") == 0) {
return new MeanTask();
}
else if ....
}
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
//call this method in your constructor
private void initTasks() {
this.tasks.put("option1", new Task() {
public int execute(int a, int b) {
return (a + b) / 2;
}
});
//init more tasks
//this.task.put(..., ...);
}
protected Task getTask(String option) {
return this.task(option);
}
其中一个实现可能如下所示:
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
public class MeanTask implements Task {
public int execute(int a, int b) {
return (a + b) / 2;
}
}
private Task create(String option) {
if (option.toLowerCase().compareTo("mean") == 0) {
return new MeanTask();
}
else if ....
}
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
//call this method in your constructor
private void initTasks() {
this.tasks.put("option1", new Task() {
public int execute(int a, int b) {
return (a + b) / 2;
}
});
//init more tasks
//this.task.put(..., ...);
}
protected Task getTask(String option) {
return this.task(option);
}
您的工厂将如下所示:
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
public class MeanTask implements Task {
public int execute(int a, int b) {
return (a + b) / 2;
}
}
private Task create(String option) {
if (option.toLowerCase().compareTo("mean") == 0) {
return new MeanTask();
}
else if ....
}
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
//call this method in your constructor
private void initTasks() {
this.tasks.put("option1", new Task() {
public int execute(int a, int b) {
return (a + b) / 2;
}
});
//init more tasks
//this.task.put(..., ...);
}
protected Task getTask(String option) {
return this.task(option);
}
这样做的好处是每个任务的逻辑都很好地封装在自己的类中。这可以帮助您:
public class Main {
private Map<String, BaseStrategy> strategyMap = new HashMap<String, BaseStrategy>();
{
strategyMap.put("case1", new Strategy1());
strategyMap.put("case2", new Strategy2());
strategyMap.put("case3", new Strategy3());
}
//...
public void actionPerformed(ActionEvent e) {
strategyMap.get(option.toLowerCase()).processEvent();
}
abstract class BaseStrategy {
public abstract void processEvent();
}
class Strategy1 extends BaseStrategy {
@Override
public void processEvent() {
//case 1
}
}
class Strategy2 extends BaseStrategy {
@Override
public void processEvent() {
//case 2
}
}
class Strategy3 extends BaseStrategy {
@Override
public void processEvent() {
//case 3
}
}
}
公共类主{
private Map strategyMap=新建HashMap();
{
战略地图投入(“案例1”,新战略1());
战略地图投入(“案例2”,新战略2());
战略地图投入(“案例3”,新战略3());
}
//...
已执行的公共无效操作(操作事件e){
strategyMap.get(option.toLowerCase()).processEvent();
}
抽象类基类策略{
公共抽象void processEvent();
}
类策略1扩展了基本策略{
@凌驾
public void processEvent(){
//案例1
}
}
类策略2扩展了基本策略{
@凌驾
public void processEvent(){
//案例2
}
}
类策略3扩展了基本策略{
@凌驾
public void processEvent(){
//案例3
}
}
}
您可以制作一个映射,其中键是定义命令的字符串,值是处理事件的策略。现在,您甚至可以将处理代码放在其他java文件中,只需一行代码即可处理事件
事实上,如果你有太多的案例-
Map
是一种比if-else更好的方法,如果-…
Map会找到一种合适的策略,一种更快的方法-O(ln n)而不是O(n)。这可以帮助你:
public class Main {
private Map<String, BaseStrategy> strategyMap = new HashMap<String, BaseStrategy>();
{
strategyMap.put("case1", new Strategy1());
strategyMap.put("case2", new Strategy2());
strategyMap.put("case3", new Strategy3());
}
//...
public void actionPerformed(ActionEvent e) {
strategyMap.get(option.toLowerCase()).processEvent();
}
abstract class BaseStrategy {
public abstract void processEvent();
}
class Strategy1 extends BaseStrategy {
@Override
public void processEvent() {
//case 1
}
}
class Strategy2 extends BaseStrategy {
@Override
public void processEvent() {
//case 2
}
}
class Strategy3 extends BaseStrategy {
@Override
public void processEvent() {
//case 3
}
}
}
公共类主{
private Map strategyMap=新建HashMap();
{
战略地图投入(“案例1”,新战略1());
战略地图投入(“案例2”,新战略2());
战略地图投入(“案例3”,新战略3());
}
//...
已执行的公共无效操作(操作事件e){
strategyMap.get(option.toLowerCase()).processEvent();
}
抽象类基类策略{
公共抽象void processEvent();
}
类策略1扩展了基本策略{
@凌驾
public void processEvent(){
//案例1
}
}
类策略2扩展了基本策略{
@凌驾
public void processEvent(){
//案例2
}
}
类策略3扩展了基本策略{
@凌驾
public void processEvent(){
//案例3
}
}
}
您可以制作一个映射,其中键是定义命令的字符串,值是处理事件的策略。现在,您甚至可以将处理代码放在其他java文件中,只需一行代码即可处理事件
事实上,如果你有太多的案例-
Map
是一种比更好的方法,如果-…
Map会找到一种合适的策略,一种更快的方法-O(ln)而不是O(n)。正如维克托和尤金所发布的那样,我会将这两种解决方案结合起来:
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
public class MeanTask implements Task {
public int execute(int a, int b) {
return (a + b) / 2;
}
}
private Task create(String option) {
if (option.toLowerCase().compareTo("mean") == 0) {
return new MeanTask();
}
else if ....
}
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
//call this method in your constructor
private void initTasks() {
this.tasks.put("option1", new Task() {
public int execute(int a, int b) {
return (a + b) / 2;
}
});
//init more tasks
//this.task.put(..., ...);
}
protected Task getTask(String option) {
return this.task(option);
}
正如vektor和Eugene所发布的,我会将这两种解决方案结合起来:
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
public class MeanTask implements Task {
public int execute(int a, int b) {
return (a + b) / 2;
}
}
private Task create(String option) {
if (option.toLowerCase().compareTo("mean") == 0) {
return new MeanTask();
}
else if ....
}
public interface Task {
[whatever result it should return] execute([whatever parameters it needs]);
}
//call this method in your constructor
private void initTasks() {
this.tasks.put("option1", new Task() {
public int execute(int a, int b) {
return (a + b) / 2;
}
});
//init more tasks
//this.task.put(..., ...);
}
protected Task getTask(String option) {
return this.task(option);
}
我需要编写的任务是非常短的(3-4)行代码。我应该为每个任务创建新的“类”吗?这不是太多的类吗?@Robik如果你想缩短方法,你没有太多选择。此外,如果代码发生了变化,并且突然超过了3-4行:)@zip:Yes我想对GUI进行更改(更改显示在面板中的BuffereImage),这种分离将对您有所帮助。我从来没有用过SwingWorker。我真的没有得到SwingWorker.invokeLater()部分。。我应该在run()函数中写什么?它将如何更好地组织代码?@Robik:我胡说八道。如果执行长时间运行的任务(例如,完整的hdd扫描)需要几分钟,则只需使用SwingWorker。如果您的方法中的代码将非常快,则不需要考虑任何额外的内容。但在您的场景中并非如此。我误解了你的问题。对此我很抱歉。在这种情况下使用反射API真的很奇怪。你不应该让你的方法变得困难。我需要写的任务是非常短的(3-4)行代码。我应该为每个任务创建新的“类”吗?这不是太多的类吗?@Robik如果你想缩短方法,你没有太多选择。此外,如果代码发生了变化,并且突然超过了3-4行:)@zip:Yes我想对GUI进行更改(更改显示在面板中的BuffereImage),这种分离将对您有所帮助。我从来没有用过SwingWorker。我真的没有得到SwingWorker.invokeLater()部分。。什么