PDA

View Full Version : CS Help, Pretty Simple



bullet2urbrain
05-03-2006, 10:42 AM
Hey all,

Just want to know the answer to a very simple Java question.

I have to use JPanels and crap like that to have the user input a Int and then convert that Int into Feet and Inches within a JPanel,

Where do i do the math in this step? whenever i try and do the mathematical operators i get errors saying you cant do that within Javax.swing

here's my code if anyone wants to take a look @ it.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* NamePanel shows use of a JTextField to read in a String.
* It contains a private listener class.
*
* @author James Mikolich
* @version 5/1/06
* Modified from William Austad
* SectionA02
*
*/
public class NumberPanel extends JPanel
{
// constants known through all of the class
private final int PANEL_HEIGHT = 750;
private final int PANEL_WIDTH = 350;
private final Color BACK_COLOR = Color.BLUE;

// the text field when the user types input
private JTextField text;

// labels used to display messages
private JLabel input, inches, num;

/**
* Constructor for NamePanel initializes the text field
* several labels and such.
*/
public NumberPanel()
{
// initialize text area
text = new JTextField(10);

// hook up a listener
text.addActionListener(new TextListener());

// initialize the labels
input = new JLabel("Enter A height in Inches");
inches = new JLabel(input/12 + "feet");This is where the problem is
num = new JLabel("");

// add components to this panel
add(input);
add(text);
add(inches);
add(num);

// set the size
setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));

// set the color
setBackground(BACK_COLOR);
} // of constructor

// This internal listener has access to any variables declared
// outside of the main. This is quite convenient.
private class TextListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String foot = text.getText();
String inches = text.getText();
inches.setText("You entered: ");
num.setText(Totalnum);
} // of actionPerformed
} // of class TextListener
} // of class NamePanel

bullet2urbrain
05-03-2006, 10:47 AM
import javax.swing.*;
public class NameDropper
{
public static void main(String [] args)
{
// create a frame
JFrame frame = new JFrame("Name Dropper");

// end the program when the frame is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

// create a NamePanel
NumberPanel panel = new NumberPanel();

// add the panel to the frame
frame.getContentPane().add(panel);

// set up size of frame
frame.pack();

// make it visible
frame.setVisible(true);
} // of main
} // of class

Heretic
05-03-2006, 09:28 PM
There are quite a few problems in that code.

First, in the actionPerformed method, you're redeclaring your JLabel "inches" as a String ( 'String inches= text.getText()' ). Second, you're trying to divide a JLabel ('input') by 12, that's why it's throwing that error.

You want to put all your action code in the actionPerformed. If you want it to convert feet to inches, put that code in your actionPerformed like so:


public void actionPerformed(ActionEvent e)
{
//get the input and parse as int
int feet = Integer.parseInt(text.getText());
//set the inches label with the value of feet in inches
inches.setText(feet + " feet = " + (feet*12) + " inches.");
}

If you want to convert inches to feet, take into consideration that integer division will leave off the decimal portion, so be sure to use double/float values.

Also, the blue background is quite painful to my eyes. :p:

bullet2urbrain
05-04-2006, 06:20 AM
ZOMG, wow, thanks a whole bunch I havent tried what you've suggested. but the sheer fact that someone can explain why the JLabel division wouldnt work in English is awesome.

so i must use the parseInt method...

ahh thanks so much, i'll report back if i have any further problems.

bullet2urbrain
05-04-2006, 06:50 AM
Well modifying it slightly yields the results i need, I will post it here.

that was AWESOME, thank you so much.



public void actionPerformed(ActionEvent event)
{
int inch = Integer.parseInt(text.getText());
inches.setText((inch/12) + " feet " + (inch%12) + " inches.");
}

PS: i changed the color to Orange :p: