-
Help! The integer always jump.
Hi,
Having problem here with my program. The number shown in the CM box always jumped to show same result as the output (inch box) during converting but there is no problem when convert from inch to cm. Anyone help to debug on my source code?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class MetricConverter extends JFrame {
private JButton btnDivide, btnMultiply;
private JTextField txtNum1, txtNum2;
private JLabel lab1Cm, lab2Inches;
public MetricConverter() {
setSize(400, 180);
setTitle("ABC Metric Converter");
setVisible(true);
JPanel contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
btnDivide = new JButton(">");
btnDivide.setBounds(new Rectangle(100, 20, 50, 30));
contentPane.add(btnDivide);
btnMultiply = new JButton("<");
btnMultiply.setBounds(new Rectangle(100, 50, 50, 30));
contentPane.add(btnMultiply);
txtNum1 = new JTextField("0");
txtNum1.setBounds(new Rectangle(10, 30, 50, 20));
contentPane.add(txtNum1);
txtNum2 = new JTextField("0");
txtNum2.setBounds(new Rectangle(170, 30, 50, 20));
contentPane.add(txtNum2);
lab1Cm = new JLabel("cm");
lab1Cm.setBounds(new Rectangle(65, 30, 40,20));
contentPane.add(lab1Cm);
lab2Inches = new JLabel("Inches");
lab2Inches.setBounds(new Rectangle(230, 30, 40,20));
contentPane.add(lab2Inches);
AHandler handle = new AHandler();
btnDivide.addActionListener(handle);
btnMultiply.addActionListener(handle);
}
public static void main(String[] args) {
MetricConverter myFrame = new MetricConverter();
}
class AHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
double num1 = Double.parseDouble(txtNum1.getText());
double num2 = Double.parseDouble(txtNum2.getText());
double answer = 0;
if (e.getSource() == btnDivide) {
answer = num1 / 2.54;
txtNum2.setText(" " + fmt.format(answer));
}
else
answer = num2 * 2.54;
txtNum1.setText(" " + fmt.format(answer));
}
DecimalFormat fmt = new DecimalFormat("0.#");
// if (e.getSource() == btnMultiply) {
// answer = num2 * 2.54;
// txtNum1.setText(" " + fmt.format(answer));
// }
// else
// answer = num1 / 2.54;
// txtNum2.setText(" " + fmt.format(answer));
}
}
Similar Threads
-
By angela_quests in forum VB Classic
Replies: 2
Last Post: 04-13-2007, 04:57 AM
-
Replies: 3
Last Post: 04-08-2006, 06:38 PM
-
By eduardoms in forum Security
Replies: 2
Last Post: 05-06-2005, 10:26 AM
-
Replies: 146
Last Post: 08-12-2002, 10:40 PM
-
By Kunal Sharma in forum VB Classic
Replies: 2
Last Post: 04-25-2000, 03:45 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|