gpt4 book ai didi

java - 将 Actionlistener 移至单独的类

转载 作者:行者123 更新时间:2023-12-02 12:46:28 24 4
gpt4 key购买 nike

我目前正在为大学开发一个银行应用程序项目,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中的方法类,所以最简单的重构就是拥有每个 ActionListenerGUI 内的嵌套类中:

   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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com