:)
/*
* 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, 2));
p1.add(button1);
p1.add(new JLabel("Celsius"));
p1.add(button2);
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);
// Add p1 and p3 to the frame
getContentPane().add(p1, BorderLayout.WEST);
getContentPane().add(p2, BorderLayout.CENTER);
// 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) {
int celsius =
(int) Double.parseDouble(jtfCelsius.getText().trim());
int fahrenheit = ((int)(9 / 5) * celsius + 32);
jtfFahrenheit.setText(new Double(fahrenheit).toString());
jtfFahrenheit.requestFocusInWindow();
}
else{
jtfCelsius.setText("");
jtfFahrenheit.setText("");
}
}
}No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!
