1

編碼爲同意服務條款和接受和拒絕按鈕的複選框。ActionListener事件處理

我需要幫助的異常處理

如果用戶不具有複選框爲選中,那麼當他們擊中接受,錯誤信息時,告訴他已經沒有選擇複選框用戶。

我將如何使用這兩個參數對錯誤處理異常進行編碼?

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 
     { 

     } 
    } 
} 
+0

到目前爲止你有什麼? – krock 2010-06-16 12:41:49

+0

問題到底是什麼? – Pace 2010-06-16 12:42:14

+0

是的,但唯一的問題是,我將如何使異常處理警報窗口(JOptionPane)告訴他們檢查複選框以繼續耶。 – 2010-06-16 12:54:18

回答

1

爲什麼不直接檢查isSelected爲true?

if (!serviceTerms.isSelected()) 
    JOptionPane.showMessageDialog(null, "You have not accepted the terms."); 
+0

所以if(serviceTerms.isSelected = false)JOptionPane.showMessageDialog(null,「Must accept。」); } – 2010-06-16 13:16:25