我在编译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.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
你有(非法)任务而不是比较.当然你的意思是:
if (serviceTerms.isSelected() == false)
Run Code Online (Sandbox Code Playgroud)
当然,编写此条件的优先且更易读的方法是:
if (!serviceTerms.isSelected())
Run Code Online (Sandbox Code Playgroud)
问题是您正在使用赋值运算符=。您应该使用的是如下所示的相等运算符==:
if(serviceTerms.isSelected() == false) // line 79
{
JOptionPane.showMessageDialog(null,"Please accept the terms of service.");
}
Run Code Online (Sandbox Code Playgroud)
或者,要完全绕过这个错误,您应该跳过比较false并!像这样使用(not) 运算符。
if(!serviceTerms.isSelected()) // line 79
{
JOptionPane.showMessageDialog(null,"Please accept the terms of service.");
}
Run Code Online (Sandbox Code Playgroud)
我觉得这种方式读起来更好。
如果不是,则选择服务条款
这更像是一个句子,而不是:
如果选择服务条款等于 false
| 归档时间: |
|
| 查看次数: |
3283 次 |
| 最近记录: |