Results 1 to 7 of 7

Thread: GUI program

  1. #1
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default GUI program

    I want my boss to be able to enter inches and the computer calculate feet and inches from the inches that are inputted.
    I also want to display a sentence containing the answer in a Label
    Im having a hard time doing integer division of dividing inches by 12 and storing the answer in an integer called feet.
    I also need help concatenating the sentence that displays the answer of how many inches are in a foot when the user inputs the inches

    Can anyone assist please

    Here's what I have so far

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class InchFeetGUI extends JFrame implements ActionListener {
    
        private JLabel inchesQuestionLabel = new JLabel();
        private JTextField inchesBox = new JTextField();
        private JLabel inchesAnswerLabel = new JLabel();
    
        ;
        private Container contentPane;
        private JButton submitButton = new JButton();
    
        public InchFeetGUI() {
            createGUI();
        }// ends Welcome constructor
    
        private void createGUI() {
            //get content pane and set the layout to null
    
            contentPane = getContentPane();
            contentPane.setBackground(Color.white);
            contentPane.setLayout(null);
    
            //setup the question label
            inchesQuestionLabel.setText("Enter inches: ");
            inchesQuestionLabel.setLocation(0, 0);
            inchesQuestionLabel.setSize(210, 50);
            contentPane.add(inchesQuestionLabel);
    
            //set up the textfield for user entry
            inchesBox.setText("0");
            inchesBox.setLocation(210, 0);
            inchesBox.setSize(100, 30);
            contentPane.add(inchesBox);
            inchesBox.addActionListener(this);
    
            //set up the submit button
            submitButton.setText("Submit");
            submitButton.setLocation(50, 100);
            submitButton.setSize(100, 30);
            contentPane.add(submitButton);
            submitButton.addActionListener(this);
    
            //set up the label where answer will be
    
            inchesAnswerLabel.setText(" ");
            inchesAnswerLabel.setLocation(0, 200);
            inchesAnswerLabel.setSize(300, 30);
            contentPane.add(inchesAnswerLabel);
    
    
            //set properties of Window
    
            setTitle("Inches/Feet Converter");
            setSize(400, 400);
            setVisible(true);
    
        }//ends createGUI method
    
        public static void main(String[] args) {
            InchFeetGUI application = new InchFeetGUI();
            application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
        }// ends main
    
        public void actionPerformed (ActionEvent  event ){
            // statements get executed here when button is pressed.
            int inches = Integer.parseInt (inchesBox.getText());
            int feet = inches/12;
            int inchesLeft = inches %12;
            
            
    
    
            
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by program junkie View Post
    I want my boss to be able to enter inches and the computer calculate feet and inches from the inches that are inputted.
    First off let's be open and honest as that is the only way we can fully help you -- this isn't for "your boss" as it's clearly an academic assignment as have all your other threads in this forum.

    That out of the way, I'm also going to ask you to use informative thread titles for your questions in this forum as the title will help those of us scanning the threads to know in a glance what your main problem is. Here I'd say something like "Help with displaying GUI program output", or something along those lines.

    Now on to your code and your problem.

    Im having a hard time doing integer division of dividing inches by 12 and storing the answer in an integer called feet.
    Here's your math:

    Java Code:
       public void actionPerformed(ActionEvent event) {
          int inches = Integer.parseInt(inchesBox.getText());
          int feet = inches / 12;
          int inchesLeft = inches % 12;
    and it looks just fine to me when I print it out:

    Java Code:
       public void actionPerformed(ActionEvent event) {
          int inches = Integer.parseInt(inchesBox.getText());
          int feet = inches / 12;
          int inchesLeft = inches % 12;
          
          System.out.println("feet: " + feet + ", and inches left: " + inchesLeft);
       }

    I also need help concatenating the sentence that displays the answer of how many inches are in a foot when the user inputs the inches
    I'll bet you should have no problem with that. Simply create a String with String literals such as "feet: " then a + or concatanation operator then a variable then a concatenation operator, etc... And set your JLabel's text to that String. Give it a try and I'll bet you'll get it on the first shot.

    Luck!

  3. #3
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default problem displaying a sentence containing the answer in a JLabel

    I'm not getting it

    Thanks for your help, just will hire a tutor

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by program junkie View Post
    I'm not getting it
    What is confusing you?


    Thanks for your help, just will hire a tutor
    Whatever helps you is good, though I still think that you're very close.

  5. #5
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default Response to Fubarable's Post

    Fubarable, Im not understanding how I need to go about displaying a sentence containing the answer in a label.

    This is what I tried:

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

    public class InchFeetGUI extends JFrame implements ActionListener {

    private JLabel inchesQuestionLabel = new JLabel();
    private JTextField inchesBox = new JTextField();
    private JLabel inchesAnswerLabel = new JLabel();

    ;
    private Container contentPane;
    private JButton submitButton = new JButton();

    public InchFeetGUI() {
    createGUI();
    }// ends Welcome constructor

    private void createGUI() {
    //get content pane and set the layout to null

    contentPane = getContentPane();
    contentPane.setBackground(Color.yellow);
    contentPane.setLayout(null);

    //setup the question label
    inchesQuestionLabel.setText("Enter inches: ");
    inchesQuestionLabel.setLocation(10, 0);
    inchesQuestionLabel.setSize(210, 50);
    contentPane.add(inchesQuestionLabel);

    //set up the textfield for user entry
    inchesBox.setText("0");
    inchesBox.setLocation(220, 0);
    inchesBox.setSize(100, 30);
    contentPane.add(inchesBox);
    inchesBox.addActionListener(this);

    //set up the submit button
    submitButton.setText("Submit");
    submitButton.setLocation(50, 100);
    submitButton.setSize(100, 30);
    contentPane.add(submitButton);
    submitButton.addActionListener(this);

    //set up the label where answer will be

    inchesAnswerLabel.setText("feet: " + feet + ", and inches left: " + inchesLeft ");
    inchesAnswerLabel.setLocation(0, 200);
    inchesAnswerLabel.setSize(300, 30);
    contentPane.add(inchesAnswerLabel);


    //set properties of Window

    setTitle("Inches/Feet Converter");
    setSize(400, 400);
    setVisible(true);

    }//ends createGUI method

    public static void main(String[] args) {
    InchFeetGUI application = new InchFeetGUI();
    application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    }// ends main

    public void actionPerformed (ActionEvent event ){
    // statements get executed here when button is pressed.




    int inches = Integer.parseInt (inchesBox.getText());
    int feet = inches/12;
    int inchesLeft = inches %12;

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You'll want to declare a String variable and then concatenate the text into this variable (hint, shoot I've already done something similar in my println statement above -- I thought you'd have already seen it and studied it), and then call setText(myStringVariable) on your JLabel.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    So in pseudocode:

    Java Code:
    First calculate feet and left over inches -- you've done that.
    Create String variable and set = "some text " + feetVariable + "some other test" + leftOverInchesVariable;
    call setText on JLabel and pass in the String variable created above.
    That's it!
    :)

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •