将 Actionlisteners 移至单独的 class
Move Actionlisteners to a separate class
我目前正在为大学做一个银行应用程序项目,GUI(这里使用 swing)由许多不同的屏幕组成,每个屏幕有 2-8 个按钮,所以最后可以说是 50+具有不同功能的按钮。所有 GUI 的单个组件都外包给了一个不同的 class,我在主程序调用 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 构造函数为我的 ActionListeners 填充大约 600 行代码,所以我尝试将它们移动到不同的 class,将组件和 GUI 本身作为参数传递。但是现在启动程序时 none 按钮有效。
我一直在搜索一些网站,但没有真正找到似乎能帮助我解决这个问题的答案。
非常感谢您对此提供任何帮助。
如果需要任何代码示例或其他任何东西来解决问题,请随时告诉我,我只是不想用太多不必要的代码向这个 post 发送垃圾邮件。
您的 ActionListener
s 在 GUI
class 中的引用方法,因此您最简单的重构是将每个 ActionListener
嵌套在 class 中GUI
:
class DepositeAL implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
depositScreen();
}
}
并使用它:
components.btnDeposit.addActionListener(new DepositeAL());
要将动作侦听器重构为非嵌套 class,您需要传递对 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));
我目前正在为大学做一个银行应用程序项目,GUI(这里使用 swing)由许多不同的屏幕组成,每个屏幕有 2-8 个按钮,所以最后可以说是 50+具有不同功能的按钮。所有 GUI 的单个组件都外包给了一个不同的 class,我在主程序调用 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 构造函数为我的 ActionListeners 填充大约 600 行代码,所以我尝试将它们移动到不同的 class,将组件和 GUI 本身作为参数传递。但是现在启动程序时 none 按钮有效。 我一直在搜索一些网站,但没有真正找到似乎能帮助我解决这个问题的答案。
非常感谢您对此提供任何帮助。 如果需要任何代码示例或其他任何东西来解决问题,请随时告诉我,我只是不想用太多不必要的代码向这个 post 发送垃圾邮件。
您的 ActionListener
s 在 GUI
class 中的引用方法,因此您最简单的重构是将每个 ActionListener
嵌套在 class 中GUI
:
class DepositeAL implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
depositScreen();
}
}
并使用它:
components.btnDeposit.addActionListener(new DepositeAL());
要将动作侦听器重构为非嵌套 class,您需要传递对 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));