学习来源:https://blog.csdn.net/minfanphd/article/details/116974889
一、GUI (1. 对话框相关控件)1、GUI(图形用户接口)是指采用图形方式显示的计算机操作用户界面。GUI便捷、准确,实用性很强,主要功能是实现人与计算机等电子设备的人机交互。
2、图形用户界面是一种人与计算机通信的界面显示格式,允许用户使用鼠标等输入设备操纵屏幕上的图标或菜单选项,以选择命令、调用文件、启动程序或执行其它一些日常任务。与通过键盘输入文本或字符命令来完成例行任务的字符界面相比,图形用户界面有许多优点。图形用户界面由窗口、下拉菜单、对话框及其相应的控制机制构成,在各种新式应用程序中都是标准化的,即相同的操作总是以同样的方式来完成,在图形用户界面,用户看到和操作的都是图形对象,应用的是计算机图形学的技术。
3、主要组成。桌面、视图、标签、菜单、图标和按钮等。
4、ApplicationShowdown.java 仅用于退出图形用户界面 GUI。
5、代码
package bp神经网络;
import java.awt.event.*;
public class ApplicationShutdown implements WindowListener, ActionListener {
public static ApplicationShutdown applicationShutdown = new ApplicationShutdown();
private ApplicationShutdown() {
}// Of ApplicationShutdown.
public void windowClosing(WindowEvent comeInWindowEvent) {
System.exit(0);
}// Of windowClosing.
public void windowActivated(WindowEvent comeInWindowEvent) {
}// Of windowActivated.
public void windowClosed(WindowEvent comeInWindowEvent) {
}// Of windowClosed.
public void windowDeactivated(WindowEvent comeInWindowEvent) {
}// Of windowDeactivated.
public void windowDeiconified(WindowEvent comeInWindowEvent) {
}// Of windowDeiconified.
public void windowIconified(WindowEvent comeInWindowEvent) {
}// Of windowIconified.
public void windowOpened(WindowEvent comeInWindowEvent) {
}// Of windowOpened.
public void actionPerformed(ActionEvent ee) {
System.exit(0);
}// Of actionPerformed.
}// Of class ApplicationShutdown
6、DialogCloser.java 用于关闭窗口, 而不是整个的 GUI。
package bp神经网络;
import java.awt.*;
import java.awt.event.*;
public class DialogCloser extends WindowAdapter implements ActionListener {
private Dialog currentDialog;
public DialogCloser() {
super();
}// Of the first constructor
public DialogCloser(Dialog paraDialog) {
currentDialog = paraDialog;
}// Of the second constructor
public void windowClosing(WindowEvent paraWindowEvent) {
paraWindowEvent.getWindow().dispose();
}// Of windowClosing.
public void actionPerformed(ActionEvent paraEvent) {
currentDialog.dispose();
}// Of actionPerformed.
}// Of class DialogCloser
7、ErrorDialog.java 用于显示出错信息。
package bp神经网络;
import java.awt.*;
public class ErrorDialog extends Dialog {
private static final long serialVersionUID = 124535235L;
public static ErrorDialog errorDialog = new ErrorDialog();
private TextArea messageTextArea;
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);
}// Of constructor
public void setMessageAndShow(String paramMessage) {
messageTextArea.setText(paramMessage);
setVisible(true);
}// Of setTitleAndMessage
}// Of class ErrorDialog
8、GUICommon.java 存储一些公用变量。
package bp神经网络;
import java.awt.*;
import javax.swing.*;
public class GUICommon extends Object {
public static Frame mainFrame = null;
public static JTabbedPane mainPane = null;
public static int currentProjectNumber = 0;
public static final Font MY_FONT = new Font("Times New Romans", Font.PLAIN, 12);
public static final Color MY_COLOR = Color.lightGray;
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!");
} // Of if
}// Of setFrame
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!");
} // Of if
}// Of setPAne
}// Of class GUICommon
9、HelpDialog.java 显示帮助信息, 这样, 在主界面点击 Help 按钮时, 就会显示相关参数的说明。
package bp神经网络;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class HelpDialog extends Dialog implements ActionListener {
private static final long serialVersionUID = 3869415040299264995L;
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);
}// Of constructor
public void actionPerformed(ActionEvent ee) {
setVisible(true);
}// Of actionPerformed.
}// Of class HelpDialog
GUI (2. 数据读取控件)
1、DoubleField.java 用于接受实型值。
package bp神经网络;
import java.awt.*;
import java.awt.event.*;
public class DoubleField extends TextField implements FocusListener {
private static final long serialVersionUID = 363634723L;
protected double doubleValue;
public DoubleField() {
this("5.13", 10);
}// Of the first constructor
public DoubleField(String paraString) {
this(paraString, 10);
}// Of the second constructor
public DoubleField(int paraWidth) {
this("5.13", paraWidth);
}// Of the third constructor
public DoubleField(String paraString, int paraWidth) {
super(paraString, paraWidth);
addFocusListener(this);
}// Of the fourth constructor
public void focusGained(FocusEvent paraEvent) {
}// Of focusGained
public void focusLost(FocusEvent paraEvent) {
try {
doubleValue = Double.parseDouble(getText());
} catch (Exception ee) {
ErrorDialog.errorDialog
.setMessageAndShow(""" + getText() + "" Not a double. Please check.");
requestFocus();
} // Of try
}// Of focusLost
public double getValue() {
try {
doubleValue = Double.parseDouble(getText());
} catch (Exception ee) {
ErrorDialog.errorDialog
.setMessageAndShow(""" + getText() + "" Not a double. Please check.");
requestFocus();
} // Of try
return doubleValue;
}// Of getValue
}// Of class DoubleField
2、IntegerField.java
package bp神经网络;
import java.awt.*;
import java.awt.event.*;
public class IntegerField extends TextField implements FocusListener {
private static final long serialVersionUID = -2462338973265150779L;
public IntegerField() {
this("513");
}// Of constructor
public IntegerField(String paraString, int paraWidth) {
super(paraString, paraWidth);
addFocusListener(this);
}// Of constructor
public IntegerField(String paraString) {
super(paraString);
addFocusListener(this);
}// Of constructor
public IntegerField(int paraWidth) {
super(paraWidth);
setText("513");
addFocusListener(this);
}// Of constructor
public void focusGained(FocusEvent paraEvent) {
}// Of focusGained
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();
}
}// Of focusLost
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;
}// Of getValue
}// Of class IntegerField
3、FilenameField.java 则需要借助于系统提供的 FileDialog。
package bp神经网络;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class FilenameField extends TextField implements ActionListener,
FocusListener {
private static final long serialVersionUID = 4572287941606065298L;
public FilenameField() {
super();
setText("");
addFocusListener(this);
}// Of constructor
public FilenameField(int paraWidth) {
super(paraWidth);
setText("");
addFocusListener(this);
}// Of constructor
public FilenameField(int paraWidth, String paraText) {
super(paraWidth);
setText(paraText);
addFocusListener(this);
}// Of constructor
public FilenameField(String paraText, int paraWidth) {
super(paraWidth);
setText(paraText);
addFocusListener(this);
}// Of constructor
public void setText(String paraText) {
if (paraText.trim().equals("")) {
super.setText("unspecified");
} else {
super.setText(paraText.replace('\', '/'));
}//Of if
}// Of setText
public void actionPerformed(ActionEvent paraEvent) {
FileDialog tempDialog = new FileDialog(GUICommon.mainFrame,
"Select a file");
tempDialog.setVisible(true);
if (tempDialog.getDirectory() == null) {
setText("");
return;
}//Of if
String directoryName = tempDialog.getDirectory();
String tempFilename = directoryName + tempDialog.getFile();
//System.out.println("tempFilename = " + tempFilename);
setText(tempFilename);
}// Of actionPerformed
public void focusGained(FocusEvent paraEvent) {
}// Of focusGained
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("");
}
}// Of focusLost
}// Of class FilenameField



