Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将Actionlisteners移动到单独的类_Java_Swing_Actionlistener - Fatal编程技术网

Java 将Actionlisteners移动到单独的类

Java 将Actionlisteners移动到单独的类,java,swing,actionlistener,Java,Swing,Actionlistener,我目前正在为大学做一个银行应用程序项目,GUI(在这里使用swing)由许多不同的屏幕组成,每个屏幕有2-8个按钮,所以最后它归结为50多个按钮,它们都有不同的功能。所有GUI的单个组件都外包给一个不同的类,我在主程序调用GUI时调用该类 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import

我目前正在为大学做一个银行应用程序项目,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();
                }
            }
        });
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.filechooser.FileNameExtensionFilter;
导入javax.swing.text.NumberFormatter;
导入java.sql.*;
导入java.text.DecimalFormat;
导入java.text.NumberFormat;
导入java.io.*;
公共类Gui扩展JFrame{
private JPanel contentPane=new JPanel();
私有静态数据库用户数据;
私有组件;
公共Gui(数据库用户数据){
components=新组件(这是userData);
ActionListeners al=新的ActionListeners(组件、此、用户数据);
对于(int i=0;i
到目前为止,一切都很好,应用程序实际上已经完全完成了,但我现在面临的问题是,我不希望我的GUI的构造函数为ActionListener填充大约600行代码,所以我尝试将它们移动到另一个类,将组件和GUI本身作为参数传递现在启动程序时,所有按钮都不起作用。 我一直在搜索一些网站,但还没有找到一个似乎对我有帮助的答案

在此方面的任何帮助都将不胜感激。
如果需要任何代码示例或其他任何东西来解决这个问题,请随时告诉我,我只是不想在这篇文章中添加太多不必要的代码。

您的
ActionListener
GUI
类中的
ActionListener
参考方法,因此最简单的重构就是将每个
ActionListener
都放在
GUI

   class DepositeAL implements ActionListener{

        public void actionPerformed(ActionEvent arg0) {
            depositScreen();        
        }
    }
    class DepositeAL implements ActionListener{

        private Gui gui;
        DepositeAL(Gui gui){
            this.gui = gui;
        }
        public void actionPerformed(ActionEvent arg0) {
            gui.depositScreen();        
        }
    }
并使用它:
components.btndeposite.addActionListener(新的DepositeAL());

要将操作侦听器重构为非嵌套类,需要传递对
GUI
的引用:

   class DepositeAL implements ActionListener{

        public void actionPerformed(ActionEvent arg0) {
            depositScreen();        
        }
    }
    class DepositeAL implements ActionListener{

        private Gui gui;
        DepositeAL(Gui gui){
            this.gui = gui;
        }
        public void actionPerformed(ActionEvent arg0) {
            gui.depositScreen();        
        }
    }

并使用它:
components.btndeposite.addActionListener(新的存储库(this))< /代码> 

不完全回答你的问题:你可以通过使用来减少你的<代码> AdActudiListAudio/Cuth>代码。欢迎这么做。不需要这么多代码来演示这个问题。请张贴。使用<代码> Actudio<代码>,以封装功能。还考虑<代码>动作< /代码>。