java - 为什么事件在我的小程序中不起作用? (它们在原始应用程序中工作)

标签 java swing events 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();
    }
}

最佳答案

您需要设置小程序的大小。在测试时,您可以使用 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小程序语句会将宽度和高度传递给您的小程序,因此您可以通过小程序语句参数设置大小。

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

关于java - 为什么事件在我的小程序中不起作用? (它们在原始应用程序中工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8339892/

相关文章:

java - 在 JavaFX Web 浏览器中集成 Flash 播放器

java - 从 Python 中使用 jar 运行 Java 程序

Java Swing 如何在 Nimbus 中设置 JButton 大小?

c# - 按下按钮后运行事件

c++ - 无法在 mac 上使用 qt app 打开文件

c# - System.Web.httpApplicationFactory 类是什么?我在我的调用堆栈中看到它,但在框架中找不到它

java - Spring 的@DependsOn 不能处理应用程序事件?

java - 双击无法在Windows中运行jar文件

java - 在 Java 中设置游标

java - Swing 中的根 Pane 是什么?