Question:
Create a celsius to fahrenheit calculator using a JSlider.
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class CelsiusToFahrenheit { public static void main(String[] arguments) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame f = new JFrame("Celsius To Fahrenheit"); f.setSize(400,200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel container = new JPanel(); container.setBorder(BorderFactory.createEmptyBorder(0,20,0,20)); container.setLayout(new GridLayout(3, 1)); JPanel results = new JPanel(); results.setLayout(new FlowLayout()); JLabel cLabel = new JLabel("Celsius: "); final JTextField cText = new JTextField(5); JLabel fLabel = new JLabel("Fahrenheit: "); final JTextField fText = new JTextField(5); results.add(cLabel); results.add(cText); results.add(fLabel); results.add(fText); JLabel sLabel = new JLabel("Celsius Slider",JLabel.CENTER); final JSlider slider = new JSlider(JSlider.HORIZONTAL,0,100,10); slider.setMinorTickSpacing(5); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int c = slider.getValue(); fText.setText("" + (c * (double) 9 / 5 + 32) ); cText.setText("" + c); } }); // set default value fText.setText("" + (slider.getValue() * (double) 9 / 5 + 32) ); cText.setText("" + slider.getValue()); container.add(sLabel); container.add(slider); container.add(results); f.add(container); f.setVisible(true); } }
Output: