2016-08-08 4 views
-1

JFrameフォームでテキストフィールドを2回設定しましたが、最後に設定したテキストフィールドは残っています。 JFrameフォームを除いて、私はより多くの時間を成功させることができます。たとえば、JFrameフォームで何度もコンポーネントを設定する方法

class test extends JFrame { 
public static void main(String[] args) { 
    test t = new test(); 
    textfield.setText("Hello"); 
    long a = System.currentTimeMillis(); 
    long c = a; 
    while (c > a - 1000) { 
     a = System.currentTimeMillis(); 

    } 
    textfield.setText("Hello2"); 
} 
static private JTextField textfield; 
public test() { 
    super(); 
    setSize(300, 300); 
    textfield = new JTextField("Hello1"); 
    add(textfield); 
    setVisible(true); 
}} 

上記のコードは正常に実行されています。 1秒後に最初に "Hello"を表示し、次に "Hello2"を表示します。しかし、JFrameフォームでは、 "Hello"の代わりに "Hello2"と表示されます。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           

    jTextField1.setText("Hello"); 
    long a = System.currentTimeMillis(); 
    long c = a; 
    while (c > a - 1000) { 
     a = System.currentTimeMillis(); 

    } 
    jTextField1.setText("Hello2"); 


}           

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(deneme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(deneme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(deneme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(deneme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new deneme().setVisible(true); 
     } 
    }); 
} 

はまた、私はrepaint()validate()revalidate()方法を捜索しました。しかし、私のプロジェクトはJFrameフォームなので、JFrameオブジェクトはありません。したがって、私はこれらの方法を使用することはできません。

お返事ありがとうございます。

+1

「実行していない」とはどういう意味ですか?あなたの現在の行動と期待される行動を説明してください。 –

+1

あなたのプログラムで何をしたいのかをより正確に教えてください。 – whyn0t

+0

@ KrzysztofKosmatka私は、テキストフィールドには "Hello2"しか表示されないことを意味します。私は1秒後に "Hello"、次に "Hello2"と表示することを期待しています。ありがとうございました –

答えて

1

2番目のケースでは、イベントディスパッチスレッド(EDT)でjButton1ActionPerformed()メソッドが呼び出され、このスレッドを1秒間ブロックするためです。 EDTがブロックされている場合、UIは更新されません。テキストが第二の後に変更したい場合は、EDTをブロックしますが、いくつかのthred backgroungを使うべきではありません、例えば:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ 

    jTextField1.setText("Hello"); 
    new javax.swing.SwingWorker< Void, Void >() { 

     @Override 
     protected Void doInBackground() throws Exception { 
      Thread.sleep(1000); 
     } 

     @Override 
     protected void done() { 
      jTextField1.setText("Hello2"); 
     } 
    }.execute(); 
} 

とアクティブ待ち(while (c > a - 1000))を使用しないでください。代わりにThread.sleep()を使用してください。

+0

ありがとうございました。 –

+0

きれいで効果的です、感謝Krzysztof Kosmatka; @ K.タルハ:それを維持する=] – whyn0t

関連する問題