英文:
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件事情:
-
复选框上的检查是错误的。应该是:
if (cb1.getState()) {...}
-
您必须在按钮中注册actionlistener:在构造函数中添加:
button.addActionListener(this);
英文:
2 things:
-
the check on the checkbox is wrong. It should be:
if (cb1.getState()) {...}
-
you have to register the actionlistener in the button: in the constructor add:
button.addActionListener(this);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论