所以我试图将华氏温度转换为摄氏温度(反之亦然)转换程序。所以它有点工作,但我有一个下拉菜单,我从中获取值来检查我需要的转换方程式。我的程序没有安静完成我只有一个等式但是一旦我按下转换它就不会恢复正常。有谁知道我如何刷新所以它可以回到第一个选项?
package tools;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
public class TempConvert extends JFrame {
private JTextField Value;
private JComboBox Options;
private JLabel FVal;
private JButton Convert;
private static String[] OptionsList = {"","Celsius", "Farinheit"};
String TempVal;
int GetVal;
double C2F;
float F2C;
int GetUnit;
public TempConvert(){
super("Tempurature Converter");
setLayout(new FlowLayout());
FVal = new JLabel();
Convert = new JButton();
Options = new JComboBox(OptionsList);
Value = new JTextField("Insert Temperature here:");
Value.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
TempVal = Value.getText();
GetVal = Integer.parseInt(TempVal);
}
}
);
Options.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
if (event.getStateChange()==ItemEvent.SELECTED)
System.out.print(Options.getSelectedIndex());
GetUnit = Options.getSelectedIndex();
}
}
);
Convert.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (GetUnit==1){
double Calc= (GetVal * 1.8);
C2F = (Calc+32);
System.out.println(C2F);
FVal.setText(C2F + " Fahrenheit");
Convert.revalidate();
Convert.repaint();
}
}
}
);
add(Options);
add(Value);
add (Convert);
add(FVal);
}
}