• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with GUI

 
Mike Brooks
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit. got it working, but I need help on how to format the result to two decimal places.

---------------------------------------------------------------------
// Driver class

import javax.swing.JFrame;

public class Kilometers
{
public static void main (String[] args)
{

JFrame frame = new JFrame ("Kilometers");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

KilometersPanel panel = new KilometersPanel();

frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
-------------------------------------------------------------------------

// Panel to set up GUI

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class FahrenheitPanelN extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField kilometers;
private JButton work;

//-----------------------------------------------------------------
// Constructor: Sets up the main GUI components.
//-----------------------------------------------------------------
public FahrenheitPanelN()
{
inputLabel = new JLabel ("Enter number of kilometers");
outputLabel = new JLabel ("Numbers of miles:");
resultLabel = new JLabel ();

work = new JButton("Convert");
work.addActionListener(new DoListener());

kilometers = new JTextField (5);
kilometers.addActionListener (new DoListener());

add (inputLabel);
add (kilometers);
add (work);
add (outputLabel);
add (resultLabel);

setPreferredSize (new Dimension(400, 75));
setBackground (Color.yellow);
}


private class DoListener implements ActionListener
{

public void actionPerformed (ActionEvent event)
{
double miles, kilometer;

DecimalFormat dec = new DecimalFormat("0.00");

String text = kilometers.getText();

kilometer = Integer.parseInt (text);
miles = kilometer * .6214;

resultLabel.setText (Double.toString (miles));
}
}
}
[ April 07, 2006: Message edited by: Jon Martin ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best place to find problems is with the error messages, which you failed to show us. <hint hint>
 
Mike Brooks
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could read "Moby dick" before you got through reading all the error messages =)

Anyways, I figured it out, but I want to format the result to two deciaml places which I can't figure out. Help..

updated code....

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class FahrenheitPanelN extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField kilometers;
private JButton work;

//-----------------------------------------------------------------
// Constructor: Sets up the main GUI components.
//-----------------------------------------------------------------
public FahrenheitPanelN()
{
inputLabel = new JLabel ("Enter number of kilometers");
outputLabel = new JLabel ("Numbers of miles:");
resultLabel = new JLabel ();

work = new JButton("Convert");
work.addActionListener(new DoListener());

kilometers = new JTextField (5);
kilometers.addActionListener (new DoListener());

add (inputLabel);
add (kilometers);
add (work);
add (outputLabel);
add (resultLabel);

setPreferredSize (new Dimension(400, 75));
setBackground (Color.yellow);
}


private class DoListener implements ActionListener
{
//--------------------------------------------------------------
// Performs the conversion when the enter key is pressed in
// the text field.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
double miles, kilometer;

DecimalFormat dec = new DecimalFormat("0.00");

String text = kilometers.getText();

kilometer = Integer.parseInt (text);
miles = kilometer * .6214;

resultLabel.setText (Double.toString (miles));
}
}
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// resultLabel.setText (Double.toString (miles));
resultLabel.setText (dec.format(miles));
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!