2012-10-15 126 views
3

我正在製作一個小程序,它根據從java中的組合框中選擇的5種不同的物理活動來計算消耗的卡路里。用戶有5種選擇:保齡球,射箭,lacross,摔跤和繪畫。這裏是計算給定數字的網站,用戶在幾分鐘或幾小時內輸入他的體重和他制定的體重多久,當他/她進行計算時,它應顯示燃燒的總卡路里。基於組合框選擇的更新文本框

http://www.nutristrategy.com/activitylist4.htm

我的問題是我不知道如何使基於選擇的字段更新,換句話說,我需要創建一個等待選擇的方法,然後更新公式來計算他們的卡路里根據它們的重量燃燒。我該怎麼做呢?我做了一個if else聲明,上面寫着「如果射箭選中並且重量文本框中有一個數字?」那麼使用這個公式?我不知道如何檢查選擇是從組合框。

這是我的代碼。

import java.awt.Component; 
import java.awt.event.*; 
import java.awt.*; 
import javax.swing.*; 
import net.miginfocom.swing.MigLayout; 
import javax.imageio.ImageIO; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

public class calorie extends JFrame { 

    public calorie() { 
     JLabel titleLabel = new JLabel("Calories Burned during excercise"); 
     titleLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
     titleLabel.setForeground(Color.blue); 

     final JLabel distLabel = new JLabel("Choose an Activity"); 
     final JTextField distText = new JTextField(8); 
     String[] actStrings = {"bowling", "lacrosse", "wrestling", "painting", "archery"}; 
     JComboBox jComboBox1 = new JComboBox(actStrings); 
     jComboBox1.setEditable(true); 

     final JLabel fuelLabel = new JLabel("Current weight:"); 
     final JTextField fuelText = new JTextField(8); 

     final JLabel actTime = new JLabel("How long did you work out:"); 
     final JTextField time = new JTextField(8); 
     String[] timeStrings = {"Minutes", "Hours"}; 
     JComboBox jComboBox2 = new JComboBox(timeStrings); 
     jComboBox2.setEditable(true); 

     final JLabel mpgLabel = new JLabel("Calories burned = "); 
     final JTextField mpgText = new JTextField(8); 

     JButton clearButton = new JButton("Clear"); 
     JButton calcButton = new JButton("Calculate"); 
     final JLabel labelPic1; 
     final ImageIcon[] imgIcons; 
     String[] fileNames = {"pics/bowling.jpg", "pics/lacrosse.jpg", 
      "pics/wrestling.jpg", "pics/painting.jpg", "pics/archery.jpg"}; 
     imgIcons = new ImageIcon[fileNames.length]; 

     BufferedImage image = null; 
     for (int i = 0; i < fileNames.length; i++) { 
      try { 
       image = ImageIO.read(new File(fileNames[i])); 
      } catch (IOException ex) { 
       System.out.println(ex.toString()); 
       System.out.println(fileNames[i]); 
       JOptionPane.showMessageDialog(null, ex.toString() + " " + fileNames[i]); 
       System.exit(0); // exit program 
      } 
      Image newimg = image.getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH); 
      imgIcons[i] = new ImageIcon(newimg); 
     } 
     labelPic1 = new JLabel(imgIcons[0]); 
     setResizable(false); 
     JPanel p = new JPanel(new MigLayout("", "[][][][][]", 
       "[][][][][][][][][][][][][][][]")); 
     p.setBackground(Color.WHITE); 
     setContentPane(p); 
     p.add(labelPic1, "cell 0 3 1 3"); 
     p.add(calcButton, "cell 0 9"); 
     p.add(titleLabel, "cell 1 0 2 1"); 
     p.add(distLabel, "cell 0 2"); 
     p.add(fuelLabel, "cell 1 5"); 
     p.add(mpgLabel, "cell 1 9"); 
     p.add(jComboBox1, "cell 1 2"); 
     p.add(fuelText, "cell 1 7"); 
     p.add(jComboBox2, "cell 1 7"); 
     p.add(actTime, "cell 0 7"); 
     p.add(distText, "cell 1 5"); 
     p.add(mpgText, "cell 1 9"); 

     jComboBox1.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent event) { 
       //JComboBox jComboBox1 = (JComboBox)event.getSource(); 
       JComboBox jComboBox1 = (JComboBox) event.getSource(); 
       if (jComboBox1.getSelectedItem() == "bowling") { 
        labelPic1.setIcon(imgIcons[0]); 
       } 
       if (jComboBox1.getSelectedItem() == "lacrosse") { 
        labelPic1.setIcon(imgIcons[1]); 
       } 
       if (jComboBox1.getSelectedItem() == "wrestling") { 
        labelPic1.setIcon(imgIcons[2]); 
       } 
       if (jComboBox1.getSelectedItem() == "painting") { 
        labelPic1.setIcon(imgIcons[3]); 
       } 
       if (jComboBox1.getSelectedItem() == "archery") { 
        labelPic1.setIcon(imgIcons[4]); 
       } 
      } 
     }); 

     calcButton.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent event) { 

       if (isNumeric(distText.getText()) && isNumeric(fuelText.getText())) { 
        double fuel; 
        double dist; 
        double result; 
        fuel = Double.parseDouble(fuelText.getText()); 
        dist = Double.parseDouble(distText.getText()); 
        result = dist/fuel; 
        mpgText.setText(String.format("%f", result)); 
       } else { 
        JOptionPane.showMessageDialog(null, "Enter distance traveled and fuel used"); 
       } 
      } 
     }); 
     setTitle("Calorie Calculator"); 
     pack(); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    } 

    private static boolean isNumeric(String text) { 
     try { 
      Double.parseDouble(text); 
     } catch (Exception e) { 
      return false; 
     } 
     return true; 
    } 

    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       try { 
        UIManager.setLookAndFeel(
          // "javax.swing.plaf.metal.MetalLookAndFeel"); 
          // "com.sun.java.swing.plaf.motif.MotifLookAndFeel"); 
          UIManager.getCrossPlatformLookAndFeelClassName()); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
       new calorie().setVisible(true); 
      } 
     }); 
    } 
} 

回答

3

我做出的if else語句說,「如果選擇了射箭和存在的重文本是多少?」那麼使用這個公式?

是。

我還沒有讀取所有代碼,但是您希望通過添加對更新文本框方法updateTextBox();的調用並刪除JComboBox jComboBox1 = (JComboBox) event.getSource();來執行此類操作。

編輯,代碼如下更新:

public void actionPerformed(ActionEvent event) 
{ 
    updateTextBox(); 
} 

下面是其中您可以更改和更新文本框的方法:

public void updateTextBox() 
{ 
    if (jComboBox1.getSelectedIndex() == 0) //bowling 
    { 
     labelPic1.setIcon(imgIcons[0]); 
     //Update textbox here to show results for item 0 
    } 
    else if (jComboBox1.getSelectedIndex() == 1) //lacrosse 
    { 
     labelPic1.setIcon(imgIcons[1]); 
     //Update textbox here to show results for item 1 
    } 
} 

爲了使這項工作,你需要讓你的「jComboBox1」靜。 所以加入這一行的拳頭級以上static JComboBox jComboBox1 = null

然後用這個jComboBox1 = new JComboBox(actStrings);替換JComboBox jComboBox1 = new ComboBox(actStrings);

,因爲我們已經與static JComboBox jComboBox1 = null

+0

創建的組合框前面謝謝我想通了 – user1730008

+0

以防萬一,我更新我的代碼在第一個框中刪除getItem檢查,因爲'updateTextBox()'處理它,所以不需要它。 – sorifiend