2011-12-01 72 views
0

我的小應用程序基本上是兩個組合框,它們將變量的值存儲在其事件中(當用戶選擇一個選項時)。確認按鈕會生成一個將這兩個值相加的事件。應用程序工作正常,但當我嘗試將其轉換爲小程序時,文本字段不顯示,並且似乎有一些警告標誌,每當我單擊組合選項時會出現任何通知請嗎?爲什麼事件不能在我的小程序中工作? (他們在原始應用程序中工作)

這裏是該applet代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class DormPlanApplet extends JApplet 
{ 
    private JPanel selectionPanel; 
    private JPanel costPanel; 
    private JComboBox dormListBox; 
    private JComboBox mealPlanListBox; 
    private JLabel costLabel; 
    private JTextField costField; 
    private JButton confirmButton; 
    double dormCost; 
    double mealCost; 
    double totalCost; 
    int checker1; 
    int checker2; 
    String costString; 

    private String[] dormListArray = {"Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" }; 
    private String[] mealPlanListArray = {"7 Meals", "14 Meals", "Unlimited Meals" }; 


    public void init() 
    { 
     //super("College Cost Calculator"); 

     //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     setLayout(new BorderLayout()); 

     buildSelectionPanel(); 
     buildCostPanel(); 

     add(selectionPanel, BorderLayout.CENTER); 
     add(costPanel, BorderLayout.SOUTH); 

     //pack(); 
     //setVisible(true); 
    } 


    private void buildSelectionPanel() 
    { 
     selectionPanel = new JPanel(); 
     selectionPanel.setLayout(new FlowLayout()); 

     dormListBox = new JComboBox(dormListArray); 
     mealPlanListBox = new JComboBox(mealPlanListArray); 

     dormListBox.addActionListener(new dormCostListener()); 
     mealPlanListBox.addActionListener(new mealCostListener()); 

     selectionPanel.add(dormListBox); 
     selectionPanel.add(mealPlanListBox); 

    } 

    private void buildCostPanel() 
    { 
     costPanel = new JPanel(); 
     costPanel.setLayout(new FlowLayout()); 

     costLabel = new JLabel("The total cost is:"); 
     confirmButton = new JButton("Confirm"); 
     confirmButton.addActionListener(new calcButtonListener()); 

     costField = new JTextField(12); 
     costField.setEditable(false); 
     costPanel.add(confirmButton); 
     costPanel.add(costLabel); 
     costPanel.add(costField); 
    } 

    private class dormCostListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

      checker1 = 1; 

      switch (dormListBox.getSelectedIndex()) 
      { 
       case 0: 
        dormCost = 1500; 
       break; 

       case 1: 
        dormCost = 1600; 
       break; 

       case 2: 
        dormCost = 1200; 
       break; 


       case 3: 
        dormCost = 1800; 
       break; 

       default: 
        dormCost = 0; 
       break; 
      } 
     } 
    } 


    private class mealCostListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      checker2 = 1; 

      switch (mealPlanListBox.getSelectedIndex()) 
      { 
       case 0: 
        mealCost = 560; 
       break; 

       case 1: 
        mealCost = 1095; 
       break; 

       case 2: 
        mealCost = 1500; 
       break; 

       default: 
        mealCost = 0; 
       break; 
      } 
     } 
    } 

    private class calcButtonListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      if ((checker1 == 1) && (checker2 == 1)) 
      { 
       totalCost = dormCost + mealCost; 
       costString = Double.toString(totalCost); 

       costField.setText(costString); 
      } else { 
        costField.setText("It doesn't work!"); 
       } 

     } 
    } 

} 

