アプリケーションやアプレットの構築時および表示時には、イベントディスパッチスレッドに制御を渡すように注意してください。制御を渡し、Swing の処理を開始するためには、invokeLater を使用することをお勧めします。
public class MyApp implements Runnable {
public void run() {
// Invoked on the event dispatching thread.
// Construct and show GUI.
}
public static void main (String [] args) {
SwingUtilities.invokeLater(new MyApp(args));
}
}
public class MyApp {
MyApp(String[] args) {
// Invoked on the event dispatching thread.Do any initialization
// here.
}
public void show() {
// Show the UI.
}
public static void main(final String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyApp(args).show();
}
});
}
}
Runnable doHelloWorld = new Runnable() {
public void run() {
System.out.println("Hello World on " + Thread.currentThread());
}
};
SwingUtilities.invokeLater(doHelloWorld);
System.out.println("This might well be displayed before the other message.");
public static void main(final String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyApp(args).show();
}
});
}
isMiddleMouseButton(MouseEvent anEvent)
マウスイベントが中央のマウスボタンを示す場合に true を返します。
isRightMouseButton(MouseEvent anEvent)
マウスイベントが右マウスボタンを示す場合に true を返します。
isLeftMouseButton(MouseEvent anEvent)
マウスイベントが左マウスボタンを示す場合に true を返します。
isEventDispatchThread()
現在のスレッドが AWT イベントディスパッチスレッドの場合に true を返します。
java.awt.EventQueue.isDispatchThread()
long getId()
このスレッドの識別子を返します。
String getName()
このスレッドの名前を返します。
/**
* ボタンにイベント
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EventTest2 extends JFrame implements ActionListener {
JLabel label;
JLabel declabel;
JButton button;
int count = 0;
int deccount = 100;
/* コンストラクタ */
public EventTest2(){
System.out.println(Thread.currentThread().getName());
setDefaultCloseOperation(EXIT_ON_CLOSE); //終了処理を追加
label = new JLabel(count + " clicks", JLabel.CENTER); //ラベルを作る
declabel = new JLabel(deccount + " remains", JLabel.CENTER); //ラベルを作る
button = new JButton("Click Me!"); //ボタンを作る
setLayout(new BorderLayout()); //レイアウトマネージャを指定
add(declabel, BorderLayout.NORTH);
add(label, BorderLayout.CENTER); //ラベルを真ん中に
add(button, BorderLayout.SOUTH); //ボタンを下に配置
setLocationByPlatform(true); //ウインドウの位置を自動化
pack(); //ボタンに合わせてフレームの大きさを調節
setVisible(true); //実際に表示する
button.addActionListener(this);
System.out.println(Thread.currentThread().getName());
}
/* イベントがあったらここに来る */
public void actionPerformed(ActionEvent e) {
//イベントがbuttonで起こっていたら…
System.out.println(Thread.currentThread().getName());
if (e.getSource() == button) {
//countを増加
count++;
deccount--;
//ラベルの文字を換える。
label.setText(count + " clicks");
declabel.setText(deccount + " remains");
}
}
/********* main **********/
public static void main(String[] args){
/* フレームを作成(事実上のプログラム実行) */
System.out.println(Thread.currentThread().getName());
EventTest2 myframe = new EventTest2();
System.out.println(Thread.currentThread().getName());
EventTest2 myfram2 = new EventTest2();
}
}
