当您在 netbeans IDE 中创建 GUI 应用程序时,
它生成 2 个文件,App.java 和 View.java
可以将应用程序逻辑包含在 view.java 中吗?我的直觉告诉我这是一个坏主意。但是,我不知道如何调用这些方法或以某种方式“ Hook ”App.java 与 View.java
因此,我不必将 actionPerformed 的所有代码都放在按钮上,只需插入 App.java 中的方法即可
我对 Java 还很陌生。如果有人有关于 Netbeans 的优秀教程或书籍,我想我会购买。
最佳答案
另一个好方法是学习和使用操作类。请参阅How to Use Actions
这样的 Action 类封装了一个工作单元(“登录”,“打印”,...),您只需将其附加到一个或多个 gui 元素(JButton,JMenu,...)。如果您使用这个概念,您的应用程序可以更轻松地增长。将应用程序逻辑、GUI 和数据分开始终是一个好主意。
不完整的示例
public class ShowListAction extends AbstractAction {
JTextArea listArea;
YourListHandler listHandler;
public ShowListAction() {
this.putValue(Action.NAME,"Show List");
// this.putValue(Action.SMALL_ICON, yourIcon); // You can set various Properties for your Action...
this.setEnabled(enabled); // You can enable/disable the Action and hence any JButton connected to it ....
}
public void setListArea(JTextArea listArea) {
this.listArea = listArea;
}
public void setListHandler(YourListHandler listHandler) {
this.listHandler = listHandler;
}
public void actionPerformed(ActionEvent e) {
// Here comes the actual work
// list with data injected from another class which handles that
List<String> list = listHandler.getNamesList();
// output - correct
for (String s : list) {
listArea.append(s);
}
}
}
要使用它,您需要在 View 中创建/获取操作的实例并将其附加到例如一个带有
的 JButtonyourButton.setAction(theAction)
关于java - netbeans:链接应用程序和 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090837/