troubleshooting Question

error message window and ceasing execution

Avatar of urobins
urobins asked on
Java
2 Comments1 Solution308 ViewsLast Modified:
Is there a way to do a message window?  I am doing a project for my class and thought I would go above and beyond.  I would like to put in some error checking so if my value is less than 1 or greater than 99 I'd like to pop a window and cease executiong.  Is this possible?

package project2;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class ChangeWindow implements ActionListener  
{
      JFrame coinFrame;
    JPanel coinPanel,textPanel;
    JTextField amount,txtAmount;
    JLabel amountLabel;
    DisplayCoins coinWindow;
    JButton enterAmount;
    Graphics2D graphics;
    int quarters,dimes,nickels,pennies;
    int intCoins;
    String number;
   
      public void ChangeWIndow()
      {
      
            quarters= 0;
            dimes=0;
            nickels=0;
            pennies=0;
            
      }
      
      public void SetChange()
      {
                  
              // Create my window and set up the title
              coinFrame = new JFrame("Enter value between 1 and 99");
              coinFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              coinFrame.setSize(new Dimension(700, 700));
              
              // Create the coin panel
              coinPanel = new JPanel(new GridLayout(10, 10));
              textPanel = new JPanel(new GridLayout(10, 10));
              coinWindow = new DisplayCoins();
              
              //Adds the widgets to the panel
              addWidgets();
                           
              //Set the default button.
              coinFrame.getRootPane().setDefaultButton(enterAmount);
              
              //Add the panel to the window.
              coinFrame.getContentPane().add(coinPanel, BorderLayout.NORTH);
              coinFrame.getContentPane().add(coinWindow, BorderLayout.CENTER);
              coinFrame.getContentPane().add(textPanel, BorderLayout.SOUTH);

              //Display the window.
              coinFrame.pack();
              coinFrame.setVisible(true);
              
                    
      }
      
      // method to add widgets to the panel
       private void addWidgets() {
              //Create widgets.
              amount = new JTextField(2);
              amountLabel = new JLabel("Amount of change between 1 and 99", SwingConstants.HORIZONTAL);
              enterAmount = new JButton("Click to enter value");
              txtAmount = new JTextField(50);

              //Listen to events from the click button.
              enterAmount.addActionListener(this);

              //Add the widgets to the container.
              coinPanel.add(amount);
              coinPanel.add(amountLabel);
              coinPanel.add(enterAmount);
              textPanel.add(txtAmount);
              
              amountLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
              
          }
    //       Change number value to words
      private void setAmountString(int amount)
      {
            
            String[] numbers = {"zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven",
                        "Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
                        
            if (amount > 1 && amount <20)
            {
                  number = numbers[amount];            
            }
            else if (amount >19 && amount <30 )
            {                                    
                  number = "Twenty "+numbers[amount-20]+" Cents";                  
            }
            else if (amount >29 && amount <40 )
            {                                    
                  number = "Thirty "+numbers[amount-30]+" Cents";                  
            }
            else if (amount >39 && amount <50 )
            {                                    
                  number = "Fourty "+numbers[amount-40]+" Cents";                  
            }
            else if (amount >49 && amount <60 )
            {                                    
                  number = "Fifty "+numbers[amount-50]+" Cents";                  
            }
            else if (amount >59 && amount <70 )
            {                                    
                  number = "Sixty "+numbers[amount-60]+" Cents";                  
            }
            else if (amount >69 && amount <80 )
            {                                    
                  number = "Seventy "+numbers[amount-70]+" Cents";                        
            }
            else if (amount >79 && amount <90 )
            {                                    
                  number = "Eighty "+numbers[amount-80]+" Cents";                  
            }
            else if (amount >89 && amount <100 )
            {                                    
                  number = "Ninety "+numbers[amount-90]+" Cents";                  
            }
            
            
      }
      
      //Actions performed after you click the button
      public void actionPerformed(ActionEvent event)
      {
            //sets the number of quarters nickles dimes and pennies
            String value= amount.getText();
        intCoins = Integer.parseInt(value);
       
       
        //Determines the number of each coin
            quarters = intCoins/25;
        int tempValue = intCoins%25;
        dimes = tempValue/10;
        tempValue = tempValue%10;
        nickels = tempValue/5;
        tempValue= tempValue%5;
        pennies = tempValue;
       
       
       
        //turns the number into a string
        setAmountString(intCoins);
        txtAmount.setText(this.number);
        //Testing to check string
       
        graphics = (Graphics2D) coinFrame.getGraphics();
            
        //coinWindow.paintComponent(graphics);
        coinWindow.
       
       
      }
      
      public class DisplayCoins extends JPanel
      {
            /**
             *
             */
            private static final long serialVersionUID = 1L;
            
            
            
            public void paintComponent(Graphics2D graphics)
            {
                  
                  
              paintQuarters(quarters);             
              paintDimes(dimes);
              paintNickels(nickels);
              paintPennies(pennies);                                
            }
            private void paintQuarters(int quarters)
            {
                  int y =190;
                  int x = 50;
                  for (int i=1; i<=quarters;i++)
                  {
                        Coins c = Coins.QUARTER;
                        c.paint(graphics,y,x);
                        y=y+10;
                  }
                  
            }
            
            private void paintDimes(int dimes)
            {
                  int y =190;
                  int x=150 ;
                  
                  for (int i=1; i<=dimes;i++)
                  {
                        Coins c = Coins.DIME;
                        c.paint(graphics,y,x);
                        y=y+10;
                  }
            }
            private void paintNickels(int nickels)
            {
                  int y =190;
                  int x=225;
                  for (int i=1; i<=nickels;i++)
                  {
                        Coins c = Coins.NICKEL;
                        c.paint(graphics,y,x);
                        y=y+10;
                  }
            }
            private void paintPennies(int pennies)
            {
                  int y =190;
                  int x=310;
                  
                  for (int i=1; i<=pennies;i++)
                  {
                        Coins c = Coins.PENNY;
                        c.paint(graphics,y,x);
                        y=y+10;
                  }
            }
      }
      
}
---------------------------------------------------------------------------
package project2;

import java.awt.*;

public enum Coins {

      QUARTER("25",75,Color.lightGray),DIME("10",55,Color.lightGray),
      NICKEL("5",65,Color.lightGray.darker()),PENNY("1",60,Color.orange.darker());
      
      private Color color;
      private String value;
      private int size;
      
      private Coins (String value, int size,Color color)
      {
            this.color = color;
            this.value = value;
            this.size = size;
      }
      public int getSize()
      {
            return this.size;
      }
      
      public String getValue()
      {
            return this.value;
      }
      
      public Color getColor()
      {
            return this.color;
      }
      public void paint(Graphics2D g, int y, int x)
    {
            g.setStroke(new BasicStroke(6.0f));
        g.setColor(Color.black);
        g.drawOval(x, y, size, size);
        g.setColor(color);
        g.fillOval(x, y, size-2, size-2);
        g.setColor(Color.black);
        g.drawString(this.value, x+(this.getSize()/2)-8, y+(this.getSize()/2)+2);
   }
}
ASKER CERTIFIED SOLUTION
CPColin
Senior Java Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 2 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 2 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004