日撸java三百行day77-79

文章目录

  • 说明
  • 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 总结

说明

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

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

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

相关文章

day35-Postman/ajax

0目录 1.postman 2.ajax 1.Postman 1.1 定义&#xff1a;postman用于测试http协议接口&#xff0c;无论是开发还是测试人员 1.2 Servlet中的doGet&#xff08;&#xff09;/doPost…

idea 常用快捷键总结

IDEA常用快捷键总结 很多新手小白在使用IDEA进行代码编写的时候 对快捷键很感兴趣 这里泡泡给大家总结了一些常用的快捷键 希望能帮助到你 记得要收藏下来时常观看并且练习&#xff0c;才能熟练哦~ 1. 根据psvm或者main快速生成主函数 我们可以在类中输入psvm 或者main 然后I…

C# Winfrom将DataGridView数据导入Excel

1.项目添加Word和Excel的COM类型库引用 2.创建Excel工作表 //定义Excel操作对象Microsoft.Office.Interop.Excel.Application excelApp new Microsoft.Office.Interop.Excel.Application();//定义Excel工作表Microsoft.Office.Interop.Excel.Worksheet worksheet excelApp.Wo…

TCP的窗口控制和重发控制【TCP原理(笔记三)】

文章目录 利用窗口控制提高速度窗口控制与重发控制确认应答未能返回的情况某个报文段丢失的情况 控制流 利用窗口控制提高速度 TCP以1个段为单位&#xff0c;每发一个段进行一次确认应答的处理&#xff0c;如图。这样的传输方式有一个缺点。那就是&#xff0c;包的往返时间越长…

Centos使用docker部署nacos

Centos使用docker部署nacos 对于使用Docker部署Nacos&#xff0c;您可以按照以下步骤进行操作&#xff1a; 在您的服务器上安装Docker和Docker Compose。创建一个用于存储Nacos数据的目录&#xff0c;例如/path/to/nacos/data。创建一个docker-compose.yml文件&#xff0c;并…

心电前置放大电路制作与原理详细分析(附电路板实物图)

心电前置放大电路制作与原理详细分析(附电路板实物图) 实验目的实验结果实验电路图原理解释与计算实验测试过程实验参数测量实验洞洞板焊接实验目的 心电信号具有微弱、低频、和高阻抗等特性,极其容易受到干扰。为了实现心电信号的放大,前置放大器需要满足高输入阻抗、高共…

前端开发如何更好的避免样式冲突?级联层(CSS@layer)

目录 前言 一、什么是级联层 (Cascade Layers)&#xff1f; 1.1 级联层的官方定义 1.2 级联层为了解决什么问题&#xff1f; 二、理解级联层的前提 —— 级联 (cascade) 2.1 什么是级联&#xff1f; 2.2 当前级联的排序标准 2.3 级联起源&#xff08;Cascading Origins…

Spring Boot进阶(54):Windows 平台安装 MongoDB数据库 | 超级详细,建议收藏

1. 前言&#x1f525; Windows如何安装MongoDB数据库及使用呢&#xff1f;这将又会是干货满满的一期&#xff0c;全程无尿点不废话只抓重点教&#xff0c;具有非常好的学习效果&#xff0c;拿好小板凳准备就坐&#xff01;希望学习的过程中大家认真听好好学&#xff0c;学习的途…

第二章:在html中使用javascript

1、在html页面中插入js的主要方法就是使用<script>元素 2、html4.01为<script>定义了以下6个属性&#xff1a;【language已经废弃&#xff0c;其他5个属性都是可选的】 async 表示应该立即下载脚本&#xff0c;但不应该妨碍页面中的其他操作&#xff0c;比如下载…

中金:龙湖基本面稳健,股价超跌具备配置价值

恒大2.4万亿元的天量债务爆出后&#xff0c;让本就信心不足的房地产行业&#xff0c;越发雪上加霜&#xff0c;房企股价遭遇集体下挫&#xff0c;业内公认的万科、龙湖、保利、中海等“优等生”也不免被波及。多家证券机构提醒&#xff0c;行业预期降至冰点的情况下&#xff0c…

