hello.
i, a neophyte, have completed a gui using netbeans. how do i apply loops (i.e. while statements) within, or to the entire gui? i am trying to program it to go through fifty cycles, then print the elapsed time.the interface wont accept any application of while statements that encompass what i see as the critical parts of code. here is the source. thank you.
CODE
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class CelsiusConverterGUI extends javax.swing.JFrame {
long start = System.currentTimeMillis();
int problemCount = 1;
int rndma = new Random().nextInt(12) + 1;
int rndmb = new Random().nextInt(12) + 1;
public CelsiusConverterGUI() {
initComponents();
problemField.setText(" " + rndma + "x" + rndmb);
answerField.requestFocusInWindow();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
convertButton = new javax.swing.JButton();
fahrenheitLabel = new javax.swing.JLabel();
equalSign = new javax.swing.JLabel();
problemField = new javax.swing.JTextField();
answerField = new javax.swing.JFormattedTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Converter");
convertButton.setText("Submit");
convertButton.setBorder(null);
convertButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButtonActionPerformed(evt);
}
});
equalSign.setText("=");
problemField.setEditable(false);
problemField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
problemFieldActionPerformed(evt);
}
});
answerField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
answerField.setAutoscrolls(false);
answerField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
ENTER(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(fahrenheitLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(problemField, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(equalSign)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(answerField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(convertButton)))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {answerField, convertButton, problemField});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(problemField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(equalSign)
.addComponent(answerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(convertButton)
.addGap(15, 15, 15)
.addComponent(fahrenheitLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {answerField, convertButton});
pack();
}// </editor-fold>
private void problemFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Parse degrees Celsius as a double and convert to Fahrenheit.
// TODO add your handling code here:
}
private void ENTER(java.awt.event.KeyEvent evt) {
int answerFieldinterger2 = (int)((Double.parseDouble(answerField.getText())));
int solution = rndma * rndmb;
boolean correctAns2 = solution == answerFieldinterger2;
if(correctAns2){fahrenheitLabel.setText("Correct!");
answerField.setText("");
problemCount ++;
} else { long elapsed = System.currentTimeMillis() - start;
fahrenheitLabel.setText(" " + elapsed/1000f + "seconds");}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CelsiusConverterGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JFormattedTextField answerField;
private javax.swing.JButton convertButton;
private javax.swing.JLabel equalSign;
private javax.swing.JLabel fahrenheitLabel;
private javax.swing.JTextField problemField;
// End of variables declaration
}