有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要帮助重置JComboBox

所以我试着把华氏温度转换成摄氏温度(反之亦然)。所以它有点工作,但我有一个下拉菜单,我从中获取值,以检查我需要的转换方程。我的程序并没有安静地完成,我只有一个方程,但一旦按下转换键,它就不会恢复正常。有人知道我如何刷新,使其返回到第一个选项吗

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);
}
}

共 (0) 个答案