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

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

java swing program converter km to miles and vice versa

问题

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Converter extends JFrame implements ActionListener {
    JLabel label = new JLabel("Distance : ");
    JTextField input = new JTextField(10);
    JButton button = new JButton("Convert");
    JTextArea output = new JTextArea(10, 15);
    CheckboxGroup cbg = new CheckboxGroup();
    Checkbox cb1 = new Checkbox("Convert MILES to KM", cbg, true);
    Checkbox cb2 = new Checkbox("Convert KM to MILES", cbg, false);

    public static void main(String args[]) {
        Converter s = new Converter();
        s.setVisible(true);
    }

    public Converter() {
        setLayout(null);
        setSize(300, 400);
        // left-down-width-height
        cb1.setBounds(60, 30, 150, 30);
        cb2.setBounds(60, 60, 150, 30);
        label.setBounds(30, 90, 120, 30);
        input.setBounds(90, 95, 170, 20);
        button.setBounds(100, 130, 90, 30);
        output.setBounds(45, 168, 200, 165);

        add(cb1);
        add(cb2);
        add(label);
        add(input);
        add(button);
        add(output);

        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (cb1.getState()) {
            if (e.getSource() == button) {
                double d = Double.parseDouble(input.getText());
                double d2 = d * 0.62;
                String str2 = String.valueOf(d2);

                output.setText(str2);
            } else {
            }
        }
    }
}
英文:

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:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Converter extends JFrame implements ActionListener {
    JLabel label = new JLabel("Distance : ");
    JTextField input = new JTextField(10);
    JButton button = new JButton("Convert");
    JTextArea output = new JTextArea(10,15);
    CheckboxGroup cbg = new CheckboxGroup();
    Checkbox cb1 = new Checkbox("Convert MILES to KM", cbg, true);
    Checkbox cb2 = new Checkbox("Convert KM to MILES", cbg, false);

    public static void main(String args[]){
        Converter s = new Converter();
        s.setVisible(true);
    }

    public Converter(){
        setLayout(null);
        setSize(300,400);  
        //left-down-width-hegiht
        cb1.setBounds(60,30,150,30);
        cb2.setBounds(60,60,150,30);
        label.setBounds(30,90,120,30);
        input.setBounds(90,95,170,20);
        button.setBounds(100,130,90,30);
        output.setBounds(45,168,200,165);
                
        add(cb1);
        add(cb2);
        add(label);
        add(input);
        add(button);
        add(output);
    }

    public void actionPerformed(ActionEvent e){
        if (cb1 .equals(true) ) {
            if (e.getSource() == button ){
                double d = Double.parseDouble(input.getText());
                double d2 =  d* 0.62;
                String str2 = String.valueOf(d2); 

                output.setText(str2);
            }
            else { 
            }
        }
    }		 
}

答案1

得分: 2

Sure, here's the translation:

2件事情:

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

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

    button.addActionListener(this);
    
英文:

2 things:

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

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

    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:

确定