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