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 4 of 4

Thread: the program working fine .. the cancel button doesn't work

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default the program working fine .. the cancel button doesn't work

    hi guys

    this is my program .. If there is anyone could help me before 11:50 I will be so happy
    the last time to submit it tonight

    the program working fine .. the cancel button doesn't work
    I want the program to show message when I click ( cancel ) such as ( cancel button is working )

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
     
     
    public class Program3 {
     
        public static void main(String[] args) 
     
        {
     
            // declare variables
            int sum = 0;
            int max=0, min= 0;
            //  initialize it one time.
            boolean firstIteration = true;
     
     
            // the for loop
            for ( int i=1; i<=4; i++)
     
            {
                //each inputted number will stores here
                String userInput = JOptionPane
                    .showInputDialog("Enter an integer");
                // to converse the string to int
                int value = Integer.parseInt(userInput);
     
                if(firstIteration)
                {
                    // the variables
                    sum = value; 
                    max = value;
                    min = value;
                    firstIteration = false;
     
                }
                else
                {
                    //add to sum 
                    sum += value; 
                } 
     
                 //conditional statement
                //if inputted number is smaller than our min then exchange it.
                if(value < min)
                {
                    min = value; 
                }
     
                 //conditional statement
                //if inputted number is bigger than our max then exchange it.
                if(value > max)
                {
                    max = value; 
                }
        }
            // calculating the averege by extracted the sum by 4
            double averge = sum / 4.0;
     
               JOptionPane.showMessageDialog(null," Max is " + max + " \nMin is " + min + 
                                          "\nSum is " + sum + " \nAverage is " + averge
                                   , "Program 3", JOptionPane.PLAIN_MESSAGE);
     
     
     
        JButton button = new JButton("cancel");
        button.addActionListener(new ActionListener(){
     
     
     
                JButton button = new JButton();
                button.setVisible(true);
                button.setSize(250,250);
                button.setDefaultCloseOperation(JButton.EXIT_ON_CLOSE);
     
                JPanel p = new JPanel();
                JButton b1 = new JButton ("Action Listener");
                button.addActionListener(new ActionListener(){
     
                    public void actionPerformed(ActionEvent e)
                    {
                        JOptionPane.showMessageDialog(null, "cancel button clicked")
     
     
                });
     
                p.add(b1);
                button.add(p);
            }
        });
     
                System.exit(0); // program end
     
        }   
     
    }


  2. #2
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: the program working fine .. the cancel button doesn't work

    my out put has a cancel button .. why do I need to have other one ?? I read this is the inter net and I did the same but I feel I am missing my program up

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: the program working fine .. the cancel button doesn't work

    That code doesn't look as though it would compile - does it? There's no ; after showMessageDialog, and no closing curly bracket on the actionPerformed method. If your code doesn't compile, copy and paste the compiler output.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: the program working fine .. the cancel button doesn't work

    Quote Originally Posted by smasm View Post

    p.add(b1);
    button.add(p);

    }
    });

    System.exit(0); // program end

    }

    }[/CODE]
    Just to let you know, you're adding a button to a JPanel, then a JPanel to a button. Let me know how that works out!

    You also don't seem to have a Frame of any sort as far as I can tell. You're basically adding theoretical objects to more theoretical objects. Nothing will ever appear on screen.

    And Sean4u is right... this code probably won't compile.

Similar Threads

  1. Cancel button ..??
    By smasm in forum Loops & Control Statements
    Replies: 3
    Last Post: November 5th, 2011, 06:12 AM
  2. How to set cancel button ASAP please
    By smasm in forum Object Oriented Programming
    Replies: 1
    Last Post: October 1st, 2011, 08:29 AM
  3. Calculate Button Not Working
    By czerenborg in forum AWT / Java Swing
    Replies: 8
    Last Post: September 8th, 2011, 09:25 PM
  4. Code is working in Eclipse but when exported to Runnable Jar doesn't work
    By jjain.jitendra@gmail.com in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 24th, 2011, 07:12 AM
  5. My Clear Info button is not working with Screenshot
    By drkossa in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 15th, 2010, 07:06 AM