0

I'm working on an applet that, in the end, will display a table for interest in a bank account. I'm am trying to go about gathering input from the user, by displaying a series of prompts, followed by text input boxes, and finally a button to trigger the clear, calculate, and display parts. I would like the prompts and buttons left justified , one set per line (but they're long enough so only 1 fits per line anyways) the problem the keeps occurring, is that try as I may, they keep showing up centered, both in my browser, and in the basic IDE my school uses. I currently have them in panels, but that's not required by me, it was just my last try. Id prefer them gone if possible. Either way, I figure I'm likely missing something, because I'm a noob to java still, and that maybe someone can spot why they are staying on the left like I want. Thanks.

And here's the section of code that's relevant, there's some more for output late, but its not quite ready yet, and I don't think it matters for this question anyways.

import java.applet.*;   //imports req'd libraries
import java.awt.*;

public class AppletsExercise_2 extends Applet
{
Label promptDeposit, promptRate, promptStart, promptLast;
TextField inputDeposit, inputRate, inputStart, inputLast;
private Panel p1, p2, p3, p4;
int min = 1998;
int max = 2008;

public void init ()
{
    p1 =new Panel();                          //a bunch of nasty panels, my last attempt, but not at all preferred
    p1.setLayout (new FlowLayout (FlowLayout.LEFT,20,10));
    p2 =new Panel();
    p2.setLayout (new FlowLayout (FlowLayout.LEFT,20,10));p1 =new Panel();
    p3 = new Panel();
    p3.setLayout (new FlowLayout (FlowLayout.LEFT,20,10));
    p4 =new Panel();
    p4.setLayout (new FlowLayout (FlowLayout.LEFT,20,10));

    promptDeposit = new Label ("Enter the initial deposit: ");          //creates labels      for each of the fields
    promptRate = new Label ("Enter the interest rate, in percent: ");
    promptStart = new Label ("Enter the starting year: ");
    promptLast = new Label ("Enter the number of years: ");

    inputDeposit = new TextField (6);       //creates each of the fields
    inputRate = new TextField (2);
    inputStart = new TextField (4);
    inputLast = new TextField (4);

    Button calculate = new Button ("Calculate");

    p1.add(promptDeposit);      //adds a prompt for each piece of info, followed by a text field for the user
    p1.add(inputDeposit);
    p2.add(promptRate);
    p2.add(inputRate);
    p3.add(promptStart);
    p3.add(inputStart);
    p4.add(promptLast);
    p4.add(inputLast);
    p1.add (calculate);

    add("North", p1);   //adds the stupid panels, top down
    add("North", p2);
    add("North", p3);
    add("North", p4);
}

And finally, here's a picture of the current output, in Google Chrome https://i.stack.imgur.com/VGpov.jpg

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
qwerty22
  • 101
  • 2
  • 6
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Nov 12 '14 at 23:26
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). 2) Provide ASCII art, or a simple drawing, of the layout of the GUI. – Andrew Thompson Nov 12 '14 at 23:31
  • Why is it an applet? Because we are learning the basics of applets with yes, arguably useless examples. The reason for this example is that we did it in other languages, and it helps us by us having already done it in non-applet form. Why awt? Its supposed to be written as our somewhat dated textbook suggests, and for consistency we need to follow it, at least as were learning. And Andrew, thanks for the tip, I will look into that – qwerty22 Nov 14 '14 at 07:28
  • Thanks for your reply. I was serious about directing your instructor to that blog article. Anything that can be done in applet can be done better and easier in a `Frame` or (better) `JFrame`. As to using AWT, I'd bet that if you used the native PLAF in it, the instructor would not even notice the difference. And ..bonus, there would be a lot more people willing to, and capable of, giving pointers when you have trouble, Did Outman's suggestion help solve the problem? – Andrew Thompson Nov 14 '14 at 08:46
  • Unfortunately it did not, and seeing as it was just for looks, and not a firm requirement of the example, I just used it as is, in spite of my disliking for it. All 3 of the methods, including Outman's should have worked from my research, so Im unsure of why they all had the same, and seemingly incorrect result. – qwerty22 Nov 17 '14 at 00:38

1 Answers1

0

Maybe you should specify the layout strategy in your init method as follows:

this.setLayout(new GridLayout(4, 1));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
outman
  • 1