package machinelearning.gui;
import java.awt.event.*;
/**
* ******************************************
* Shut down the application according to window action or button action.
*
* @author Michelle Min MitchelleMin@163.com
* @date 2021-08-09
* ******************************************
*/
public class ApplicationShutdown implements WindowListener, ActionListener {
/**
* Only one instance.
*/
public static ApplicationShutdown applicationShutdown = new ApplicationShutdown();
/**
***************************
* This constructor is private such that the only instance is generated here.
***************************
*/
private ApplicationShutdown() {
}//of ApplicationShutdown.
/**
***************************
* Shutdown the system
***************************
*/
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.
/**
********************
* actionPerformed.
********************
*/
public void actionPerformed(ActionEvent ee) {
System.exit(0);
}// Of actionPerformed.
}// Of class ApplicationShutdown
package machinelearning.gui;
import java.awt.*;
import java.awt.event.*;
/**
* ******************************************
* Close the dialog.
*
* @author Michelle Min MitchelleMin@163.com
* @date 2021-08-10
* ******************************************
*/
public class DialogCloser extends WindowAdapter implements ActionListener {
/**
* The dialog under control.
*/
private Dialog currentDialog;
/**
***************************
* The first constructor.
***************************
*/
public DialogCloser() {
super();
}//of the first constructor
/**
***************************
* The second constructor.
*
* @param paraDialog
* the dialog under control
***************************
*/
public DialogCloser(Dialog paraDialog) {
currentDialog = paraDialog;
}//of the second constructor
/**
***************************
* Close the dialog which clicking the cross at the up-right corner of the window.
*
* From it we can obtain which window sent the message because X
* was used.
***************************
*/
public void windowClosing(WindowEvent paraWindowEvent) {
paraWindowEvent.getWindow().dispose();
}//of windowClosing.
/**
***************************
* Close the dialog while pushing an "OK" or "Cancel" button.
*
* @param paraEvent
* Not considered.
***************************
*/
public void actionPerformed(ActionEvent paraEvent) {
currentDialog.dispose();
}//of actionPerformed.
}//of class DialogCloser
package machinelearning.gui;
import java.awt.*;
/**
* ******************************************
* For error message.
*
* @author Michelle Min MitchelleMin@163.com
* @date 2021-08-10
* ******************************************
*/
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);
}//of constructor
/**
***************************
* set message.
*
* @param paramMessage
* the new message
***************************
*/
public void setMessageAndShow(String paramMessage) {
messageTextArea.setText(paramMessage);
setVisible(true);
}//of setTitleAndMessage
}//of class ErrorDialog
package machinelearning.gui;
import java.awt.*;
import javax.swing.*;
/**
* ******************************************
* Manage the GUI.
*
* @author Michelle Min MitchelleMin@163.com
* @date 2021-08-10
* ******************************************
*/
public class GUICommon extends Object {
/**
* 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!");
}//of if
}//of setFrame
/**
***************************
* 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!");
} //of if
}//of setPAne
}//of class GUICommon
package machinelearning.gui;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
/**
* ******************************************
* Display the help message.
*
* @author Michelle Min MitchelleMin@163.com
* @date 2021-08-10
* ******************************************
*/
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());
}//of try
// 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
/**
*************************
* Simply set it visible.
*************************
*/
public void actionPerformed(ActionEvent ee) {
setVisible(true);
}//of actionPerformed.
}//of class HelpDialog
day77
最新推荐文章于 2024-02-22 12:11:50 发布
这篇博客详细介绍了如何在Java GUI应用程序中实现窗口关闭功能,包括通过窗口事件和按钮点击来关闭应用。同时,展示了创建错误提示对话框和帮助对话框的方法,对话框能够显示错误信息或加载外部帮助文件供用户查阅。
摘要由CSDN通过智能技术生成