Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why wont the Button wrap?

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why wont the Button wrap?

    Hi,
    Working on an assignment and everything is going great until I notice that the Button is not wrapping. What am I missing that can help me move this thing down?

    Thanks,
    Taylor

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    public class KilowattApplet extends Applet implements ActionListener
    {
    //component constructor
    Label welcome = new Label("Welcome to the Appliance Energy Calculator");
    Label costKwhrLabel = new Label("Please enter the cost per kilowatt-hour in cents:");
    TextField costKwhrField = new TextField(5);
    Label hoursPerYearLabel = new Label("Please enter the kilowatt-hours consumed:");
    TextField hoursPerYearField = new TextField(5);
    Button calcButton = new Button("Calculate");
    Label outputLabel = new Label("Click the Calculate button to display the average energy cost.");

    public void init()
    {
    add(welcome);
    add(costKwhrLabel);
    add(costKwhrField);
    add(hoursPerYearLabel);
    add(hoursPerYearField);
    add(calcButton); // *This button prints on the same line as the previous line of code "add(hoursPerYearField)"*
    calcButton.addActionListener(this);
    add(outputLabel);
    }

    public void actionPerformed(ActionEvent e)
    {

    double costKwhr = Double.parseDouble(costKwhrField.getText());
    double kwHours = Double.parseDouble(hoursPerYearField.getText());
    double average = costKwhr*kwHours;
    outputLabel.setText("The average annual cost to operate this appliance is $" + Math.round(average*100)/100D);
    }
    }
    Last edited by Taylorleemusic; September 30th, 2010 at 07:31 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why wont the Button wrap?

    the Button is not wrapping
    Can you explain what what you mean by wrapping a Button?

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why wont the Button wrap?

    By wrapping I mean returning to the next line. I want the Button to display on the line UNDER the Label I have created not and the same line. I hope I explained well.
    Thanks,
    Taylor

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why wont the Button wrap?

    The position of components in a container in your GUI is controlled by the layout manager.
    Which layout manager are you using? It looks like the flow layout manager.
    If you want to align components other than having them flow left to right and wrap at the end, you need to read up on layout managers and see which one will format your components the way you want them to appear.

    You can get some changes by changing the width= attribute in the <APPLET tag.

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Why wont the Button wrap?

    You can find out more about the Layout managers Norm mentioned here:
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

Similar Threads

  1. Action button HELP?
    By utoptas in forum Java Theory & Questions
    Replies: 8
    Last Post: August 27th, 2010, 03:32 PM
  2. Compliing fine but wont run--please help!
    By Nova in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2010, 07:51 PM
  3. [SOLVED] Simple Grahpic porgram wont work.
    By kogo50 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2010, 12:46 PM
  4. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM
  5. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM