I'm working on a program that suppose to let a user enter a number and converts it to Celsius or Fahrenheit when you click on a Celsius button or Fahrenheit button respectively. So far I have created the GUI but I'm not sure how to write the rest of the code. Help me please.
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FtoCConversionTest{
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
ConvertorFrame frame = new ConvertorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class ConvertorFrame extends JFrame
{
public ConvertorFrame()
{
setTitle("Fehrenheit to Celsius converter");
ConvertorPanel panel = new ConvertorPanel();
add(panel);
pack();
}
}
class ConvertorPanel extends JPanel
{
public ConvertorPanel()
{
setLayout(new BorderLayout());
//add a text field and position it
final JTextField textField = new JTextField();
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(2, 2));
//add the text field label and position it
northPanel.add(new JLabel("Type the number to convert below: ", SwingConstants.LEFT));
northPanel.add(textField);
//position the panel
add(northPanel, BorderLayout.NORTH);
//add JPanel
panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
//add buttons and position them
addButton("Convert to Celsius", command);
addButton("Convert to Fehrenheit", command );
add(panel, BorderLayout.SOUTH);
}
private void addButton(String lable, ActionListener listener){
JButton button = new JButton(lable);
button.addActionListener(listener);
panel.add(button);
}
private JPanel panel;
private ActionListener command;
}

New Topic/Question
Reply



MultiQuote





|