Everything works fine, except for one thing. I need this program to show a messa
ID: 667962 • Letter: E
Question
Everything works fine, except for one thing.
I need this program to show a message in the event the user inputs anything else instead of numbers.
This is what it looks like when running:
I NEED IT TO DISPLAY THIS IF USER INPUTS OTHER THAN NUMBERS:
Here is the source:
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class CelsiusConverter extends JPanel {
private final JTextField jpt6;
private final JLabel jpt7;
private JLabel celAns;
private final JButton jpt8;
private final JTextField jpt9;
private final JLabel jpt10;
private JLabel farAns;
private final JButton jpt11;
public CelsiusConverter() {
jpt6 = new JTextField(5);
jpt7 = new JLabel("Celsius");
celAns = new JLabel("");
jpt8 = new JButton("Convert");
jpt9 = new JTextField(5);
jpt10 = new JLabel("Farenheit");
farAns = new JLabel("");
jpt11 = new JButton("Convert");
setPreferredSize (new Dimension (325,200));
setLayout (null);
add (jpt6);
add (jpt7);
add (jpt8);
add(celAns);
add (jpt9);
add (jpt10);
add (jpt11);
add (farAns);
jpt8.addActionListener((ActionEvent ae) -> {
int temp1 = Integer.parseInt(jpt6.getText());
float far = ((temp1 * 9) / 5) + 32;
celAns.setText(far +" °F");
});
jpt11.addActionListener((ActionEvent ae) -> {
int temp2 = Integer.parseInt(jpt9.getText());
float cel = ((temp2 -32) * 5/ 9);
farAns.setText(cel +" °C");
});
jpt6.setBounds (45, 95, 50, 25);
jpt7.setBounds (105, 95, 50, 25);
celAns.setBounds(50, 125, 100, 25);
jpt8.setBounds (25, 155, 85, 25);
jpt9.setBounds (235, 95, 50, 25);
jpt10.setBounds (175, 95, 100, 25);
jpt11.setBounds (215, 155, 85, 25);
farAns.setBounds(235, 125, 100, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Converter");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new CelsiusConverter());
frame.pack();
frame.setVisible (true);
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
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("Fahrenheit to Celsius Calculator");
ConvertorPanel panel = new ConvertorPanel();
add(panel);
pack();
}
}
class ConvertorPanel extends JPanel{
public ConvertorPanel(){
setLayout(new BorderLayout());
//add the panel
panel = new JPanel();
//add JTextField and position it
textField = new JTextField();
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(2,1));
//add the text field label and position it
northPanel.add(new JLabel("Type the number to
convert below: ", SwingConstants.LEFT));
northPanel.add(textField);
add(northPanel, BorderLayout.NORTH);
//set the panel and position it
panel.setLayout(new GridLayout(1,2));
//add buttons and position them
cButton = new JButton("Convert to Celsius");
panel.add(cButton);
fButton = new JButton("Convert to
Fahrenheit");
panel.add(fButton);
add(panel, BorderLayout.SOUTH);
Converter converter = new Converter();
cButton.addActionListener(converter);
fButton.addActionListener(converter);
}
private class Converter implements ActionListener{
public void actionPerformed (ActionEvent
event){
if (event.getSource() == cButton){
int c = (int)
(((Double.parseDouble(textField.getText()))- 32) * 0.56);
textField.setText(c + " °C");
textField.requestFocus();
textField.selectAll();
}
else if(event.getSource() == fButton){
int f = (int)(((Double.parseDouble
(textField.getText()))* 1.8) + 32);
textField.setText(f + " ºF");
textField.requestFocus();
textField.selectAll();
}
}
}
private JButton cButton, fButton;
private JTextField textField;
private JPanel panel;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.