下面是oringal機應用代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class DormPlanApp extends JFrame 
{ 
    private JPanel selectionPanel; 
    private JPanel costPanel; 
    private JComboBox dormListBox; 
    private JComboBox mealPlanListBox; 
    private JLabel costLabel; 
    private JTextField costField; 
    private JButton confirmButton; 
    double dormCost; 
    double mealCost; 
    double totalCost; 
    int checker1; 
    int checker2; 
    String costString; 

    private String[] dormListArray = {"Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" }; 
    private String[] mealPlanListArray = {"7 Meals", "14 Meals", "Unlimited Meals" }; 


    public DormPlanApp() 
    { 
     super("College Cost Calculator"); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     setLayout(new BorderLayout()); 

     buildSelectionPanel(); 
     buildCostPanel(); 

     add(selectionPanel, BorderLayout.CENTER); 
     add(costPanel, BorderLayout.SOUTH); 

     pack(); 
     setVisible(true); 
    } 


    private void buildSelectionPanel() 
    { 
     selectionPanel = new JPanel(); 
     selectionPanel.setLayout(new FlowLayout()); 

     dormListBox = new JComboBox(dormListArray); 
     mealPlanListBox = new JComboBox(mealPlanListArray); 

     dormListBox.addActionListener(new dormCostListener()); 
     mealPlanListBox.addActionListener(new mealCostListener()); 

     selectionPanel.add(dormListBox); 
     selectionPanel.add(mealPlanListBox); 

    } 

    private void buildCostPanel() 
    { 
     costPanel = new JPanel(); 
     costPanel.setLayout(new FlowLayout()); 

     costLabel = new JLabel("The total cost is:"); 
     confirmButton = new JButton("Confirm"); 
     confirmButton.addActionListener(new calcButtonListener()); 

     costField = new JTextField(12); 
     costField.setEditable(false); 
     costPanel.add(confirmButton); 
     costPanel.add(costLabel); 
     costPanel.add(costField); 
    } 

    private class dormCostListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

      checker1 = 1; 

      switch (dormListBox.getSelectedIndex()) 
      { 
       case 0: 
        dormCost = 1500; 
       break; 

       case 1: 
        dormCost = 1600; 
       break; 

       case 2: 
        dormCost = 1200; 
       break; 


       case 3: 
        dormCost = 1800; 
       break; 

       default: 
        dormCost = 0; 
       break; 
      } 
     } 
    } 


    private class mealCostListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      checker2 = 1; 

      switch (mealPlanListBox.getSelectedIndex()) 
      { 
       case 0: 
        mealCost = 560; 
       break; 

       case 1: 
        mealCost = 1095; 
       break; 

       case 2: 
        mealCost = 1500; 
       break; 

       default: 
        mealCost = 0; 
       break; 
      } 
     } 
    } 

    private class calcButtonListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      if ((checker1 == 1) && (checker2 == 1)) 
      { 
       totalCost = dormCost + mealCost; 
       costString = Double.toString(totalCost); 

       costField.setText(costString); 
      } else { 
        costField.setText("It doesn't work!"); 
       } 

     } 
    } 

    public static void main(String[] args) 
    { 
     new DormPlanApp(); 
    } 
} 
+0

*「文本字段不顯示」*它在這裏。 *「每當我點擊一個組合選項時,似乎都會出現一些警告標誌」* DYM在組合附近漂浮的一個小黃色三角形? –

+0

是的,這就是我的意思---黃色的小三角形。 – Johnny

+1

如果黃色三角形離開小程序的區域,則對於任何浮動元素(例如組合框的下拉列表),黃色三角形是正常的。如果小程序是可信的,它將消失,或者組合向上移動小程序(所以下拉菜單仍在小程序範圍內)。 –

回答

1

你需要設置你的小應用程序的大小。測試時,你可以使用setSize方法,像這樣:

public void init() 
{ 
    //super("College Cost Calculator"); 

    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    setLayout(new BorderLayout()); 

    buildSelectionPanel(); 
    buildCostPanel(); 

    add(selectionPanel, BorderLayout.CENTER); 
    add(costPanel, BorderLayout.SOUTH); 

    setSize(400, 100); 

    //pack(); 
    //setVisible(true); 
} 

的HTML小程序語句將在寬度和高度傳遞給你的小程序,讓您可以通過小程序語句的參數設置的大小。

編輯添加:您爲組合框設置默認字符串,但不設置默認值。如果有人只是按下計算按鈕,接受組合框的默認值,那麼你的程序不會計算出正確的值。

相關問題