2011-12-01 46 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小程序语句将在宽度和高度传递给你的小程序,让您可以通过小程序语句的参数设置的大小。

编辑添加:您为组合框设置默认字符串,但不设置默认值。如果有人只是按下计算按钮,接受组合框的默认值,那么你的程序不会计算出正确的值。

相关问题