Java Swing程序转换器,用于将公里转换为英里,反之亦然。

huangapple go评论101阅读模式
英文:

java swing program converter km to miles and vice versa

问题

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Converter extends JFrame implements ActionListener {
  5. JLabel label = new JLabel("Distance : ");
  6. JTextField input = new JTextField(10);
  7. JButton button = new JButton("Convert");
  8. JTextArea output = new JTextArea(10, 15);
  9. CheckboxGroup cbg = new CheckboxGroup();
  10. Checkbox cb1 = new Checkbox("Convert MILES to KM", cbg, true);
  11. Checkbox cb2 = new Checkbox("Convert KM to MILES", cbg, false);
  12. public static void main(String args[]) {
  13. Converter s = new Converter();
  14. s.setVisible(true);
  15. }
  16. public Converter() {
  17. setLayout(null);
  18. setSize(300, 400);
  19. // left-down-width-height
  20. cb1.setBounds(60, 30, 150, 30);
  21. cb2.setBounds(60, 60, 150, 30);
  22. label.setBounds(30, 90, 120, 30);
  23. input.setBounds(90, 95, 170, 20);
  24. button.setBounds(100, 130, 90, 30);
  25. output.setBounds(45, 168, 200, 165);
  26. add(cb1);
  27. add(cb2);
  28. add(label);
  29. add(input);
  30. add(button);
  31. add(output);
  32. button.addActionListener(this);
  33. }
  34. public void actionPerformed(ActionEvent e) {
  35. if (cb1.getState()) {
  36. if (e.getSource() == button) {
  37. double d = Double.parseDouble(input.getText());
  38. double d2 = d * 0.62;
  39. String str2 = String.valueOf(d2);
  40. output.setText(str2);
  41. } else {
  42. }
  43. }
  44. }
  45. }
英文:

I have created a program in java using swing and ActionListener the program simply to converter km to miles and vice versa. at the actionPerformed i am trying to return value to the output variable but I can not get the value back in the JTextArea.
the code attached below:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Converter extends JFrame implements ActionListener {
  5. JLabel label = new JLabel("Distance : ");
  6. JTextField input = new JTextField(10);
  7. JButton button = new JButton("Convert");
  8. JTextArea output = new JTextArea(10,15);
  9. CheckboxGroup cbg = new CheckboxGroup();
  10. Checkbox cb1 = new Checkbox("Convert MILES to KM", cbg, true);
  11. Checkbox cb2 = new Checkbox("Convert KM to MILES", cbg, false);
  12. public static void main(String args[]){
  13. Converter s = new Converter();
  14. s.setVisible(true);
  15. }
  16. public Converter(){
  17. setLayout(null);
  18. setSize(300,400);
  19. //left-down-width-hegiht
  20. cb1.setBounds(60,30,150,30);
  21. cb2.setBounds(60,60,150,30);
  22. label.setBounds(30,90,120,30);
  23. input.setBounds(90,95,170,20);
  24. button.setBounds(100,130,90,30);
  25. output.setBounds(45,168,200,165);
  26. add(cb1);
  27. add(cb2);
  28. add(label);
  29. add(input);
  30. add(button);
  31. add(output);
  32. }
  33. public void actionPerformed(ActionEvent e){
  34. if (cb1 .equals(true) ) {
  35. if (e.getSource() == button ){
  36. double d = Double.parseDouble(input.getText());
  37. double d2 = d* 0.62;
  38. String str2 = String.valueOf(d2);
  39. output.setText(str2);
  40. }
  41. else {
  42. }
  43. }
  44. }
  45. }

答案1

得分: 2

Sure, here's the translation:

2件事情:

  1. 复选框上的检查是错误的。应该是:

    1. if (cb1.getState()) {...}
  2. 您必须在按钮中注册actionlistener:在构造函数中添加:

    1. button.addActionListener(this);
英文:

2 things:

  1. the check on the checkbox is wrong. It should be:

    1. if (cb1.getState()) {...}
  2. you have to register the actionlistener in the button: in the constructor add:

    1. button.addActionListener(this);

huangapple
  • 本文由 发表于 2020年9月22日 19:37:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64008942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定