Java单元转换器GUI应用程序

Java单元转换器GUI应用程序,java,swing,if-statement,converter,jcombobox,Java,Swing,If Statement,Converter,Jcombobox,我正在尝试制作一个简单的单位质量转换GUI应用程序。我有两个选择单位的组合框,一个输入框输入要转换的数字,以及显示转换结果的文本 我现在的问题是,我试图实际按下按钮来启动计算,但是当我从列表下拉组合框中选择任何内容时,它就会启动。我可以在文本框中输入一个数字,从第二个组合框中选择一个单位,然后从第一个组合框中选择一个单位,它会工作,因为它有它需要的变量 我的代码就在下面 public class GuiApp{ JFrame frame; JButton button; JTextF

我正在尝试制作一个简单的单位质量转换GUI应用程序。我有两个选择单位的组合框,一个输入框输入要转换的数字,以及显示转换结果的文本

我现在的问题是,我试图实际按下按钮来启动计算,但是当我从
列表
下拉组合框中选择任何内容时,它就会启动。我可以在文本框中输入一个数字,从第二个组合框中选择一个单位,然后从第一个组合框中选择一个单位,它会工作,因为它有它需要的变量

我的代码就在下面

public class GuiApp{
  JFrame frame;
  JButton button;
  JTextField field;
  JLabel prompt,result,to;
  JPanel contentpane;
  JComboBox list,list2;

  public GuiApp(){
    frame = new JFrame("Mass Conversion Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    contentpane=new JPanel();
    contentpane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    String [] type={"Units", "Gram(s)","Kilogram(s)", "Pound(s)","Ounce(s)"};
    list=new JComboBox(type);
    list.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
    list.setSelectedIndex(0);
    list.addActionListener(new converty());
    contentpane.add(list);
    to=new JLabel("to");
    contentpane.add(to);
    list2=new JComboBox(type);
    list2.setAlignmentX(JComboBox.RIGHT_ALIGNMENT);
    list2.setSelectedIndex(0);   
    list2.addActionListener(new converty());
    contentpane.add(list2);
    prompt=new JLabel("Enter here:");
    prompt.setAlignmentX(JLabel.LEFT_ALIGNMENT);
    contentpane.add(prompt);
    field=new JTextField(10);
    contentpane.add(field);
    button=new JButton("Convert");
    button.setAlignmentX(JButton.CENTER_ALIGNMENT);
    button.setEnabled(false);
    button.addActionListener(new converty());
    button.setActionCommand("Convert");
    contentpane.add(button);     
    result=new JLabel(" asdff ");
    result.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    contentpane.add(result);
    frame.pack();
    frame.setVisible(true);
    frame.setContentPane(contentpane); 
  }

  class converty implements ActionListener{

    public void actionPerformed(ActionEvent event){    
      if (!list.getSelectedItem().toString().equalsIgnoreCase("Units") && !list2.getSelectedItem().toString().equalsIgnoreCase("Units")){
        button.setEnabled(true);
      }
      if (list.getSelectedItem().toString().equalsIgnoreCase(list2.getSelectedItem().toString()) && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+thing+" "+list2.getSelectedItem().toString());
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Gram(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Kilogram(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing/1000)+" "+list2.getSelectedItem().toString());             
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Gram(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Pound(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing*453.59237)+" "+list2.getSelectedItem().toString());
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Gram(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Ounce(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing*28.349523125)+" "+list2.getSelectedItem().toString());
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Kilogram(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Gram(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing*1000)+" "+list2.getSelectedItem().toString());
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Kilogram(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Pound(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing/0.45359237)+" "+list2.getSelectedItem().toString());
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Kilogram(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Ounce(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing/0.0283495231257)+" "+list2.getSelectedItem().toString());
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Ounce(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Gram(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing*28.349523125)+" "+list2.getSelectedItem().toString());    
      }   
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Ounce(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Pound(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing/0.0625)+" "+list2.getSelectedItem().toString());
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Ounce(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Kilogram(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing/0.0625)+" "+list2.getSelectedItem().toString());
      }
      else if (list.getSelectedItem().toString().equalsIgnoreCase("Ounce(s)") && !list2.getSelectedItem().toString().equalsIgnoreCase("Pound(s)") && button.getActionCommand().equals("Convert")){
        double thing=Double.parseDouble(field.getText());
        result.setText(thing+" "+list.getSelectedItem().toString()+" is equal to "+(thing/0.0625)+" "+list2.getSelectedItem().toString());
      }
    }

  }

  private static void GuiApp(){
    JFrame.setDefaultLookAndFeelDecorated(true);
    GuiApp greeting= new GuiApp();
  }

  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
      public void run() {  
        GuiApp();
      }
    });
  }
}

您不希望在列表和按钮上都有操作侦听器。相反,只要按一下按钮就可以了


按照您的方式,每次列表发生更改时(如您所观察到的),都将执行计算。如果只是在按钮上,则只有在单击按钮时才会调用它。

当我从列表中删除actionlisteners时,If-else语句都不起作用,即使是我第一个启用按钮的语句也不起作用。对不起,我想我在发布答案之前没有完全阅读您的代码。