java单位代码对照表 java单位换算怎么写代码

java中把米换算成公里private int dist = 789;
private double dis = 0;
//java单位代码对照表你的距离数据应该不是写死的吧java单位代码对照表 , 如果你是从服务器获取的距离数据 , 可能是String,赋值给//distance时候就要强制类型转换(Integer),然后再执行以下四舍五入
dis = Math.round(dist/100d)/10d;
disText.setText(dis+"公里")
//System.out.println("距离java单位代码对照表:"+disText);
/**写的可能不规范,但就是这个意思,应该是你正在做的东西**/
数据单位转换工具java代码import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class UnitTransfer extends JFrame{
private final int WIDTH = 400, HEIGHT = 300;//窗口默认的宽度、高度
private JLabel lblInUnit=new JLabel("输入单位");
private JComboBox cboIn=new JComboBox(new String[]{"", ""});
private JLabel lblIn=new JLabel("输入数值");
private JTextField txtIn=new JTextField("10");
private JLabel lblOutUnit=new JLabel("输出单位");
private JLabel lblResult=new JLabel("显示结果");
private JLabel txtResult=new JLabel("结果");
private JComboBox cboOut=new JComboBox(new String[]{"", ""});
private JButton btnTrans = new JButton("转换");
private JButton btnClear = new JButton("清空");
private JRadioButton rdLeng = new JRadioButton("长度");
private JRadioButton rdWeig = new JRadioButton("时间");
private String [] lengthUnit={"米", "分米", "厘米", "毫米"};
private String [] timeUnit={"天", "时", "分", "秒"};
public UnitTransfer(){
super("简单的单位转换器 Beta");
this.setSize(WIDTH, HEIGHT);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonGroup group = new ButtonGroup();
group.add(rdLeng);
group.add(rdWeig);
this.getContentPane().add(rdLeng);
this.getContentPane().add(rdWeig);
this.getContentPane().add(btnTrans);
this.getContentPane().add(btnClear);
this.getContentPane().add(lblIn);
this.getContentPane().add(txtIn);
this.getContentPane().add(lblInUnit);
this.getContentPane().add(cboIn);
this.getContentPane().add(lblResult);
this.getContentPane().add(txtResult);
this.getContentPane().add(lblOutUnit);
this.getContentPane().add(cboOut);
this.setVisible(true);
this.doLayout();
btnTrans.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
doConvert();
}
});
btnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txtIn.setText("0");
txtResult.setText("0");
}
});
rdLeng.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cboIn.setModel(new DefaultComboBoxModel(lengthUnit));
cboOut.setModel(new DefaultComboBoxModel(lengthUnit));
}
});
rdWeig.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cboIn.setModel(new DefaultComboBoxModel(timeUnit));
cboOut.setModel(new DefaultComboBoxModel(timeUnit));
}
});
rdLeng.setSelected(true);
cboIn.setModel(new DefaultComboBoxModel(lengthUnit));
cboOut.setModel(new DefaultComboBoxModel(timeUnit));
}
final int offX=100;
public void doLayout(){
super.doLayout();
rdLeng.setBounds(offX, 15, 60, 20);
rdWeig.setBounds(rdLeng.getX()+rdLeng.getWidth()+5, 15, 60, 20);
lblInUnit.setBounds(offX, rdLeng.getY()+rdLeng.getHeight()+20, 80, 20);
cboIn.setBounds(lblInUnit.getX()+lblInUnit.getWidth()+5, lblInUnit.getY(), 80, 20);
lblIn.setBounds(offX, lblInUnit.getY()+lblInUnit.getHeight()+5, 80, 20);
txtIn.setBounds(lblIn.getX()+lblIn.getWidth()+5, lblIn.getY(), 80, 20);

推荐阅读