1

为同意服务条款的复选框和接受和拒绝按钮编码。

我需要帮助来处理异常

如果用户没有选择复选框,那么当他们点击接受时,会出现一条错误消息,告诉用户他没有选择复选框。

我将如何使用这两个参数编写错误处理异常?

import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.lang.Integer.*;
import java.lang.Object.*;
import java.util.*;
import java.util.Random;
import java.io.*;
import java.text.*;
import java.text.DecimalFormat.*;

public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener
{
    int packageIndex;
    double price;
    double[] prices = {49.99, 39.99, 34.99, 99.99};

    DecimalFormat money = new DecimalFormat("$0.00");
    JLabel priceLabel = new JLabel("Total Price: "+price);
    JButton button = new JButton("Check Price");
    JComboBox packageChoice = new JComboBox();
    Font newFont = new Font("Helvetica", Font.BOLD, 14);
    JPanel pane = new JPanel();
    TextField text = new TextField(5);
    JButton accept = new JButton("Accept");
    JButton decline = new JButton("Decline");
    JCheckBox serviceTerms = new JCheckBox("I Agree to the Terms of Service.", false);
    JTextArea termsOfService = new JTextArea("This is a text area", 5, 10);

    public JFrameWithPanel()
    {
        super("JFrame with Panel");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(packageChoice);
        setContentPane(pane);
        setSize(250,250);
        setVisible(true);

        packageChoice.addItem("A+ Certification");
        packageChoice.addItem("Network+ Certification ");
        packageChoice.addItem("Security+ Certifictation");
        packageChoice.addItem("CIT Full Test Package");

        pane.add(button);
        button.addActionListener(this);

        pane.add(text);
        text.setEditable(false);
        text.setBackground(Color.WHITE);
        text.addActionListener(this);

        pane.add(termsOfService);
        termsOfService.setEditable(false);
        termsOfService.setBackground(Color.lightGray);

        pane.add(serviceTerms);
        serviceTerms.addItemListener(this);

        pane.add(accept);
        accept.addActionListener(this);

        pane.add(decline);
        decline.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        packageIndex = packageChoice.getSelectedIndex();
        price = prices[packageIndex];
        text.setText("$"+price);

        Object source = e.getSource();

        if(source == accept)
        {
            JOptionPane.showMessageDialog(this,"Thank you for choosing our tests. Enjoy!");
        }
        else if(source == decline)
        {
            System.exit(0);
        }
    }

    public void itemStateChanged(ItemEvent e)
    {
        int select = e.getStateChange();

        if(select == ItemEvent.DESELECTED)
        {
            JOptionPane.showMessageDialog(null,"Please agree to the terms of service.");
        }
        else
        {

        }
    }
}
4

1 回答 1

1

为什么不检查 isSelected 是否为真?

if (!serviceTerms.isSelected())
    JOptionPane.showMessageDialog(null, "You have not accepted the terms.");
于 2010-06-16T13:13:45.073 回答