Results 1 to 7 of 7
Thread: GUI program
- 04-23-2011, 10:35 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
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; } }
-
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.
Here's your math:Im having a hard time doing integer division of dividing inches by 12 and storing the answer in an integer called feet.
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;
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'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.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
Luck!
- 04-23-2011, 11:17 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
problem displaying a sentence containing the answer in a JLabel
I'm not getting it
Thanks for your help, just will hire a tutor
-
- 04-23-2011, 11:35 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
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;
-
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.
-
So in pseudocode:
That's it!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.
:)
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks