1

Codé pour une case à cocher qui accepte les conditions d'utilisation et un bouton d'acceptation et de refus.ActionListener Gestion des événements

je besoin d'aide pour une gestion des exceptions qui

si l'utilisateur n'a pas la case à cocher comme SÉLECTIONNÉ puis quand ils ont frappé accepter, un message d'erreur se produit indiquant à l'utilisateur qu'il n'a pas sélectionné la case à cocher.

Comment coder une exception de gestion d'erreur en utilisant ces 2 arguments?

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

Qu'avez-vous si loin? – krock

+0

Quelle est exactement la question? – Pace

+0

Ouais, mais le seul problème est, comment pourrais-je faire une exception gérer la fenêtre d'alerte (JOptionPane) pour leur dire de cocher la case pour continuer oui. –

Répondre

1

Pourquoi ne vérifiez-vous pas que isSelected est vrai?

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

alors si (serviceTerms.isSelected = false) { JOptionPane.showMessageDialog (null, "Doit accepter."); } –