预付费电表收费系统

预付费电表收费系统是一种先进的电表管理系统&#xff0c;它能够帮助电力公司更加高效地管理电表收费&#xff0c;提高用电效率&#xff0c;降低能源浪费。本文将从以下几个方面介绍预付费电表收费系统的特点和优势。 一、预付费电表收费系统的原理 预付费电表收费系统是指用户…

京东自动化功能之商品信息监控是否有库存

这里有两个参数,分别是area和skuids area是地区编码,我这里统计了全国各个区县的area编码,用户可以根据实际地址进行构造skuids是商品的信息ID填写好这两个商品之后,会显示两种状态,判断有货或者无货状态,详情如下图所示 简单编写下python代码,比如我们的地址是北京市…

2023无监督摘要顶会论文合集

2023无监督摘要顶会论文合集 写在最前面ACL-2023Aspect-aware Unsupervised Extractive Opinion Summarization 面向的无监督意见摘要&#xff08;没找到&#xff09;Unsupervised Extractive Summarization of Emotion Triggers *情绪触发(原因)的 *无监督 *抽取式 摘要&#…

postgresql 内核源码分析 表锁relation lock的使用,session lock会话锁的应用场景,操作表不再困难

​专栏内容&#xff1a; postgresql内核源码分析 手写数据库toadb 并发编程 个人主页&#xff1a;我的主页 座右铭&#xff1a;天行健&#xff0c;君子以自强不息&#xff1b;地势坤&#xff0c;君子以厚德载物. 表锁介绍 当表打开&#xff0c;或者操作表时&#xff0c;都需要…

CDA数据分析系01 anaconda

简介 数据处理集成包&#xff0c;不局限于python 创建一个新的environment conda create --name python34 python3.4 激活一个environment activate python34 # for windows conda的package管理 类似pip&#xff0c;conda install xxxx 查看已安装的python包 conda list…

【计算机视觉】DINOv2(视觉大模型)代码四个不同模型的对比,以 28 * 28 的图像为例(完整的源代码)

文章目录 一、ViT-S/14二、ViT-B/14三、ViT-L/14四、ViT-g/14 一、ViT-S/14 import torch import torchvision.transforms as T import matplotlib.pyplot as plt import numpy as np import matplotlib.image as mpimg from PIL import Image from sklearn.decomposition im…

opencv 05 彩色RGB像素值操作

opencv 05 彩色RGB像素值操作 RGB 模式的彩色图像在读入 OpenCV 内进行处理时&#xff0c;会按照行方向依次读取该 RGB 图像的 B 通道、G 通道、R 通道的像素点&#xff0c;并将像素点以行为单位存储在 ndarray 的列中。例如&#xff0c; 有一幅大小为 R 行C 列的原始 RGB 图像…

React和Vue生命周期、渲染顺序

主要就是命名不同 目录 React 组件挂载 挂载前constructor() 挂载时render() 挂载后componentDidMount()&#xff1a;初始化节点 更新 更新时render()&#xff1a;prop/state改变 更新后componentDidUpdate() 卸载 卸载前componentWillUnmount()&#xff1a;清理 V…

王道计算机网络学习笔记(4)——网络层

前言 文章中的内容来自B站王道考研计算机网络课程&#xff0c;想要完整学习的可以到B站官方看完整版。 四&#xff1a;网络层 ​​​​​​​​​​​​​​在计算机网络中&#xff0c;每一层传输的数据都有不同的名称。 物理层&#xff1a;传输的数据称为比特&#xff08;Bi…

字节跳动面试挂在2面,复盘后,决定二战.....

先说下我基本情况&#xff0c;本科不是计算机专业&#xff0c;现在是学通信&#xff0c;然后做图像处理&#xff0c;可能面试官看我不是科班出身没有问太多计算机相关的问题&#xff0c;因为第一次找工作&#xff0c;字节的游戏专场又是最早开始的&#xff0c;就投递了&#xf…