6 Replies - 6638 Views - Last Post: 20 August 2008 - 07:15 PM Rate Topic: -----

#1 rwilliamj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-August 08

got gui. now how to loop it?

Post icon  Posted 15 August 2008 - 05:14 PM

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.

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

}



Is This A Good Question/Topic? 0
  • +

Replies To: got gui. now how to loop it?

#2 thenovices  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 19-January 08

Re: got gui. now how to loop it?

Posted 16 August 2008 - 07:31 PM

what do you mean by "loop the GUI"? What are you trying to accomplish?

This post has been edited by thenovices: 16 August 2008 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: got gui. now how to loop it?

Posted 16 August 2008 - 07:52 PM

View Postrwilliamj, on 15 Aug, 2008 - 05:14 PM, said:

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.


fifty cycles of what ?
Was This Post Helpful? 0
  • +
  • -

#4 rwilliamj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-August 08

Re: got gui. now how to loop it?

Posted 17 August 2008 - 11:30 AM

i want to give the student 50 questions--something that would be like

int count = 1
while count <51 {blah blah gui}
count ++


textFeld.setText ("all done")
textFild.setText (elapsed time + "")
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: got gui. now how to loop it?

Posted 17 August 2008 - 01:06 PM

You don't loop though the GUI
You have the GUI to react to and event (like a button push)

Here a quick example

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class SimpleQuestionAnswer extends JFrame implements ActionListener {

	String[] question = {"Question 1", "Question 2", "Question 3"}; 
	JLabel qLabel;
	JTextField answer;
	JButton ok;
	int count = 0;
	
	SimpleQuestionAnswer() {
		super("FirstGui");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JPanel p = new JPanel(new GridLayout(1,2));
		p.add(new JLabel(" The question is: "));
		qLabel = new JLabel(question[count]);
		p.add(qLabel);
		add(p, BorderLayout.NORTH);
		
		p = new JPanel(new GridLayout(1,2));
		p.add(new JLabel("Enter your answer here: "));
		answer = new JTextField("");
		p.add(answer);
		add(p, BorderLayout.CENTER);
		
		ok = new JButton("OK");
		ok.addActionListener(this);
		add(ok, BorderLayout.SOUTH);
		
		setSize(300, 100);
		setVisible(true);
		
	}

	// invoked when the user click the button
	public void actionPerformed(ActionEvent arg0) {
		// check the answer
		// ....
		
		count++;
		if(count >= question.length) {
			qLabel.setText(" you're done");
			ok.setEnabled(false);
			return;
		}
		qLabel.setText(question[count]);
		answer.setText("");
	}
	
	
	public static void main(String[] arg) {
		new SimpleQuestionAnswer();
	}
	
}


Was This Post Helpful? 1
  • +
  • -

#6 rwilliamj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-August 08

Re: got gui. now how to loop it?

Posted 20 August 2008 - 05:55 PM

thank you so much!!
arrays were the way to go.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: got gui. now how to loop it?

Posted 20 August 2008 - 07:15 PM

View Postrwilliamj, on 20 Aug, 2008 - 05:55 PM, said:

thank you so much!!
arrays were the way to go.

Array or not... the important point is:
You don't drive a GUI !
GUI are event driven
the User must make your GUI react
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1