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 4 of 4

Thread: How to fix the places

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to fix the places

    Hello idols , im IT student beginner.

    can anyone help me to make this applet organize like this : but wait first this is my code

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


    public class Calculator extends Applet implements ActionListener {

    Button btnAdd, btnSubtract, btnMultiply, btnDivide;
    Label lblFirstDigit, lblSecondDigit, lblAnswer;
    TextField txtFirstDigit, txtSecondDigit, txtAnswer;


    public void init() {

    lblFirstDigit = new Label("Enter the first integer: ");
    txtFirstDigit = new TextField(10);
    lblSecondDigit = new Label("Enter the second integer:");
    txtSecondDigit = new TextField(10);
    btnAdd = new Button("+");
    btnSubtract = new Button("-");
    btnMultiply = new Button("*");
    btnDivide = new Button("/");
    lblAnswer = new Label("Answer: ");
    txtAnswer = new TextField(10);


    add(lblFirstDigit);
    add(txtFirstDigit);
    add(lblSecondDigit);
    add(txtSecondDigit);
    add(btnAdd);
    add(btnSubtract);
    add(btnMultiply);
    add(btnDivide);
    add(lblAnswer);
    add(txtAnswer);

    btnAdd.addActionListener(this);
    btnMultiply.addActionListener(this);
    btnSubtract.addActionListener(this);
    btnDivide.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) {

    double firstDigit = Double.parseDouble(txtFirstDigit.getText());
    double secondDigit = Double.parseDouble(txtSecondDigit.getText());

    if (e.getSource() == btnAdd)
    txtAnswer.setText(Double.toString(firstDigit+secon dDigit));

    if (e.getSource() == btnSubtract)
    txtAnswer.setText(Double.toString(firstDigit-secondDigit));

    if (e.getSource() == btnMultiply)
    txtAnswer.setText(Double.toString(firstDigit*secon dDigit));

    if (e.getSource() == btnDivide)
    txtAnswer.setText(Double.toString(firstDigit/secondDigit));



    }
    }



    i cant do it like this :
    541639_485416044858934_940158186_n.jpg



    this is my output :

    rgi.jpg


  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: How to fix the places

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Can you explain what you are trying to do? The images are hard to see.
    Is your problem with laying out the components in the GUI?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to fix the places

    import javax.swing.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class Calculator extends Applet implements ActionListener {
     
    Button btnAdd, btnSubtract, btnMultiply, btnDivide;
    Label lblFirstDigit, lblSecondDigit, lblAnswer;
    TextField txtFirstDigit, txtSecondDigit, txtAnswer;
     
     
    public void init() {
     
    lblFirstDigit = new Label("Enter the first integer: ");
    txtFirstDigit = new TextField(10);
    lblSecondDigit = new Label("Enter the second integer:");
    txtSecondDigit = new TextField(10);
    btnAdd = new Button("+");
    btnSubtract = new Button("-");
    btnMultiply = new Button("*");
    btnDivide = new Button("/");
    lblAnswer = new Label("Answer: ");
    txtAnswer = new TextField(10);
     
     
    add(lblFirstDigit);
    add(txtFirstDigit);
    add(lblSecondDigit);
    add(txtSecondDigit);
    add(btnAdd);
    add(btnSubtract);
    add(btnMultiply);
    add(btnDivide);
    add(lblAnswer);
    add(txtAnswer);
     
    btnAdd.addActionListener(this);
    btnMultiply.addActionListener(this);
    btnSubtract.addActionListener(this);
    btnDivide.addActionListener(this);
     
    }
     
    public void actionPerformed(ActionEvent e) {
     
    double firstDigit = Double.parseDouble(txtFirstDigit.getText());
    double secondDigit = Double.parseDouble(txtSecondDigit.getText());
     
    if (e.getSource() == btnAdd)
    txtAnswer.setText(Double.toString(firstDigit+secon dDigit));
     
    if (e.getSource() == btnSubtract)
    txtAnswer.setText(Double.toString(firstDigit-secondDigit));
     
    if (e.getSource() == btnMultiply)
    txtAnswer.setText(Double.toString(firstDigit*secon dDigit));
     
    if (e.getSource() == btnDivide)
    txtAnswer.setText(Double.toString(firstDigit/secondDigit));
     
     
     
    }
    }




    i want my output to be like thiss


    Enter the first integer: ---TEXTFIELD----
    Enter the second integer: ---TEXTFIELD----
    + - * /

    Answer: ---TEXTFIELD----



    because my current layout is like this


    Enter the first integer: ---TEXTFIELD----
    Enter the second integer: ---TEXTFIELD----
    + - * / Answer: ---TEXTFIELD----

  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: How to fix the places

    What do you mean by "output"?
    Are you talking about changing the layout of components on the GUI?

    See the tutorial:
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How do I get my answers to out with 2 decimal places?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 8th, 2012, 12:23 PM
  2. New to Java Need 2 decimal places, please :). Here is my code
    By Charyl in forum Member Introductions
    Replies: 2
    Last Post: June 28th, 2011, 03:06 AM
  3. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  4. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 11:27 AM
  5. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM