日撸java三百行day77-80

文章目录

  • 说明
  • GUI
    • 1. GUI 总体布局
    • 2. GUI 代码理解
      • 2.1 对话框相关控件
        • 2.1.1 ApplicationShowdown.java(关闭应用程序)
        • 2.1.2 DialogCloser.java(关闭对话框)
        • 2.1.3 ErrorDialog.java(显示错误信息)
        • 2.1.4 HelpDialog.java(显示帮助信息)
        • 2.1.5 GUICommon.java (通用的 GUI 配置信息和变量)
      • 2.2 数据读取控件
        • 2.2.1 DoubleField.java(输入功能的文本字段且只能输入double类型)
        • 2.2.2 IntegerField.java(输入功能的文本字段且只能输入int类型)
        • 2.2.2 FilenameField.java(输入功能的文本字段且用于输入文件名或文件路径)
      • 2.3 整体布局GUI
      • 2.4 GUI之接口与监听机制
      • 2.5 总结

说明

闵老师的文章链接: 日撸 Java 三百行(总述)_minfanphd的博客-CSDN博客
自己也把手敲的代码放在了github上维护:https://github.com/fulisha-ok/sampledata

GUI

1. GUI 总体布局

我是copy代码直接运行了GUI的一个总体代码,最后运行的界面如下,这个界面实现了灵活输入参数(神经层数,激活函数,训练次数等。点击OK即可完成一次)再执行神经网络的训练和测试(其中训练的方式以及前向传播函数和后向传播函数都是调用的通用神经网络的方法)
在这里插入图片描述
在这里插入图片描述

2. GUI 代码理解

在看了一个整体的思路,在来详细看代码类。

2.1 对话框相关控件

2.1.1 ApplicationShowdown.java(关闭应用程序)

package machinelearing.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * @author: fulisha
 * @date: 2023/7/17 15:39
 * @description:
 */
public class ApplicationShutdown implements WindowListener, ActionListener {
    /**
     * Only one instance.
     */
    public static ApplicationShutdown applicationShutdown = new ApplicationShutdown();

    private ApplicationShutdown() {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}

  1. 实现了WindowListener和ActionListener接口(java中,继承只能单继承,但能多实现)主要处理程序的关闭事件,并提供了在窗口关闭时退出应用程序的功能
  2. 从代码中可以知道 虽然重写了很多方法,但实际上主要是actionPerformed和windowClosing方法,实现了System.exit(0);来终止应用程序,使其退出
  3. 这个类使用了单例模式(23种设计模式中的一种)
    它确保一个类只有一个实例,并提供全局访问点以访问该实例。所以ApplicationShutdown 这个类就是一个单例模式的实现。常见的实现方式
  • 懒汉式,线程不安全
public class Singleton {  
    private static Singleton instance;  
    private Singleton (){}  
  
    public static Singleton getInstance() {  
        if (instance == null) {  
            instance = new Singleton();  
        }  
        return instance;  
    }  
}
  • 懒汉式,线程安全
public class Singleton {  
    private static Singleton instance;  
    private Singleton (){}  
    public static synchronized Singleton getInstance() {  
        if (instance == null) {  
            instance = new Singleton();  
        }  
        return instance;  
    }  
}
  • 饿汉式
public class Singleton {  
    private static Singleton instance = new Singleton();  
    private Singleton (){}  
    public static Singleton getInstance() {  
    return instance;  
    }  
}

2.1.2 DialogCloser.java(关闭对话框)

package machinelearing.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author: fulisha
 * @date: 2023/7/17 15:41
 * @description:
 */
public class DialogCloser extends WindowAdapter implements ActionListener {
    /**
     * The dialog under control.
     */
    private Dialog currentDialog;

    /**
     * The first constructor.
     */
    public DialogCloser() {
        super();
    }

    /**
     * The second constructor.
     *
     * @param paraDialog the dialog under control
     */
    public DialogCloser(Dialog paraDialog) {
        currentDialog = paraDialog;
    }

    /**
     * Close the dialog which clicking the cross at the up-right corner of the window.
     * @param paraWindowEvent  From it we can obtain which window sent the message because X was used.
     */
    @Override
    public void windowClosing(WindowEvent paraWindowEvent) {
        paraWindowEvent.getWindow().dispose();
    }

    /**
     * Close the dialog while pushing an "OK" or "Cancel" button.
     * @param paraEvent Not considered.
     */
    @Override
    public void actionPerformed(ActionEvent paraEvent) {
        currentDialog.dispose();
    }
}

