السلام عليكم
أنا كتبت برنامج يقوم بعمليات التحويل أذا أدخل المستخدم رقم وأنا أريد أذا الم يدخل المستخدم أى رقم وطلب التحويل فنه يحسب عمليه الدخل صفر ويخرج الرقم 0.32
أوريد منكم التوضيح
وشكرا.
وهذى الكود
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication23;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextFieldDemo extends JFrame
implements ActionListener {
private JTextField jtfCelsius = new JTextField(10);
private JTextField jtfFahrenheit = new JTextField(10);
private JButton button1;
private JButton button2;
private JLabel cel;
private JLabel feh;
/** Main method */
public static void main(String[] args) {
TextFieldDemo frame = new TextFieldDemo();
frame.pack();
frame.setTitle("TextFieldDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public TextFieldDemo() {
button1 = new JButton("convert");
button2 = new JButton("clear");
// Panel p1 to hold labels
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 1));
p1.add(new JLabel("Celsius"));
p1.add(new JLabel("Fahrenheit"));
// Panel p2 to hold text fields
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2, 1));
p2.add(jtfCelsius);
p2.add(jtfFahrenheit);
JPanel p3 = new JPanel();
p3.add(button1);
p3.add(button2);
p3.setLayout(new GridLayout(1, 2));
// Add p1 and p3 to the frame
getContentPane().add(p1, BorderLayout. EAST );
getContentPane().add(p2, BorderLayout. WEST);
getContentPane().add(p3, BorderLayout.SOUTH);
// Set horizontal alignment to RIGHT for text fields
jtfCelsius.setHorizontalAlignment(JTextField.RIGHT);
jtfFahrenheit.setHorizontalAlignment(JTextField.RIGHT);
// Register listener
button1.addActionListener(this);
button2.addActionListener(this);
}
/** Handle ActionEvent */
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
double celsius =
Double.parseDouble(jtfCelsius.getText().trim());
double fahrenheit = ( (1.8) * celsius + 32);
jtfFahrenheit.setText(new Double(fahrenheit).toString());
jtfFahrenheit.requestFocusInWindow();
} else {
jtfCelsius.setText("");
jtfFahrenheit.setText("");
}
}
}