温度转换

package homeWork13;

 

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class TempertureConvert extends JFrame {// 温度转换,摄氏,华氏,开式
 private static final long serialVersionUID = 1L;

 private JLabel fromJLabel, enterJLabel, toJLabel, outJLabel;
 private JRadioButton fahFromRadioButton, celFromRadioButton,
   kelFromRadioButton, fahToRadioButton, celToRadioButton,
   kelToRadioButton;
 private ButtonGroup fromButtonGroup, toButtonGroup;
 private JTextField inputField, outputField;
 private JPanel converToJPanel,convertFromJPanel;
 private int tempFrom,tempTo;

 public TempertureConvert() {
  super("Temperture Convert");

  // 创建GUI
  fromJLabel = new JLabel("Convert From:");
  fahFromRadioButton=new JRadioButton("Fahrenheit");
  celFromRadioButton=new JRadioButton("Celcius");
  kelFromRadioButton=new JRadioButton("Kelvin");
  fromButtonGroup=new ButtonGroup();//使用按钮组,使得一次只能选中一个按钮
  fromButtonGroup.add(fahFromRadioButton);
  fromButtonGroup.add(celFromRadioButton);
  fromButtonGroup.add(kelFromRadioButton);
  
  enterJLabel=new JLabel("Enter Numeric Temperature:");
  inputField=new JTextField(10);
  
  toJLabel=new JLabel("Convert to:");
  fahToRadioButton=new JRadioButton("Fahrenheit");
  celToRadioButton=new JRadioButton("Celcius");
  kelToRadioButton=new JRadioButton("Kelvin");
  toButtonGroup=new ButtonGroup();
  toButtonGroup.add(fahToRadioButton);
  toButtonGroup.add(celToRadioButton);
  toButtonGroup.add(kelToRadioButton);
  
  outJLabel=new JLabel("Comparable Temperature:");
  outputField=new JTextField(10);
  outputField.setEditable(false);
  
  convertFromJPanel=new JPanel();
  convertFromJPanel.setLayout(new GridLayout(1,3));//一行三列的表格
  convertFromJPanel.add(fahFromRadioButton);
  convertFromJPanel.add(celFromRadioButton);
  convertFromJPanel.add(kelFromRadioButton);
  
  converToJPanel=new JPanel();
  converToJPanel.setLayout(new GridLayout(1,3));
  converToJPanel.add(fahToRadioButton);
  converToJPanel.add(celToRadioButton);
  converToJPanel.add(kelToRadioButton);
  
  Container container=getContentPane();
  container.setLayout(new GridLayout(8,1));
  container.add(fromJLabel);
  container.add(convertFromJPanel);
  container.add(enterJLabel);
  container.add(inputField);
  container.add(toJLabel);
  container.add(converToJPanel);
  container.add(outJLabel);
  container.add(outputField);
  
  inputField.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent event) {//按下回车,进行温度转换
    // TODO Auto-generated method stub
    tempFrom=Integer.parseInt(event.getActionCommand());
    
    if(fahFromRadioButton.isSelected()&&fahToRadioButton.isSelected())
     tempTo=tempFrom;
    else if(fahFromRadioButton.isSelected()&&celToRadioButton.isSelected())
     tempTo=(int)(5.0f/9.0f*(tempFrom-32));
    else if(fahFromRadioButton.isSelected()&&kelToRadioButton.isSelected())
     tempTo=(int)(5.0f/9.0f*(tempFrom-32))+273;
    
    else if(celFromRadioButton.isSelected()&&fahToRadioButton.isSelected())
     tempTo=(int)(9.0f/5.0f*tempFrom+32);
    else if(celFromRadioButton.isSelected()&&celToRadioButton.isSelected())
     tempTo=tempFrom;
    else if(celFromRadioButton.isSelected()&&kelToRadioButton.isSelected())
     tempTo=tempFrom+273;
    
    else if(kelFromRadioButton.isSelected()&&fahToRadioButton.isSelected())
     tempTo=(int)(9.0f/5.0f*(tempFrom+273)+32);
    else if(kelFromRadioButton.isSelected()&&celToRadioButton.isSelected())
     tempTo=tempFrom-273;
    else if(kelFromRadioButton.isSelected()&&kelToRadioButton.isSelected())
     tempTo=tempFrom;
    
    outputField.setText(tempTo+"");
   }
  });
  
  setSize(280, 225);
  setVisible(true);
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  TempertureConvert application=new TempertureConvert();
  application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

}

你可能感兴趣的:(String,application,homework,Class,import)