我目前正在为大学开设一个银行应用项目,而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的构造函数为我的ActionListeners填充大约600行代码,所以我尝试将它们移动到另一个类,将组件和GUI本身作为参数传递。但是在启动程序时,现在没有任何按钮可以工作。 我一直在搜索一些网站,但还没有真正找到一个似乎可以帮助我的答案。
对此非常感谢。 如果需要任何代码示例或其他任何东西来解决问题,请随时告诉我,我只是不想将这篇文章垃圾邮件发送给许多不必要的代码。
答案 0 :(得分:1)
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));