My code is not sending out the message correctly. I want the message to show if
ID: 667987 • Letter: M
Question
My code is not sending out the message correctly. I want the message to show if a "letter" is inputted not when a "number" is inputted.
The message is backwards and just needs to be modified slightly to only show when the user inputs a letter. I have the messenger identified in "BOLD".
Inputting a number as shown below:
Source code (Netbeans)
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int temp1 = Integer.parseInt(jpt6.getText());
float far = ((temp1 * 9) / 5) + 32;
celAns.setText(far +" °F");
//Notify numbers only
JOptionPane.showMessageDialog(null,
"Input numbers only!.",
"Input warning",
JOptionPane.WARNING_MESSAGE);
}
});
jpt11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int temp2 = Integer.parseInt(jpt9.getText());
float cel = ((temp2 -32) * 5/ 9);
farAns.setText(cel +" °C");
//Notify numbers only
JOptionPane.showMessageDialog(null,
"Input numbers only!.",
"Input warning",
JOptionPane.WARNING_MESSAGE);
}
});
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);
}
}
Shows this error:
Converter DX Celsius Farenheit 1 33.0 °F 17.0 °C Convert ConvertExplanation / Answer
//Notify numbers only
JOptionPane.showMessageDialog(input,
"Input numbers only!.",
"Input warning",
JOptionPane.WARNING_MESSAGE);
}
//Notify numbers only
JOptionPane.showMessageDialog(input,
"Input numbers only!.",
"Input warning",
JOptionPane.WARNING_MESSAGE)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.