Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Netbeans earned a reputation with me years ago for creating convoluted GUI code. Never really tested it for myself, but this seems to be a lot of code for so simple an application.</p> <p>When converting a console based app to a GUI based one, you must ask yourself this question: How does the user interact with the system?</p> <p>In your case, your console app interacted with the user by:</p> <ol> <li>Outputting a prompt for input</li> <li>Accepting input</li> </ol> <p>Now, you must address how to change these two interactions to a GUI layout. It should involve very minimal modification to your existing code, while adding GUI code to handle the input/output.</p> <p>In your case, you have a label that acts as the prompt, a textarea that houses the input, and a button that acts as the acceptance of the input. </p> <p>So the flow of your program is this: User types into textarea -> User hits submit -> System accepts input and processes it -> System outputs change in terms of Quarters, Dimes, etc.</p> <p>Now, we code it...</p> <p>Step 1: Set up GUI mechanisms. You have that down</p> <p>Step 2: Process user input: You do this in your handler for the Calculate button, which in your case appears to be jButton1ActionPerformed</p> <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // Initialize varriables int quarters = 25; int dimes = 10; int nickles = 5; int pennies = 1; try { int input = Integer.parseInt(jTextField1.getText()); } catch (NumberFormatException e) { //prompt user to enter an integer, not erroneous input //e.printStackTrace(); } //future code } </code></pre> <p>Note how input has the same function as it did in your console app, we just get it by different means. Since a GUI system is driven by user input, we should not loop continuously until the user enters valid input, we simply tell the user that their input is invalid, then let them input a valid number. We check to see if the number is valid (input between 1 and 99) and do processing, otherwise do nothing (and prompt for valid input?)</p> <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // Initialize varriables int quarters = 25; int dimes = 10; int nickles = 5; int pennies = 1; try { int input = Integer.parseInt(jTextField1.getText()); } catch (NumberFormatException e) { //prompt user to enter an integer, not erroneous input //e.printStackTrace(); } //if input in the range of [1, 99] if(1 &lt;= input &amp;&amp; input &lt;= 99) { //do processing } else { //prompt user for valid input, for example by using JOptionPane } } </code></pre> <p>Now: Output. Instead of doing System.out.println on A, B, C, and D, you just need to set the text of your JComponents that represent quarter, dime, etc amounts. At the moment you seem to be missing labels/textareas for your totals, so I'd add those first, then use setText(A), setText(B), etc for those.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload