簡體   English   中英

為什么事件在我的applet中不起作用? (它們適用於原始應用程序)

[英]Why don't events work in my applet? (they work in the original application)

我的applet基本上是兩個組合框,它們將值及其事件(當用戶選擇一個選項時)存儲在變量中。 確認按鈕生成一個事件,將這兩個值加在一起。 該應用程序工作正常,但是當我嘗試將其轉換為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!");
                }

        }
    }

}

這是口頭應用的代碼:

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

您需要設置小程序的大小。 在測試時,可以使用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 applet語句會將寬度和高度傳遞給applet,因此您可以通過applet語句參數設置大小。

編輯添加:您可以為組合框設置默認字符串,但不能設置默認值。 如果有人只是按下“計算”按鈕,接受默認的組合框,則您的程序將無法計算正確的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM