java - "Unexpected type"错误

标签 java compilation

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

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

What is causing this 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.");
            }
        }
    }

最佳答案

您有一个(非法)赋值而不是比较。你的意思肯定是:

if (serviceTerms.isSelected() == false)

当然,更可读的优先方式是写这个条件:

if (!serviceTerms.isSelected()) 

关于java - "Unexpected type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3053627/

相关文章:

c - 流程的完整状态

java - 二维数组方法构建

java - 如何在没有应用程序小部件 Activity 的情况下注册广播接收器?

c - 特殊函数 _init 和 _fini 的段错误

makefile - Make 中的默认规则

java - 为什么我的 Java 类在从包目录内部编译时无法编译?

java - 获取封闭框架的索引

java - 如何调试这个Makefile

Java继承和重写方法——修饰符的影响

.NET : How to use JIT compiler