Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
Nein, wie lange es dauert bis Jesus wieder aufersteht.Die geschätzte Zeit für was? Bis die Welt untergeht?
Ist schon implementiert. Ich hätte aber gerne noch eine ungefähre Zeit. Gibt es eine Approximation für die Laufzeit eines Algorithmus oder generell einer Methode in reeller Zeit?Fortschritt im allgemeinen stellst du mit ner ProgressBar dar
Du hast also keine Schleife sondern eine Methode die etwas längliches ausführt und willst wissen wie lange das denn nun dauern wird und wieviel davon du schon erledigt hast oder wie ?Naja ich könnte ja die Methoden aufrufen und die Zeit messen. Aber das hätte zur Folge dass das Programm doppelt so lang bräuchte.
Du hast also keine Schleife sondern eine Methode die etwas längliches ausführt und willst wissen wie lange das denn nun dauern wird und wieviel davon du schon erledigt hast oder wie ?
ProgressFrame pF = new ProgressFrame("Berechnet Zeitpunkt des Weltuntergangs....");
for (int i = 0;i < 100;i++) {
pF.setValue(i);
Thread.sleep(1000);
}
import java.util.Date;
public class ProgressFrame extends javax.swing.JFrame {
private Date lastChange = null;
private int value = 0;
private int lastValue = 0;
private int maximum;
public ProgressFrame(String title) {
initComponents();
setTitle(title);
progressBar.setStringPainted(false);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
pack();
setLocationRelativeTo(null);
}
public void setValue(int value)
{
progressBar.setValue(value);
this.value = value;
if (lastChange == null)
{
lastChange = new Date();
}
Date now = new Date();
long diff = now.getTime() - lastChange.getTime();
if (diff >= 1000)
{
int dx = (int) (((new Date()).getTime() - lastChange.getTime()));
float change = (float) (value - lastValue) / (float) dx;
etaLabel.setText(
formatAsETA((int) (((float) (maximum - value) / change) / 1000))
+ " remaining"
);
this.lastValue = value;
lastChange = now;
}
}
public void setMinimum(int min)
{
progressBar.setMinimum(min);
}
public void setMaximum(int max)
{
progressBar.setMaximum(max);
maximum = max;
}
public void setAction(String action)
{
currentActionLabel.setText(action);
}
public void setIndeterminate(boolean is)
{
progressBar.setIndeterminate(is);
if (is)
{
etaLabel.setText("");
}
else
{
etaLabel.setText(
"estimating..."
);
}
}
private static String formatAsETA(int nSeconds)
{
int minutes = 0;
while (nSeconds >= 60)
{
nSeconds -= 60;
minutes++;
}
if (minutes != 0)
{
return minutes + "m " + nSeconds + "s";
}
else
{
return nSeconds + "s";
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
currentActionLabel = new javax.swing.JLabel();
etaLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setResizable(false);
currentActionLabel.setText("-");
etaLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
etaLabel.setText("estimating...");
progressBar.setToolTipText("");
progressBar.setValue(30);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(currentActionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
.addComponent(etaLabel)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(currentActionLabel)
.addComponent(etaLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel currentActionLabel;
private javax.swing.JLabel etaLabel;
private javax.swing.JProgressBar progressBar;
// End of variables declaration//GEN-END:variables
}