application.java
字号:
package piy;
import java.io.Serializable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.reflect.*;
/**
* Represents a compiled Project.
* @author David Vivash
* @version 1.0.2, 20/04/01
*/
public class Application extends Thread implements Serializable
{
private JFrame mainWindow = null;
public boolean running = true;
private ArrayList windows = new ArrayList(2);
private AbstractMap componentsToListeners = new HashMap(4);
/**
* The main window is the one that will cause the application to terminate when it is closed.
* The parameter passed here doesn't need to be added to the application via addWindow.
* @param mainWindow the window to set as the main window
*/
public void setMainWindow(JFrame mainWindow) {
this.mainWindow = mainWindow;
}
/**
* Add a window to the application.
* @param window the window to add
*/
public void addWindow(UserWindow window) {
windows.add(window);
}
/**
* Adds a mapping from a component to an abstract map, which should contain mappings from
* PIYEventListener classes to strings, then strings to action lists.
* @param c the component mapped from
* @param listeners the listeners map of PIYEventListeners to another abstract map
*/
public void addComponentMapping(Component c, AbstractMap listeners) {
componentsToListeners.put(c, listeners);
}
/**
* Run the project. Uses the Look And Feel already set up. This method automatically sets
* up any listeners on components that have been specified through the addComponentMapping
* method. Rather than calling this method, it is recommended that the thread start() method
* is called instead.
*/
public void run() {
try { System.setErr(new java.io.PrintStream(new java.io.FileOutputStream("Error.log"))); }
catch (Exception e) { }
//Set up the listeners on the components.
Iterator i = componentsToListeners.keySet().iterator();
AbstractMap listeners;
Component component;
while (i.hasNext()) {
component = (Component)i.next();
listeners = (AbstractMap)componentsToListeners.get(component);
Iterator listenersIterator = listeners.keySet().iterator();
while (listenersIterator.hasNext()) {
Class listenerClass = (Class)listenersIterator.next();
try{
PiyEventListener eventListener = (PiyEventListener)listenerClass.newInstance();
String addMethodName = eventListener.getAddListenerMethod();
Class addMethodType = eventListener.getAddListenerType();
Method addMethod = component.getClass().getMethod(addMethodName, new Class[] { addMethodType });
addMethod.invoke(component, new Object[] { eventListener });
eventListener.setActionLists((AbstractMap)listeners.get(listenerClass));
} catch (Exception e) {
System.err.println("Error setting up listeners in compiled project");
e.printStackTrace();
}
}
}
running = true;
if (mainWindow != null) {
mainWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
running = false;
}
});
mainWindow.setVisible(true);
//Nothing seems to get proper focus unless I do the following: SORT THIS OUT IT IS ANNOYING!
mainWindow.setState(Frame.ICONIFIED);
mainWindow.setState(Frame.NORMAL);
}
}
/**
* Disposes of all windows used in this application. This should be enough to kill the
* AWT thread associated with the application (ie. the VM should terminate after a call
* to this method, unless other threads are currently still active).
*/
public void closeWindows() {
for (int i=0; i<windows.size(); i++)
((JFrame)windows.get(i)).dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -