- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在为大学开发一个银行应用程序项目,GUI(此处使用 swing)由许多不同的屏幕组成,每个屏幕有 2-8 个按钮,所以最终它归结为 50 多个按钮,其中都有不同的功能。所有 GUI 的单个组件都外包给一个不同的类,当主程序调用 GUI 时我会调用该类。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.NumberFormatter;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.io.*;
public class Gui extends JFrame {
private JPanel contentPane = new JPanel();
private static Database userData;
private Components components;
public Gui(Database userData) {
components = new Components(this, userData);
ActionListeners al = new ActionListeners(components, this, userData);
for (int i = 0; i < components.accountModels.length; i++) {
initializeSettings(components.accountModels[i]);
}
components.checkingAccSettings = readSettings("Checking Account");
components.dayMoneyAccSettings = readSettings("Day Money Account");
components.depositAccSettings = readSettings("Deposit Account");
components.fixedDepositAccSettings = readSettings("Fixed Deposit Account");
components.robberyAccSettings = readSettings("Robbery Account");
components.loadLookAndFeel();
this.userData = userData;
setIconImage(Toolkit.getDefaultToolkit()
.getImage(Gui.class.getResource("/de/magani/banking/sparkasse_logo_transparent.png")));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
int screenWidth = (int) width;
int screenHeight = (int) height;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(screenWidth / 4, screenHeight / 4, screenWidth / 2, screenHeight / 2);
setResizable(true);
setMinimumSize(new Dimension(960, 608));
getContentPane().setLayout(null);
try {
File file = new File("C:/Program Files/Sparbank/adminCred.sparbank");
BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
BufferedReader in = new BufferedReader(new FileReader(file));
String currentLine = null;
if (file.exists() && ((currentLine = in.readLine()) != null)) {
components.adminPassword = currentLine;
in.close();
out.close();
} else {
file.createNewFile();
components.adminPassword = "123";
out.write(components.adminPassword);
in.close();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
// menuScreen
components.btnDisplayBalance.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
displayBalanceScreen();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
components.btnWithdraw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
withdrawScreen();
}
});
components.btnDeposit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
depositScreen();
}
});
components.btnTransfer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (components.currentAccount.equals("Robbery Account")) {
robberyScreen();
} else {
transferScreen();
}
}
});
到目前为止,一切工作正常,应用程序实际上已经完全完成,但我现在面临的问题是,我不希望 GUI 的构造函数充满了 ActionListener 的大约 600 行代码,所以我尝试将它们移动到不同的类,将组件和 GUI 本身作为参数传递。但现在启动程序时,所有按钮都不起作用。我一直在搜索一些网站,但还没有真正找到似乎对我有帮助的答案。
对此的任何帮助将非常非常感激。如果需要任何代码示例或其他任何内容来解决问题,请随时告诉我,我只是不想在这篇文章中添加太多不必要的代码。
最佳答案
您的ActionListener
引用GUI
中的方法类,所以最简单的重构就是拥有每个 ActionListener
在 GUI
内的嵌套类中:
class DepositeAL implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
depositScreen();
}
}
并使用它: components.btnDeposit.addActionListener(new DepositeAL());
要将操作监听器重构为非嵌套类,您需要传递对 GUI
的引用。 :
class DepositeAL implements ActionListener{
private Gui gui;
DepositeAL(Gui gui){
this.gui = gui;
}
public void actionPerformed(ActionEvent arg0) {
gui.depositScreen();
}
}
并使用它:components.btnDeposit.addActionListener(new DepositeAL(this));
关于java - 将 Actionlistener 移至单独的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44750233/
在另一个 ActionListener 中嵌入一个 ActionListener 更好,还是将它们作为单独的 ActionListener 更好?我有 JComboBox1、JComboBox2 和
我为我的程序制作了一个面板。它仅由 RadioButtons 组成。选择单选按钮后,我想在其他代码中设置一个 boolean 值。此面板将用作更大面板或框架的组件,该面板或框架也应该能够监听此面板内发
我想知道(如果可能的话)如何更新 JButton 的 ActionListener 执行的操作。我需要在整个程序中保留一个按钮,但根据当前情况更改它的功能。 最佳答案 首先使用removeAction
我的程序的先前版本旨在显示多个系列的魔术图像,所有这些图像都使用相同类型的 ActionListener。例如,在显示每幅图像之间设置时间的所有技巧时,我通过使用扩展 ActionListener 的
也许我做错了。让我知道使用 Swing 和 AWT,我在框架上设置了几个按钮,每个按钮都有一个与其特定功能 I.E 相对应的 ActionListener。 JButton foo_button =
我想把第一个ActionListener(About)改成第二个ActionListener(About2)如果不将第一个复制到第二个,有什么办法可以做到这一点吗? About.addActionLi
我想在单击按钮 b 后检查选中了哪些复选框,但是我的复选框是在我单击按钮 b1 之后声明的 我的意思是,我应该全局声明 checkbox[] 吗?我该怎么做? b1.addActionListener
我有一个这样的方法,它被赋予一个 JButton 数组,并在它们被按下时返回它们的文本: public static String foo(JButton[] buttons) { for (
这个问题在这里已经有了答案: Detect enter press in JTextField (10 个答案) 关闭 7 年前。 我有一个具有三个功能的程序;读取文件、写入文件以及在文件中搜索特定
我有一个 actionlistener,当我单击一个按钮时触发,让我们称之为框,我有另一个 actionlistener 用于另一个按钮调用它重新启动。我想要做的是,当我单击一个框按钮时,除了在该 a
我正在想办法如何让一个类(class)听另一个类(class)的课。这就是我们的想法。 我有一个 MainFrame 类,它只是一个容器类,即 JFrame 容器,它采用 JPanel 类型的参数。基
实现 java.awt.event.ActionListener 接口(interface)的最佳方法是什么? 让您的类实现 ActionListener 并将其添加为 ActionListener:
我有一个框架(此处名为“MainApplication”),它主要有一个 JPanel 来根据上下文显示信息。 启动时,MainApplication 有一个空的 JPanel。 然后它创建一个“Lo
我发现已经有人提出了一些关于这个主题的问题,但我还没有找到答案。我正在编写一个代码,其中用户在 JTextField 中键入内容,然后单击按钮后,他的单词将被替换为与他的单词具有相同数量的字符数的星号
我将稍微解释一下我的代码: 我有一个带有项目列表的 JComboBox当按下 JButton“Select”时,它会注册 JComboBox 中最后一个选定项目的最后一个索引。 现在我需要访问主目录中
我收到错误,“AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于此代码的参数 (new ActionListener(){})”:
哪种方式实现 ActionListener 更正确?性能上有什么重大差异吗? 在类中实现 ActionListener: public class MainFrame implements Actio
在 Java 中,我对 JButton 数组使用 ActionListener。我希望 ActionListener 的较早部分将新的 ImageIcon 设置为 JButton,该更改将立即显示,然
这里有新海报;如果我违反了任何规则/搞砸了任何事情,请告诉我,我会删除该帖子。 我有一个 MainMenu JFrame,里面有一个 JPanel (mainMenu)。 mainMenu 有一个按钮
我在一个主类中有两个 Action Listener 内部类。每一个都对应于它自己的按钮。其中一个 Action 监听器被编码为生成数组列表。另一个只是将该数组列表写入文本字段。 我的问题是如何从其他
我是一名优秀的程序员,十分优秀!