Tutorial Addendum on Beat - Beat JFrame
Interrupting Anatomy Threads
Problem: I wish to understand how some accoutrement are created for the anatomy window
and can those accoutrement been interrupted.
Solution: This is easy. Just get the account of all accoutrement of the current
thread group, and arrest them one by one. Actuality is a sample program to show
you how to do this:
/**
* FrameThreads.java
* Absorb (c) 2002 by Dr. Yang
*/
import java.awt.*;
import javax.swing.*;
public chic FrameThreads {
accessible changeless abandoned main(String[] a) {
JFrame f = new JFrame("Frame 1");
f.setBounds(0,0,100,100);
f.setVisible(true);
f = new JFrame("Frame 2");
f.setBounds(50,50,100,100);
f.setVisible(true);
f = new JFrame("Frame 3");
f.setBounds(100,100,100,100);
f.setVisible(true);
Thread[] l = new Thread[100];
int n = Thread.enumerate(l);
Cilia g = null;
for (int i=0; i<n; i++) {
System.out.println("Active cilia = "+l[i].getName());
}
Cilia t = Thread.currentThread();
for (int i=0; i<n; i++) {
try {
Thread.sleep(1000*30);
} bolt (Exception e) {
System.out.println("Interrupted.");
}
if (t != l[i]) {
System.out.println("Interrupting cilia = "
+l[i].getName());
l[i].interrupt();
}
}
}
}
If you run this program , you will see 3 anatomy windows assuming up on the screen.
After about every 30 seconds, my program will forward an arrest arresting to each
thread. If the arrest arresting alcove "Java2D Disposer" thread, all the
windows will be closed. The afterward account shows you the achievement of my program:
Active cilia = main
Active cilia = AWT-Windows
Active cilia = AWT-Shutdown
Active cilia = AWT-EventQueue-0
Active cilia = Java2D Disposer
Interrupting cilia = AWT-Windows
Interrupting cilia = AWT-Shutdown
Interrupting cilia = AWT-EventQueue-0
Interrupting cilia = Java2D Disposer
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:111)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:127)
at sun.java2d.Disposer.run(Disposer.java:100)
at java.lang.Thread.run(Thread.java:534)
AWT acquaintance activation interrupted:
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:429)
at sun.awt.AWTAutoShutdown.activateBlockerThread(AWTAutoShut...
at sun.awt.AWTAutoShutdown.setToolkitBusy(AWTAutoShutdown.ja...
at sun.awt.AWTAutoShutdown.notifyToolkitThreadBusy(AWTAutoSh...
at sun.awt.windows.WToolkit.eventLoop(Native Method)
at sun.awt.windows.WToolkit.run(WToolkit.java:262)
at java.lang.Thread.run(Thread.java:534)
Note 1: Beat arrangement uses 4 threads: AWT-Windows, AWT-Shutdown, AWT-EventQueue-0,
and Java2D Disposer.
Note 2: If cilia "Java2D Disposer" is interrupted, the absolute appliance terminates.
Displaying Chinese Characters in Anatomy Title
Problem: I wish to affectation Chinese characters in the appellation of a anatomy
window.
Solution: You can alone do this if you are using JDK 1.4.2 and accept a
Unicode chantry installed to abutment Chinese characters. In my sample cipher listed
below, I am using chantry SimSun, which was installed as allotment of Windows
multi-language support.
/**
* JFrameChinese.java
* Absorb (c) 2004 by Dr. Yang
*/
import java.awt.*;
import javax.swing.*;
public chic JFrameChinese {
accessible changeless abandoned main(String[] a) {
JFrame f = new JFrame();
f.setFont(new Font("SimSun",Font.PLAIN, 12));
f.setTitle("Hello world! - u7535u8111u4F60u597DuFF01");
f.setBounds(100,50,500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Note 1: With JDK 1.4.1 or lower, the characters are displayed as "?".
I don t any way to fix it. If you accept any suggestions, amuse allotment them
with me.
Note 2: To analysis if you accept SimSun chantry installed or not, you can attending
for winntfontssimsun.ttc book in your arrangement agenda tree.
|
Tags: program, system, windows, chinese, characters, swing, installed, method, public, string, frame, object thread, jframe, frame, swing, windows, java2d, system, disposer, awtautoshutdown, referencequeue, object, setvisible, interrupt, interrupted, threads, program, import, setbounds, public, characters, chinese, simsun, installed, println, eventqueue, native, method, wtoolkit, , java lang, thread awt, 100 100, sun awt, new jframe, java2d disposer, setvisible true, chinese characters, awt windows, awt awtautoshutdown, native method, thread java2d, jframe frame, system out, awt eventqueue, lang object, object wait, java lang object, new jframe frame, lang object wait, referencequeue remove referencequeue, ref referencequeue remove, remove referencequeue java, java lang thread, awt windows wtoolkit, wait native method, object wait native, void main string, public static void, import javax swing, swing swing jframe, static void main, |
Also see ...
PermalinkArticle In : Computers & Technology - swing