简体   繁体   English

为什么事件在我的applet中不起作用? (它们适用于原始应用程序)

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

My applet is basically two combo boxes that store values in variables with their events (when the user selects an option). 我的applet基本上是两个组合框,它们将值及其事件(当用户选择一个选项时)存储在变量中。 The confirm button generates an event that adds those two values together. 确认按钮生成一个事件,将这两个值加在一起。 The appliction works fine, but when I try to convert it into an applet, the text field doesn't show up, and there seems to be some warning sign that comes whenever I click a combo option Any advice please? 该应用程序工作正常,但是当我尝试将其转换为applet时,文本字段没有显示,每当我单击组合选项时似乎都会出现一些警告信号。

Here is the code for the 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!");
                }

        }
    }

}

Here is the code for the oringal appliction: 这是口头应用的代码:

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

You need to set the size of your applet. 您需要设置小程序的大小。 While testing, you can use the setSize method, like this: 在测试时,可以使用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);
}

The HTML applet statement will pass the width and height to your applet, so you can set the size through the applet statement parameters. HTML applet语句会将宽度和高度传递给applet,因此您可以通过applet语句参数设置大小。

Edited to add: You set default strings for the combo boxes, but not default values. 编辑添加:您可以为组合框设置默认字符串,但不能设置默认值。 If someone just presses the Calculate button, accepting the combo box defaults, then your program doesn't calculate the correct value. 如果有人只是按下“计算”按钮,接受默认的组合框,则您的程序将无法计算正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM