“意外类型”错误

时间:2010-06-16 13:27:19

标签: java compilation

我在编译Java程序时遇到问题:

\Desktop\Java Programming\JFrameWithPanel.java:79: unexpected type
required: variable
found   : value
            if(serviceTerms.isSelected() = false)
                                      ^
1 error

导致此错误的原因是什么?

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();
    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)
        {
            if(serviceTerms.isSelected() = false) // line 79
            {
                JOptionPane.showMessageDialog(null,"Please accept the terms of service.");
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Thanks.");
            }
        }
    }

2 个答案:

答案 0 :(得分:8)

您有(非法)作业而非比较。当然你的意思是:

if (serviceTerms.isSelected() == false)

当然,编写此条件的优先且更易读的方法是:

if (!serviceTerms.isSelected()) 

答案 1 :(得分:4)

问题是您使用的是赋值运算符=。您应该使用的是等于运算符==,如下所示:

if(serviceTerms.isSelected() == false) // line 79
{
    JOptionPane.showMessageDialog(null,"Please accept the terms of service.");
}

或者,为了完全绕过这个错误,你应该跳过与false的比较并使用!(非)运算符。

if(!serviceTerms.isSelected()) // line 79
{
    JOptionPane.showMessageDialog(null,"Please accept the terms of service.");
}

我觉得这样读起来更好。

  

如果没有,则选择服务条款

这更像是一句话而不是:

  

如果选择服务条款等于假