|
package JavaPerf;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//use Jna to get OnIdle: http://ochafik.free.fr/blog/?p=98
public class SwingTest extends JFrame //implements WindowListener//, WindowFocusListener
{
static int result = 0;
JLabel textLabel;
String[] args;
SwingTest(String[] args)
{
this.args = args;
}
public static void main(String[] args)
{
try
{
//Create and set up the window.
SwingTest frame = new SwingTest(args);
frame.setTitle("Simple GUI");
//frame.addWindowListener(frame);
frame.enableEvents(AWTEvent.WINDOW_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
//frame.addWindowFocusListener(frame);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(true);
frame.textLabel = new JLabel("Java Swing UI Test", SwingConstants.CENTER);
frame.textLabel.setAutoscrolls(true);
frame.textLabel.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(frame.textLabel, BorderLayout.CENTER);
//Display the window.
frame.setLocation(100, 100);// setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
catch (Exception ex)
{ System.exit(-1); }
}
/*
public void windowClosed(WindowEvent e)
{
//This will only be seen on standard output.
displayMessage(" windowClosed.");
//System.exit(0);
}
public void windowOpened(WindowEvent e)
{
displayMessage(" windowOpened.");
}
public void windowIconified(WindowEvent e)
{
displayMessage(" windowIconified.");
}
public void windowDeiconified(WindowEvent e)
{
displayMessage(" windowDeiconified.");
}
public void windowActivated(WindowEvent e)
{
displayMessage(" windowActivated.");
}
public void windowDeactivated(WindowEvent e)
{
displayMessage(" windowDeactivated.");
}
public void windowClosing(WindowEvent e)
{
displayMessage(" windowClosing.");
}
public void windowGainedFocus(WindowEvent e)
{
displayMessage(" windowGainedFocus.");
}
public void windowLostFocus(WindowEvent e)
{
displayMessage(" windowLostFocus.");
}
private void displayMessage(String str)
{
textLabel.setText(textLabel.getText() + " " + str);
}
*/
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
switch (e.getID())
{
case WindowEvent.WINDOW_OPENED:
long mainEntryTime = System.currentTimeMillis();//miliseconds since since 1970/1/1
if (args.length == 0)
{
try
{
System.gc();
System.runFinalization();
Thread.sleep(2000);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else
{
//FileTimeUtc adjusted for java epoch
long fileTimeUtc = Long.parseLong(args[0]);//100 nanoseconds units since 1601/1/1
long launchTime = fileTimeUtc - 116444736000000000L;//100 nanoseconds units since 1970/1/1
launchTime /= 10000;//miliseconds since since 1970/1/1
result = (int)(mainEntryTime - launchTime);
//displayMessage("startup time " + result + " ms");
}
//displayMessage(" WINDOW_OPENED");
break;
case WindowEvent.WINDOW_CLOSED:
System.exit(result);
break;
/*
case WindowEvent.WINDOW_CLOSING:
displayMessage(" WINDOW_CLOSING");
break;
case WindowEvent.WINDOW_ACTIVATED:
displayMessage(" WINDOW_ACTIVATED");
break;
case WindowEvent.WINDOW_LAST:
displayMessage(" WINDOW_LAST");
break;
default:
break;
*/
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.