Hallo, ich versuche gerade etwas zu schreiben, aber kann auch nicht genau sagen, was. Jedenfalls sollte ein JFrame nur 2 Sek. sichtbar sein:
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
/**
* @author ikwudls
*/
public class Disappear {
private static final int t1 = 2000;
private long t2;
private Runnable r = new Runnable() {
@Override
public void run() {
try {
while (true) {
long t3 = t2 - System.currentTimeMillis();
if (t3 > 0) {
Thread.sleep(t3);
} else {
Thread.sleep(/*bei größer 1000 funktionierts*/ t1);
}
synchronized (Disappear.class) {
if (System.currentTimeMillis() >= t2) {
d = null;
jf.dispose();
}
}
}
} catch (InterruptedException ie) {
System.out.println(ie);
}
}
};
private JFrame jf;
private JLabel jl;
private static Disappear d;
public synchronized static void show(String str) {
if (d == null) {
d = new Disappear();
}
d.show1(str);
}
private Disappear() {
jf = new JFrame();
jl = new JLabel();
JButton jb = new JButton("Ok");
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
synchronized (Disappear.class) {
d = null;
jf.dispose();
}
}
});
jf.setLayout(new GridLayout(2, 1));
jf.add(jl);
jf.add(jb);
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Thread t = new Thread(r);
t.setDaemon(true);
t.start();
}
private void show1(final String str) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jl.setText(str);
jf.pack();
jf.setVisible(true);
}
});
t2 = System.currentTimeMillis() + t1;
}
public static void main(String[] args) {
Disappear.show("12345");
}
}```
In Zeile 32 harkts, wenn ich als Wert z. B. nur 1000 eintrage, dann beendet die Anwendung nicht.