  1. 实现了 WindowAdapter 和 ActionListener 接口,用于监听对话框的关闭事件
  2. 重写windowClosing方法,当用户点击对话框的关闭按钮触发WindowEvent事件(通常是对话框的上右角的“X”按钮)时,会触发该方法。在此方法中,通paraWindowEvent.getWindow().dispose(); 来关闭对话框; 重写actionPerformed方法,监听ActionEvent事件触发currentDialog.dispose()来关闭对话框

2.1.3 ErrorDialog.java(显示错误信息)

package machinelearing.gui;

import java.awt.*;

/**
 * @author: fulisha
 * @date: 2023/7/17 16:04
 * @description:
 */
public class ErrorDialog  extends Dialog{
    /**
     * Serial uid. Not quite useful.
     */
    private static final long serialVersionUID = 124535235L;

    /**
     * The ONLY ErrorDialog.
     */
    public static ErrorDialog errorDialog = new ErrorDialog();

    /**
     * The label containing the message to display.
     */
    private TextArea messageTextArea;

    /**
     ***************************
     * Display an error dialog and respective error message. Like other dialogs,
     * this constructor is private, such that users can use only one dialog,
     * i.e., ErrorDialog.errorDialog to display message. This is helpful for
     * saving space (only one dialog) since we may need "many" dialogs.
     ***************************
     */
    private ErrorDialog(){
        // This dialog is module.
        super(GUICommon.mainFrame, "Error", true);

        // Prepare for the dialog.
        messageTextArea = new TextArea();

        Button okButton = new Button("OK");
        okButton.setSize(20, 10);
        okButton.addActionListener(new DialogCloser(this));
        Panel okPanel = new Panel();
        okPanel.setLayout(new FlowLayout());
        okPanel.add(okButton);

        // Add TextArea and Button
        setLayout(new BorderLayout());
        add(BorderLayout.CENTER, messageTextArea);
        add(BorderLayout.SOUTH, okPanel);

        setLocation(200, 200);
        setSize(500, 200);
        addWindowListener(new DialogCloser());
        setVisible(false);
    }

    /**
     * set message.
     * @param paramMessage the new message
     */
    public void setMessageAndShow(String paramMessage) {
        messageTextArea.setText(paramMessage);
        setVisible(true);
    }
}

  1. 这个类也采用了单例模式,也是只有一个实例
  2. 构造函数创建了一个对话框,用于显示错误信息和"OK"按钮

2.1.4 HelpDialog.java(显示帮助信息)

package machinelearing.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author: fulisha
 * @date: 2023/7/17 15:44
 * @description:
 */
public class HelpDialog extends Dialog implements ActionListener {
    /**
     * Serial uid. Not quite useful.
     */
    private static final long serialVersionUID = 3869415040299264995L;

    /**
     * Display the help dialog.
     *
     * @param paraTitle the title of the dialog.
     * @param paraFilename   the help file.
     */
    public HelpDialog(String paraTitle, String paraFilename) {
        super(GUICommon.mainFrame, paraTitle, true);
        setBackground(GUICommon.MY_COLOR);

        TextArea displayArea = new TextArea("", 10, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
        displayArea.setEditable(false);
        String textToDisplay = "";
        try {
            RandomAccessFile helpFile = new RandomAccessFile(paraFilename, "r");
            String tempLine = helpFile.readLine();
            while (tempLine != null) {
                textToDisplay = textToDisplay + tempLine + "\n";
                tempLine = helpFile.readLine();
            }
            helpFile.close();
        } catch (IOException ee) {
            dispose();
            ErrorDialog.errorDialog.setMessageAndShow(ee.toString());
        }
        // Use this if you need to display Chinese. Consult the author for this
        // method.
        // textToDisplay = SimpleTools.GB2312ToUNICODE(textToDisplay);
        displayArea.setText(textToDisplay);
        displayArea.setFont(new Font("Times New Romans", Font.PLAIN, 14));

        Button okButton = new Button("OK");
        okButton.setSize(20, 10);
        okButton.addActionListener(new DialogCloser(this));
        Panel okPanel = new Panel();
        okPanel.setLayout(new FlowLayout());
        okPanel.add(okButton);

        // OK Button
        setLayout(new BorderLayout());
        add(BorderLayout.CENTER, displayArea);
        add(BorderLayout.SOUTH, okPanel);

        setLocation(120, 70);
        setSize(500, 400);
        addWindowListener(new DialogCloser());
        setVisible(false);
    }

    /**
     * Simply set it visible.
     */
    @Override
    public void actionPerformed(ActionEvent ee) {
        setVisible(true);
    }
}

2.1.5 GUICommon.java (通用的 GUI 配置信息和变量)

package machinelearing.gui;

import javax.swing.*;
import java.awt.*;

/**
 * @author: fulisha
 * @date: 2023/7/17 15:43
 * @description:
 */
public class GUICommon {
    /**
     * Only one main frame.
     */
    public static Frame mainFrame = null;

    /**
     * Only one main pane.
     */
    public static JTabbedPane mainPane = null;

    /**
     * For default project number.
     */
    public static int currentProjectNumber = 0;

    /**
     * Default font.
     */
    public static final Font MY_FONT = new Font("Times New Romans", Font.PLAIN, 12);

    /**
     * Default color
     */
    public static final Color MY_COLOR = Color.lightGray;

    /**
     * Set the main frame. This can be done only once at the initialzing stage.
     * @param paraFrame the main frame of the GUI.
     * @throws Exception If the main frame is set more than once.
     */
    public static void setFrame(Frame paraFrame) throws Exception {
        if (mainFrame == null) {
            mainFrame = paraFrame;
        } else {
            throw new Exception("The main frame can be set only ONCE!");
        }
    }

    /**
     * Set the main pane. This can be done only once at the initialzing stage.
     * @param paramPane the main pane of the GUI.
     * @throws Exception  If the main panel is set more than once.
     */
    public static void setPane(JTabbedPane paramPane) throws Exception {
        if (mainPane == null) {
            mainPane = paramPane;
        } else {
            throw new Exception("The main panel can be set only ONCE!");
        }
    }
}

2.2 数据读取控件

2.2.1 DoubleField.java(输入功能的文本字段且只能输入double类型)

package machinelearing.gui;

import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

/**
 * @author: fulisha
 * @date: 2023/7/17 16:10
 * @description:
 */
public class DoubleField extends TextField implements FocusListener {

    /**
     * Serial uid. Not quite useful.
     */
    private static final long serialVersionUID = 363634723L;

    /**
     * The value
     */
    protected double doubleValue;

    /**
     * Give it default values.
     */
    public DoubleField() {
        this("5.13", 10);
    }
    /**
     * Only specify the content.
     * @param paraString The content of the field.
     */
    public DoubleField(String paraString) {
        this(paraString, 10);
    }

    /**
     * Only specify the width.
     * @param paraWidth  The width of the field.
     */
    public DoubleField(int paraWidth) {
        this("5.13", paraWidth);
    }

    /**
     * Specify the content and the width.
     * @param paraString The content of the field.
     * @param paraWidth The width of the field.
     */
    public DoubleField(String paraString, int paraWidth) {
        super(paraString, paraWidth);
        addFocusListener(this);
    }

    /**
     * Implement FocusListener.
     * @param paraEvent  The event is unimportant.
     */
    @Override
    public void focusGained(FocusEvent paraEvent) {
    }

    /**
     * Implement FocusListener.
     * @param paraEvent The event is unimportant.
     */
    @Override
    public void focusLost(FocusEvent paraEvent) {
        try {
            doubleValue = Double.parseDouble(getText());
        } catch (Exception ee) {
            ErrorDialog.errorDialog
                    .setMessageAndShow("\"" + getText() + "\" Not a double. Please check.");
            requestFocus();
        }
    }

    /**
     * Get the double value.
     * @return the double value.
     */
    public double getValue() {
        try {
            doubleValue = Double.parseDouble(getText());
        } catch (Exception ee) {
            ErrorDialog.errorDialog
                    .setMessageAndShow("\"" + getText() + "\" Not a double. Please check.");
            requestFocus();
        }
        return doubleValue;
    }
}

  1. 重写focusLost方法(当文本字段失去焦点时触发):将输入的文本转换为 double 类型的值,并将其存储在 doubleValue 变量中,如果失败调用ErrorDialog显示错误信息
  2. 自定义方法getValue,用于获取文本字段中输入的浮点数值

2.2.2 IntegerField.java(输入功能的文本字段且只能输入int类型)

和DoubleField同理

package machinelearing.gui;

import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

/**
 * @author: fulisha
 * @date: 2023/7/17 16:12
 * @description:
 */
public class IntegerField extends TextField implements FocusListener {
    /**
     * Serial uid. Not quite useful.
     */
    private static final long serialVersionUID = -2462338973265150779L;

    /**
     * Only specify the content.
     */
    public IntegerField() {
        this("513");
    }

    /**
     * Specify the content and the width.
     * @param paraString  The default value of the content.
     * @param paraWidth The width of the field.
     */
    public IntegerField(String paraString, int paraWidth) {
        super(paraString, paraWidth);
        addFocusListener(this);
    }

    /**
     * Only specify the content.
     * @param paraString The given default string.
     */
    public IntegerField(String paraString) {
        super(paraString);
        addFocusListener(this);
    }

    /**
     * Only specify the width.
     * @param paraWidth  The width of the field.
     */
    public IntegerField(int paraWidth) {
        super(paraWidth);
        setText("513");
        addFocusListener(this);
    }

    /**
     * Implement FocusListenter.
     * @param paraEvent The event is unimportant.

     */
    @Override
    public void focusGained(FocusEvent paraEvent) {
    }

