• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with GUI

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
}
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// resultLabel.setText (Double.toString (miles));
resultLabel.setText (dec.format(miles));
reply
    Bookmark Topic Watch Topic
  • New Topic