flaherty36
03-24-2008, 12:52 AM
This is for a class assignment.
I am finishing up an assignment that requires data input checking. I have a JtextField that the user is supposed to input his weight in puonds. I need to code something that would allow me to use a JOptionPane to show if the user inputs anything other than an interger. Here is my code so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Earth extends JFrame
{
private ImagePanel imagePanel;
JLabel label = new JLabel();
JLabel textLabel = new JLabel("What planet do you want your weight on?");
JLabel fatLabel = new JLabel("Enter your weight on Earth: ");
JTextField fatField = new JTextField(25);
JButton button = new JButton("ENTER");
JComboBox comboBox = new JComboBox();
//JScrollPane scrollingArea = new JScrollPane(logArea);
JTextArea logArea = new JTextArea(10, 20);
private String names[] =
{ "Mercury.jpg", "Venus.jpg",
"Earth.jpg", "Mars.jpg", "Jupiter.jpg", "Saturn.jpg", "Uranus.jpg", "Neptune.jpg", "Pluto.jpg" };
private Icon icons[] =
{
new ImageIcon( names[ 0 ] ),
new ImageIcon( names[ 1 ] ),
new ImageIcon( names[ 2 ] ),
new ImageIcon( names[ 3 ] ),
new ImageIcon( names[ 4 ] ),
new ImageIcon( names[ 5 ] ),
new ImageIcon( names[ 6 ] ),
new ImageIcon( names[ 7 ] ),
new ImageIcon( names[ 8 ] )};
public Earth(String title)
{
super(title);
Container c = getContentPane();
c.setLayout(new BorderLayout(10,20));
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
centerPanel.setLayout(new GridLayout(12,12,1,1));
southPanel.setLayout(new BorderLayout());
southPanel.add(label, "North");
southPanel.add(logArea, "South");
logArea.setEditable ( false );
logArea.setText( "Start here " );
JScrollPane scrollingArea = new JScrollPane(logArea);
centerPanel.add(fatLabel);
centerPanel.add(fatField);
centerPanel.add(button);
centerPanel.add( textLabel);
//fatField.setText (150);
button.setBackground(Color.orange);
comboBox.addItem("Mercury");
comboBox.addItem("Venus");
comboBox.addItem("Earth");
comboBox.addItem("Mars");
comboBox.addItem("Saturn");
comboBox.addItem("Uranus");
comboBox.addItem("Jupiter");
comboBox.addItem("Neptune");
comboBox.addItem("Pluto");
centerPanel.add(comboBox);
label.setIcon(
icons[2] );
comboBox.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double weight = 0;
label.setIcon(
icons[ comboBox.getSelectedIndex() ] );
String selectedItem = (String) comboBox.getSelectedItem();
String text = logArea.getText();
//if (fatField == (null) ){
// JOptionPane.showMessageDialog(null, "Please enter your weight on Earth.", "Stupid Alert!", JOptionPane.ERROR_MESSAGE);
//}
if (selectedItem=="Mercury"){
weight = Integer.parseInt(fatField.getText()) * 0.38;
}
else if (selectedItem=="Venus"){
weight = Integer.parseInt(fatField.getText()) * 0.91;
//}
//else if (selectedItem=="Earth"){
// weight = Integer.parseInt(fatField.getText());
//text = "You already know what you weight on Earth!";
}
else if (selectedItem=="Mars"){
weight = Integer.parseInt(fatField.getText()) * 0.38;
}
else if (selectedItem=="Jupiter"){
weight = Integer.parseInt(fatField.getText()) * 2.54;
}
else if (selectedItem=="Saturn"){
weight = Integer.parseInt(fatField.getText()) * 0.93;
}
else if (selectedItem=="Uranus"){
weight = Integer.parseInt(fatField.getText()) * 0.8;
}
else if (selectedItem=="Neptune"){
weight = Integer.parseInt(fatField.getText()) * 1.2;
}
else if (selectedItem=="Pluto"){
JOptionPane.showMessageDialog(null, "There is not enough known about Pluto to know your answer.", "Danger Will Robinson!", JOptionPane.ERROR_MESSAGE);
}
//if (weight <= 0 ){
// JOptionPane.showMessageDialog(null, "Please enter your weight on Earth.", "Stupid Alert!", JOptionPane.ERROR_MESSAGE);
//}
text = text+ "Your weight on "+selectedItem + " is: "+ Double.toString(weight)+ " \n";
logArea.setText(text);
}
}
);
comboBox.setSelectedIndex(2);
//JTextField.setText( );
southPanel.add(logArea);
c.add(centerPanel, BorderLayout.CENTER);
c.add(southPanel, BorderLayout.SOUTH);
logArea.add(scrollingArea);
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text = logArea.getText();
text = text+
fatField.getText()+" is what you say you weight on Earth.\n" ;
logArea.setText(text);
}
}
);
}
// Main
public static void main(String[] args)
{
Earth g2 = new Earth("Earth/Planet weight converter");
g2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g2.setSize(700,700);
g2.setVisible(true);
}
}
Any guidance would be appreciated.
Thank you
I am finishing up an assignment that requires data input checking. I have a JtextField that the user is supposed to input his weight in puonds. I need to code something that would allow me to use a JOptionPane to show if the user inputs anything other than an interger. Here is my code so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Earth extends JFrame
{
private ImagePanel imagePanel;
JLabel label = new JLabel();
JLabel textLabel = new JLabel("What planet do you want your weight on?");
JLabel fatLabel = new JLabel("Enter your weight on Earth: ");
JTextField fatField = new JTextField(25);
JButton button = new JButton("ENTER");
JComboBox comboBox = new JComboBox();
//JScrollPane scrollingArea = new JScrollPane(logArea);
JTextArea logArea = new JTextArea(10, 20);
private String names[] =
{ "Mercury.jpg", "Venus.jpg",
"Earth.jpg", "Mars.jpg", "Jupiter.jpg", "Saturn.jpg", "Uranus.jpg", "Neptune.jpg", "Pluto.jpg" };
private Icon icons[] =
{
new ImageIcon( names[ 0 ] ),
new ImageIcon( names[ 1 ] ),
new ImageIcon( names[ 2 ] ),
new ImageIcon( names[ 3 ] ),
new ImageIcon( names[ 4 ] ),
new ImageIcon( names[ 5 ] ),
new ImageIcon( names[ 6 ] ),
new ImageIcon( names[ 7 ] ),
new ImageIcon( names[ 8 ] )};
public Earth(String title)
{
super(title);
Container c = getContentPane();
c.setLayout(new BorderLayout(10,20));
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
centerPanel.setLayout(new GridLayout(12,12,1,1));
southPanel.setLayout(new BorderLayout());
southPanel.add(label, "North");
southPanel.add(logArea, "South");
logArea.setEditable ( false );
logArea.setText( "Start here " );
JScrollPane scrollingArea = new JScrollPane(logArea);
centerPanel.add(fatLabel);
centerPanel.add(fatField);
centerPanel.add(button);
centerPanel.add( textLabel);
//fatField.setText (150);
button.setBackground(Color.orange);
comboBox.addItem("Mercury");
comboBox.addItem("Venus");
comboBox.addItem("Earth");
comboBox.addItem("Mars");
comboBox.addItem("Saturn");
comboBox.addItem("Uranus");
comboBox.addItem("Jupiter");
comboBox.addItem("Neptune");
comboBox.addItem("Pluto");
centerPanel.add(comboBox);
label.setIcon(
icons[2] );
comboBox.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double weight = 0;
label.setIcon(
icons[ comboBox.getSelectedIndex() ] );
String selectedItem = (String) comboBox.getSelectedItem();
String text = logArea.getText();
//if (fatField == (null) ){
// JOptionPane.showMessageDialog(null, "Please enter your weight on Earth.", "Stupid Alert!", JOptionPane.ERROR_MESSAGE);
//}
if (selectedItem=="Mercury"){
weight = Integer.parseInt(fatField.getText()) * 0.38;
}
else if (selectedItem=="Venus"){
weight = Integer.parseInt(fatField.getText()) * 0.91;
//}
//else if (selectedItem=="Earth"){
// weight = Integer.parseInt(fatField.getText());
//text = "You already know what you weight on Earth!";
}
else if (selectedItem=="Mars"){
weight = Integer.parseInt(fatField.getText()) * 0.38;
}
else if (selectedItem=="Jupiter"){
weight = Integer.parseInt(fatField.getText()) * 2.54;
}
else if (selectedItem=="Saturn"){
weight = Integer.parseInt(fatField.getText()) * 0.93;
}
else if (selectedItem=="Uranus"){
weight = Integer.parseInt(fatField.getText()) * 0.8;
}
else if (selectedItem=="Neptune"){
weight = Integer.parseInt(fatField.getText()) * 1.2;
}
else if (selectedItem=="Pluto"){
JOptionPane.showMessageDialog(null, "There is not enough known about Pluto to know your answer.", "Danger Will Robinson!", JOptionPane.ERROR_MESSAGE);
}
//if (weight <= 0 ){
// JOptionPane.showMessageDialog(null, "Please enter your weight on Earth.", "Stupid Alert!", JOptionPane.ERROR_MESSAGE);
//}
text = text+ "Your weight on "+selectedItem + " is: "+ Double.toString(weight)+ " \n";
logArea.setText(text);
}
}
);
comboBox.setSelectedIndex(2);
//JTextField.setText( );
southPanel.add(logArea);
c.add(centerPanel, BorderLayout.CENTER);
c.add(southPanel, BorderLayout.SOUTH);
logArea.add(scrollingArea);
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text = logArea.getText();
text = text+
fatField.getText()+" is what you say you weight on Earth.\n" ;
logArea.setText(text);
}
}
);
}
// Main
public static void main(String[] args)
{
Earth g2 = new Earth("Earth/Planet weight converter");
g2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g2.setSize(700,700);
g2.setVisible(true);
}
}
Any guidance would be appreciated.
Thank you