    /**
     * Implement FocusListenter.
     * @param paraEvent   The event is unimportant.
     */
    @Override
    public void focusLost(FocusEvent paraEvent) {
        try {
            Integer.parseInt(getText());
            // System.out.println(tempInt);
        } catch (Exception ee) {
            ErrorDialog.errorDialog.setMessageAndShow("\"" + getText()
                    + "\"Not an integer. Please check.");
            requestFocus();
        }
    }

    /**
     * Get the int value. Show error message if the content is not an int.
     * @return the int value.
     */
    public int getValue() {
        int tempInt = 0;
        try {
            tempInt = Integer.parseInt(getText());
        } catch (Exception ee) {
            ErrorDialog.errorDialog.setMessageAndShow("\"" + getText()
                    + "\" Not an int. Please check.");
            requestFocus();
        }
        return tempInt;
    }
}

2.2.2 FilenameField.java(输入功能的文本字段且用于输入文件名或文件路径)

package machinelearing.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;

/**
 * @author: fulisha
 * @date: 2023/7/17 16:14
 * @description:一个带有文件选择功能的文本输入框,用于方便用户选择文件路径,并将选择的文件路径显示在文本输入框中。它还具有一些对文本内容的处理,如检查文件是否存在并显示错误消息等。
 */
public class FilenameField extends TextField implements ActionListener,
        FocusListener{
    /**
     * Serial uid. Not quite useful.
     */
    private static final long serialVersionUID = 4572287941606065298L;

    /**
     * No special initialization..
     */
    public FilenameField() {
        super();
        setText("");
        addFocusListener((FocusListener) this);
    }

    /**
     * No special initialization.
     * @param paraWidth The width of the .
     */
    public FilenameField(int paraWidth) {
        super(paraWidth);
        setText("");
        addFocusListener(this);
    }// Of constructor

    /**
     * No special initialization.
     * @param paraWidth The width of the .
     * @param paraText  The given initial text
     */
    public FilenameField(int paraWidth, String paraText) {
        super(paraWidth);
        setText(paraText);
        addFocusListener(this);
    }

    /**
     * No special initialization.
     * @param paraWidth The width of the .
     * @param paraText The given initial text
     */
    public FilenameField(String paraText, int paraWidth) {
        super(paraWidth);
        setText(paraText);
        addFocusListener(this);
    }

    /**
     * Avoid setting null or empty string.
     * @param paraText The given text.
     */
    @Override
    public void setText(String paraText) {
        if (paraText.trim().equals("")) {
            super.setText("unspecified");
        } else {
            super.setText(paraText.replace('\\', '/'));
        }
    }

    /**
     * Implement ActionListenter.
     * @param paraEvent The event is unimportant.
     */
    @Override
    public void actionPerformed(ActionEvent paraEvent) {
        FileDialog tempDialog = new FileDialog(GUICommon.mainFrame,
                "Select a file");
        tempDialog.setVisible(true);
        if (tempDialog.getDirectory() == null) {
            setText("");
            return;
        }

        String directoryName = tempDialog.getDirectory();

        String tempFilename = directoryName + tempDialog.getFile();
        //System.out.println("tempFilename = " + tempFilename);

        setText(tempFilename);
    }

    /**
     * Implement FocusListenter.
     * @param paraEvent  The event is unimportant.
     */
    @Override
    public void focusGained(FocusEvent paraEvent) {
    }

    /**
     * Implement FocusListenter.
     * @param paraEvent The event is unimportant.
     */
    @Override
    public void focusLost(FocusEvent paraEvent) {
        // System.out.println("Focus lost exists.");
        String tempString = getText();
        if ((tempString.equals("unspecified"))
                || (tempString.equals("")))
            return;
        File tempFile = new File(tempString);
        if (!tempFile.exists()) {
            ErrorDialog.errorDialog.setMessageAndShow("File \"" + tempString
                    + "\" not exists. Please check.");
            requestFocus();
            setText("");
        }
    }
}

  1. 实现actionPerformed方法,该方法打开文件选择对话框,让用户选择文件,并将选中的文件名或文件路径设置为文本字段的内容
  2. 实现focusLost方法(当文本字段失去焦点时触发),检查文本字段的内容是否为空或为 "unspecified"如果是则返回反之它会检查检查文件是否存在,若不存在,则调用ErrorDialog来显示错误信息。

2.3 整体布局GUI

package machinelearing.gui;

import machinelearing.ann.FullAnn;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * @author: fulisha
 * @date: 2023/7/18 16:02
 * @description:
 */
public class AnnMain implements ActionListener {
    /**
     * Select the arff file.
     */
    private FilenameField arffFilenameField;

    /**
     * The setting of alpha.
     */
    private DoubleField alphaField;

    /**
     * The setting of alpha.
     */
    private DoubleField betaField;

    /**
     * The setting of alpha.
     */
    private DoubleField gammaField;

    /**
     * Layer nodes, such as "4, 8, 8, 3".
     */
    private TextField layerNodesField;

    /**
     * Activators, such as "ssa".
     */
    private TextField activatorField;

    /**
     * The number of training rounds.
     */
    private IntegerField roundsField;

    /**
     * The learning rate.
     */
    private DoubleField learningRateField;

    /**
     * The mobp.
     */
    private DoubleField mobpField;

    /**
     * The message area.
     */
    private TextArea messageTextArea;

    /**
     * The only constructor.
     */
    public AnnMain() {
        // A simple frame to contain dialogs.
        Frame mainFrame = new Frame();
        mainFrame.setTitle("ANN");
        // The top part: select arff file.
        arffFilenameField = new FilenameField(30);
        arffFilenameField.setText("D:/sampledata/sampledata/src/data/iris.arff");
        Button browseButton = new Button(" Browse ");
        browseButton.addActionListener(arffFilenameField);

        Panel sourceFilePanel = new Panel();
        sourceFilePanel.add(new Label("The .arff file:"));
        sourceFilePanel.add(arffFilenameField);
        sourceFilePanel.add(browseButton);

        // Setting panel.
        Panel settingPanel = new Panel();
        settingPanel.setLayout(new GridLayout(3, 6));

        settingPanel.add(new Label("alpha"));
        alphaField = new DoubleField("0.01");
        settingPanel.add(alphaField);

        settingPanel.add(new Label("beta"));
        betaField = new DoubleField("0.02");
        settingPanel.add(betaField);

        settingPanel.add(new Label("gamma"));
        gammaField = new DoubleField("0.03");
        settingPanel.add(gammaField);

        settingPanel.add(new Label("layer nodes"));
        layerNodesField = new TextField("4, 8, 8, 3");
        settingPanel.add(layerNodesField);

        settingPanel.add(new Label("activators"));
        activatorField = new TextField("sss");
        settingPanel.add(activatorField);

        settingPanel.add(new Label("training rounds"));
        roundsField = new IntegerField("5000");
        settingPanel.add(roundsField);

        settingPanel.add(new Label("learning rate"));
        learningRateField = new DoubleField("0.01");
        settingPanel.add(learningRateField);

        settingPanel.add(new Label("mobp"));
        mobpField = new DoubleField("0.5");
        settingPanel.add(mobpField);

        Panel topPanel = new Panel();
        topPanel.setLayout(new BorderLayout());
        topPanel.add(BorderLayout.NORTH, sourceFilePanel);
        topPanel.add(BorderLayout.CENTER, settingPanel);

        messageTextArea = new TextArea(80, 40);

        // The bottom part: ok and exit
        Button okButton = new Button(" OK ");
        okButton.addActionListener(this);
        // DialogCloser dialogCloser = new DialogCloser(this);
        Button exitButton = new Button(" Exit ");
        // cancelButton.addActionListener(dialogCloser);
        exitButton.addActionListener(ApplicationShutdown.applicationShutdown);
        Button helpButton = new Button(" Help ");
        helpButton.setSize(20, 10);
        helpButton.addActionListener(new HelpDialog("ANN", "D:/sampledata/sampledata/src/data/help.txt"));
        Panel okPanel = new Panel();
        okPanel.add(okButton);
        okPanel.add(exitButton);
        okPanel.add(helpButton);

        mainFrame.setLayout(new BorderLayout());
        mainFrame.add(BorderLayout.NORTH, topPanel);
        mainFrame.add(BorderLayout.CENTER, messageTextArea);
        mainFrame.add(BorderLayout.SOUTH, okPanel);

        mainFrame.setSize(600, 500);
        mainFrame.setLocation(100, 100);
        mainFrame.addWindowListener(ApplicationShutdown.applicationShutdown);
        mainFrame.setBackground(GUICommon.MY_COLOR);
        mainFrame.setVisible(true);
    }

    /**
     * Read the arff file.
     */
    @Override
    public void actionPerformed(ActionEvent ae) {
        String tempFilename = arffFilenameField.getText();

        // Read the layers nodes.
        String tempString = layerNodesField.getText().trim();

        int[] tempLayerNodes = null;
        try {
            tempLayerNodes = stringToIntArray(tempString);
        } catch (Exception ee) {
            ErrorDialog.errorDialog.setMessageAndShow(ee.toString());
            return;
        }

        double tempLearningRate = learningRateField.getValue();
        double tempMobp = mobpField.getValue();
        String tempActivators = activatorField.getText().trim();
        FullAnn tempNetwork = new FullAnn(tempFilename, tempLayerNodes, tempLearningRate, tempMobp,
                tempActivators);
        int tempRounds = roundsField.getValue();

        long tempStartTime = new Date().getTime();
        for (int i = 0; i < tempRounds; i++) {
            tempNetwork.train();
        }
        long tempEndTime = new Date().getTime();
        messageTextArea.append("\r\nSummary:\r\n");
        messageTextArea.append("Trainng time: " + (tempEndTime - tempStartTime) + "ms.\r\n");

        double tempAccuray = tempNetwork.test();
        messageTextArea.append("Accuracy: " + tempAccuray + "\r\n");
        messageTextArea.append("End.");
    }

    /**
     * Convert a string with commas into an int array.
     * @param paraString The source string
     * @return An int array.
     * @throws Exception Exception for illegal data.
     */
    public static int[] stringToIntArray(String paraString) throws Exception {
        int tempCounter = 1;
        for (int i = 0; i < paraString.length(); i++) {
            if (paraString.charAt(i) == ',') {
                tempCounter++;
            }
        }

        int[] resultArray = new int[tempCounter];

        String tempRemainingString = new String(paraString) + ",";
        String tempString;
        for (int i = 0; i < tempCounter; i++) {
            tempString = tempRemainingString.substring(0, tempRemainingString.indexOf(",")).trim();
            if (tempString.equals("")) {
                throw new Exception("Blank is unsupported");
            }

            resultArray[i] = Integer.parseInt(tempString);

            tempRemainingString = tempRemainingString
                    .substring(tempRemainingString.indexOf(",") + 1);
        }

        return resultArray;
    }

    /**
     * The entrance method.
     * @param args The parameters.
     */
    public static void main(String args[]) {
        new AnnMain();
    }
}

AnnMain构造方法,是程序的入口方法,他初始化 GUI 界面并添加监听事件。所以从这个构造方法去了解这些代码

  1. mainFrame 来包含所有的对话框
  2. FilenameField实例化对象:arffFilenameField用于选择输入文件,将 arffFilenameField 对象注册为按钮 browseButton 的事件监听器
  3. 创建 Panel 面板 sourceFilePanel,主要用来放文件选择和按钮 在这里插入图片描述
  4. 创建 Panel 面板 settingPanel,并且采用网格布局(代码中是3行6列),并将DoubleField、TextField,IntegerField对象排列在一起,这些都是神经网络的参数 在这里插入图片描述
  5. 创建一个 TextArea 对象 messageTextArea,用于显示训练过程和结果的信息
  6. 创建 “OK” 按钮 okButton,并将 this 对象注册为它的事件监听器,这里的this就是当前对象ANN实例化的整个对象;创建 Exit按钮 exitButton并把ApplicationShutdown对象注册上去,创建 Help 按钮 helpButton,并将HelpDialog对象注册到这个按钮上。并创建 Panel 面板 okPanel,把这三个按钮都放上去 在这里插入图片描述
  7. 利用mainFrame 把所有的面板组件都放上去就成了如下: 在这里插入图片描述

2.4 GUI之接口与监听机制

下面这5个类的实现其实体现了设计模式中的策略模式,具体知识可跳转到此文章

  • Flying接口类
package machinelearing.gui.test;

/**
 * @author: fulisha
 * @date: 2023/7/23 17:12
 * @description:
 */
    public interface Flying {
    public void fly();
}

接口类,定义了一个方法fly(),无实现。但所有实现了这个接口类的类都要实现这个fly方法。

  • Controller类
package machinelearing.gui.test;

/**
 * @author: fulisha
 * @date: 2023/7/23 17:14
 * @description:
 */
public class Controller {
    Flying flying;

    Controller(){
        flying = null;
    }

    void setListener(Flying paraFlying){
        flying = paraFlying;
    }

    void doIt(){
        flying.fly();
    }
}

Controller类是一个控制器类,它用来执行飞行对象的飞行操作. 它包含一个Flying类型的成员变量flying,可以通过setListener()方法设置飞行对象,并通过doIt()方法执行飞行。

  • Bird类
package machinelearing.gui.test;

/**
 * @author: fulisha
 * @date: 2023/7/23 17:15
 * @description:
 */
public class Bird implements Flying {
    double weight = 0.5;

    @Override
    public void fly(){
        System.out.println("Bird fly, my weight is " + weight + " kg.");
    }
}

Bird类实现了Flying接口,它必须提供fly()方法的具体实现。

  • Plane
package machinelearing.gui.test;

import machinelearing.gui.test.Flying;

/**
 * @author: fulisha
 * @date: 2023/7/23 17:15
 * @description:
 */
public class Plane implements Flying {
    double price = 100000000;


