JFrame Form中如何多次设置一个组件
How to set a component many times in JFrame Form
我尝试在 JFrame 表单中设置文本字段两次,但我设置的最后一个文本字段仍然存在。除了 JFrame Form,我可以成功设置更多次。例如
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);
}}
上面的代码 运行 成功。先显示"Hello",一秒后显示"Hello2"。但在 JFrame 窗体中,只显示 "Hello2" 而不是 "Hello".
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 Form,所以没有JFrame对象。因此,我不能使用这些方法。
提前感谢您的回答。
这是因为在第二种情况下 jButton1ActionPerformed()
方法在事件调度线程 (EDT) 上被调用并且您阻塞了该线程一秒钟。如果 EDT 被阻止,则 UI 不会更新。如果你想让文本在一秒钟后改变,你不应该阻止 EDT,而是使用一些背景线程,例如:
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()
。
我尝试在 JFrame 表单中设置文本字段两次,但我设置的最后一个文本字段仍然存在。除了 JFrame Form,我可以成功设置更多次。例如
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);
}}
上面的代码 运行 成功。先显示"Hello",一秒后显示"Hello2"。但在 JFrame 窗体中,只显示 "Hello2" 而不是 "Hello".
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 Form,所以没有JFrame对象。因此,我不能使用这些方法。
提前感谢您的回答。
这是因为在第二种情况下 jButton1ActionPerformed()
方法在事件调度线程 (EDT) 上被调用并且您阻塞了该线程一秒钟。如果 EDT 被阻止,则 UI 不会更新。如果你想让文本在一秒钟后改变,你不应该阻止 EDT,而是使用一些背景线程,例如:
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()
。