Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with ExitButtonHandler

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with ExitButtonHandler

    Im trying to make a program so that the user has to enter a number for the width and length and it will give the area and perimeter:


    import java.util.* ;

    import javax.swing.*;

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;



    public class jframe extends JFrame {

    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;

    private JLabel lengthL, widthL, areaL, perimeterL;
    private JTextField lengthTF, widthTF, areaTF, perimeterTF;

    private CalculateButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    private JButton calculateB, exitB;

    public jframe () {

    lengthTF = new JTextField(10);
    widthTF = new JTextField(10);
    areaTF = new JTextField(10);
    perimeterTF = new JTextField(10);

    lengthL = new JLabel ("Enter the length: ", SwingConstants.RIGHT);
    widthL = new JLabel ("Enter the width: ", SwingConstants.RIGHT);
    areaL = new JLabel ("Area = ", SwingConstants.RIGHT);
    perimeterL = new JLabel ("Perimeter = ", SwingConstants.RIGHT);

    calculateB = new JButton("Calculate");
    cbHandler = new CalculateButtonHandler();
    calculateB.addActionListener(cbHandler);

    exitB = new JButton("Exit");
    ebHandler = new ExitButtonHandler();
    exitB.addActionListener(ebHandler);


    Container pane = getContentPane();
    pane.setLayout(new GridLayout (5,2));
    pane.add(lengthL);
    pane.add(lengthTF);
    pane.add(widthL);
    pane.add(widthTF);
    pane.add(areaL);
    pane.add(areaTF);
    pane.add(perimeterL);
    pane.add(perimeterTF);
    pane.add(calculateB);
    pane.add(exitB);

    setTitle("Area and perimeter of rectangle");
    setSize(WIDTH,HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private class CalculateButtonHandler implements ActionListener{

    public void actionPerformed(ActionEvent e) {
    double width, length, area, perimeter;

    length = Double.parseDouble(lengthTF.getText());
    width = Double.parseDouble(widthTF.getText());
    area = length*width;
    perimeter = 2 *(length+width);

    areaTF.setText("" + area);
    perimeterTF.setText("" + perimeter);


    }

    private class ExitButtonHandler implements ActionListener{

    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }

    }


    }

    public static void main (String [] args){

    jframe rectObject = new jframe();


    }

    }

    It is giving me an error saying that the ExitButtonHandler and ebHandler do not have classes but I don't understand why. Any help would be awesome on how to make this better.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with ExitButtonHandler

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly. You can edit your post and fix it.

    Class ExitButtonHandler is a private inner class of CalculateButtonHandler which is a private inner class of jframe. (Java's naming convention is that class names begin with capital letters, variables and method names begin with lowercase letters. Please follow Java's naming convention.) Move ExitButtonHandler outside CalculateButtonHandler, and that error should go away.