    @Override
    public void fly() {
        System.out.println("Plan fly, my price is " + price + " RMB.");
    }
}

Plane类也实现了Flying接口,同样需要提供fly()方法的具体实现

  • InterfaceTest类
package machinelearing.gui.test;

/**
 * @author: fulisha
 * @date: 2023/7/23 17:17
 * @description:
 */
public class InterfaceTest {
    public static void main(String[] args){
        Controller tempController = new Controller();
        Flying tempFlying1 = new Bird();
        tempController.setListener(tempFlying1);
        tempController.doIt();

        Flying tempFlying2 = new Plane();
        tempController.setListener(tempFlying2);
        tempController.doIt();
    }
}

通过使用Flying接口作为控制器Controller的成员变量,可以将不同的飞行对象(Bird和Plane)传递给Controller,使得Controller可以控制这些飞行对象的行为,这样的设计模式可以很容易地添加新的飞行对象,而不需要去修改Controller类的代码,这使得代码更灵活性,扩展性更强.

2.5 总结

今天学习的GUI 是 创建了一个包含输入和输出界面的 GUI 窗口,并为界面上的按钮注册了相应的事件监听器,当用户输入参数后,点击 OK 按钮可以神经网络训练和测试(这里的方法都是FullAnn里面的),点击 Exit 按钮可以退出程序,点击 Help 按钮可以查看帮助信息。今天的代码主要是学习的GUI布局,而关于神经网络学习是之前已经学习的知识(FullAnn类),可再巩固。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/42210.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

websoket

websoket是html5新特性&#xff0c; 它提供一种基于TCP连接上进行全双工通讯的协议; 全双工通信的意思就是:允许客户端给服务器主动发送信息,也支持服务端给另一个客户端发送信息. Websoket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在we…

c++内存映射文件

概念 将一个文件直接映射到进程的进程空间中&#xff08;“映射”就是建立一种对应关系,这里指硬盘上文件的位置与进程逻辑地址空间中一块相同区域之间一 一对应,这种关系纯属是逻辑上的概念&#xff0c;物理上是不存在的&#xff09;&#xff0c;这样可以通过内存指针用读写内…

【vue】路由的搭建以及嵌套路由

目的&#xff1a;学习搭建vue2项目基础的vue路由和嵌套路由 1.npm 安装 router npm install vue-router3.6.52.src下新建文件夹router文件夹以及文件index.js index.js import Vue from vue import VueRouter from "vue-router" import Home from ../views/Home.…

spring boot 多模块项目非启动模块的bean无法注入(问题记录)

之前有说我搭了一个多模块项目&#xff0c;往微服务升级&#xff0c;注入的依赖在zuodou-bean模块中&#xff0c;入jwt拦截&#xff0c; Knife4j ,分页插件等等&#xff0c;但是启动类在system中&#xff0c;看网上说在启动类上加SpringBootApplication注解默认扫描范围为自己…

《爆肝整理》保姆级系列教程-玩转Charles抓包神器教程(4)-Charles如何设置捕获会话

1.简介 前边几篇宏哥介绍了Charles界面内容以及作用。今天宏哥就讲解和分享如何设置Charles后&#xff0c;我们就可以愉快地捕获会话&#xff0c;进行抓包了。因为上一篇许多小伙伴看到宏哥的Charles可以分开看到request和response&#xff0c;而自己的却看不到&#xff0c;因…

【wifi模块选型指导】数据传输WiFi模块的选型参考_USB/UART接口WiFi模块

数据传输WiFi模块有USB接口和UART接口两大类&#xff0c;为满足行业客户的不同应用需求&#xff0c;SKYLAB研发推出了多款2.4GHz单频&#xff0c;2.4/5GHz双频的USB接口WiFi模块和UART接口WiFi模块&#xff0c;数据传输能力&#xff0c;传输距离各有不同。怎么选才是最适合的呢…

MySql如何卸载干净经验分享

第一步&#xff1a;首先打开注册表&#xff1a;点击电脑的开始按钮&#xff0c;打开找到运行&#xff0c;输入regedit&#xff0c;进入注册表&#xff1b; 第二步&#xff1a;删除mysql再注册表中的信息&#xff0c;以下三个目录&#xff1a; 1.HKEY_LOCAL_MACHINE\SYSTEM\Cont…

论文阅读—2023.7.13:遥感图像语义分割空间全局上下文信息网络(主要为unet网络以及改unet)附加个人理解与代码解析

前期看的文章大部分都是深度学习原理含量多一点&#xff0c;一直在纠结怎么改模型&#xff0c;论文看的很吃力&#xff0c;看一篇忘一篇&#xff0c;总感觉摸不到方向。想到自己是遥感专业&#xff0c;所以还是回归遥感影像去谈深度学习&#xff0c;回归问题&#xff0c;再想着…

CMS垃圾收集器三色标记-JVM(十二)

上篇文章说了CMS垃圾收集器是赋值清除&#xff0c;所以他不可以碎片整理&#xff0c;于是jvm支持两个参数&#xff0c;几次fullGC之后碎片整理压缩空间。Cms他会抢占cpu资源&#xff0c;因为是并行运行&#xff0c;所以会有浮动垃圾。还有执行不确定性&#xff0c;垃圾收集完&a…

企业需要一个数字体验平台(DXP)吗?

数字体验平台是一个软件框架&#xff0c;通过与不同的业务系统喝解决方案集成&#xff0c;帮助企业和机构建立、管理和优化跨渠道的数字体验。帮助企业实现跨网站、电子邮件、移动应用、社交平台、电子商务站点、物联网设备、数字标牌、POS系统等传播内容&#xff0c;除了为其中…

【ArcGIS Pro二次开发】(48):三调土地利用现状分类面积汇总统计

之前做了一个三调三大类面积统计&#xff0c;有小伙伴反映太粗糙&#xff0c;想要一个完整的地类面积汇总表。 【ArcGIS Pro二次开发】(35)&#xff1a;三调三大类面积统计 本质上并没有多少难度&#xff0c;之前也做过类似的用地用海汇总表&#xff0c;于是拿出来改一改就好了…

【已解决】天翼电信宽带改桥模式,使用路由器ppoe拨号

运营商在给办理宽带时会默认给宽带设置成光猫ppoe拨号&#xff0c;路由器只需设置为dhcp获取ip&#xff0c;插入到光猫的lan口即可上网。但运营商的光猫路由性能有限&#xff0c;会影响到网络体验。而将光猫设置为桥模式&#xff0c;使用路由器拨号&#xff0c;可以实现路由器进…

【C语言】深剖数据在内存中的存储

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前正在回炉重造C语言&#xff08;2023暑假&#xff09; ✈️专栏&#xff1a;【C语言航路】 &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章对你…

酷开科技大屏营销,撬动营销新增量

5G、人工智能、元宇宙等技术的发展促使数字营销的内容、渠道、传播方式发生了一系列变化&#xff1b;存量竞争下&#xff0c;增长成为企业更加迫切、更具挑战的课题&#xff0c;品牌营销活动越来越围绕“生意增长”和“提效转化”的目标展开。 如今的市场环境下&#xff0c;产…

Nacos(服务注册与发现)+SpringBoot+openFeign项目集成

&#x1f4dd; 学技术、更要掌握学习的方法&#xff0c;一起学习&#xff0c;让进步发生 &#x1f469;&#x1f3fb; 作者&#xff1a;一只IT攻城狮 &#xff0c;关注我&#xff0c;不迷路 。 &#x1f490;学习建议&#xff1a;1、养成习惯&#xff0c;学习java的任何一个技术…

基础语言模型LLaMA

LLaMA包含从7B到65B参数的基础语言模型集合。Meta在数万亿个tokens上训练了模型&#xff0c;LLaMA-13B在大多数基准测试中优于GPT-3&#xff08;175B&#xff09;。 来自&#xff1a;LLaMA: Open and Efficient Foundation Language Models 目录 背景概述方法预训练数据架构Op…

openGauss学习笔记-09 openGauss 简单数据管理-创建数据库

文章目录 openGauss学习笔记-09 openGauss 简单数据管理-创建数据库9.1 语法格式9.2 参数说明9.3 示例 openGauss学习笔记-09 openGauss 简单数据管理-创建数据库 数据库安装完成后&#xff0c;默认生成名称为postgres的数据库。您需要自己创建一个新的数据库。 9.1 语法格式…

Appium+python自动化(十一)- 元素定位- 下卷超详解)

1、 List定位 List故名思义就是一个列表&#xff0c;在python里面也有list这一个说法&#xff0c;如果你不是很理解什么是list&#xff0c;这里暂且理解为一个数组或者说一个集合。首先一个list是一个集合&#xff0c;那么他的个数也就成了不确定性&#xff0c;所以这里需要用复…

C\C++ 使用exception类,抛出自定义异常并捕获

文章作者&#xff1a;里海 来源网站&#xff1a;https://blog.csdn.net/WangPaiFeiXingYuan 简介&#xff1a; 抛出异常&#xff0c;并捕获 exception 效果&#xff1a; 代码&#xff1a; #include <iostream> #include <exception> #include <stdexcept&g…

C# OpenCvSharp+DlibDotNet 人脸替换 换脸

效果 Demo下载 项目 VS2022.net4.8OpenCvSharp4DlibDotNet 相关介绍参考 代码 using DlibDotNet; using OpenCvSharp.Extensions; using OpenCvSharp; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Dra…