1

私はThread実行中ですが、アプリケーションが動作していて、スレッドが停止した後、自動的に閉じられ、ボタンがずっと非アクティブになるということthreadを表示したいと考えています。私のメインコード:JOptionPane.showMessageDialogJOptionPaneOK

class Index {
public static void main(String args[]) {
   NewThread ob1 = new NewThread("One");
   NewThread ob2 = new NewThread("Two");
   NewThread ob3 = new NewThread("Three");
    System.out.println("Thread One is alive: "+ ob1.t.isAlive());
    System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
    System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
   while (ob1.t.isAlive()){
    JOptionPane.showMessageDialog(null,"Thread One is alive");
   }
    System.out.println("Thread One is alive: "+ ob1.t.isAlive());
    System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
    System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
    System.out.println("Main thread exiting.");
}}

そして、それを実行するとJOptionPaneディスプレイが表示されますが、ボタンを押さない限り、彼は自動的に閉じませんOK

class NewThread implements Runnable {
String name; 
Thread t;
    NewThread(String threadname) {
        this.name = threadname;
        this.t = new Thread(this, name);
        System.out.println("New thread: " + t);
        this.t.start(); // Start the thread
    } 
public void run() {
    try {
    for(int i = 5; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(1000);
        }
        } catch (InterruptedException e) {
        System.out.println(name + " interrupted.");}
        System.out.println(name + " exiting.");
}}
4

2 に答える 2

2
 JOptionPane msg = new JOptionPane("Mensagem de advertência", JOptionPane.WARNING_MESSAGE);
    final JDialog dlg = msg.createDialog("Advertência");
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(5000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        dlg.setVisible(false);
      }
    }).start();
    dlg.setVisible(true);
于 2014-10-02T13:25:29.897 に答える