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: I have a logical error

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I have a logical error

    Hello i wanted to ask for help .
    here is my program :-
    import java.awt.Graphics;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Scanner;
     
    public class AGod   
    {
       public JFrame window = new JFrame("A. God");
       public JFrame window1 = new JFrame("A. God");
       public JLabel Text1 = new JLabel("Choose your Colour :-",JLabel.CENTER);
       public JButton button2 = new JButton("Heavenly Blue");
       public JButton button3 = new JButton("Deadly Red");
       public JButton button4 = new JButton("Cutey Pink");
       public JButton button5 = new JButton("Simple Black");
       public Scanner scan = new Scanner(System.in);
     
       public AGod() 
       {
           window.setSize(700,400);
           window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           window.setLayout(new GridLayout(1,4));
           window.setBackground(Color.gray);
           Text1.setToolTipText("Doesn\'t matter much ...");
     
           window.add(Text1);
     
           window.add(button2);
           window.add(button3);
           window.add(button4);
           window.add(button5);
     
     
           //button2.addActionLisetener(this);
           //button3.addActionLisetener(this);
           //button4.addActionLisetener(this);
           //button5.addActionLisetener(this);
     
           window.setVisible(true);
        }
       public class blah 
       {
         Graphics g;
         public blah()
         {
     
             window1.setSize(700,400);
             window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             g.drawRect (20, 30, 300, 100);
             g.drawRect (20, 60, 300, 20);
             window1.setVisible(true);
            }
         public void actionPerformed(ActionEvent a)
         {
           if (a.getSource() == button2)
           {
             new blah();
     
     
            }
         }
     
       }
     
       public static void main(String[] args)
      {
        new AGod();
      }
    }
    i wanted to display the new blah() if we click on button2 but it is not happening


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: I have a logical error

    Hi n00b,

    You need to read about action listeners, you have completely skipped them. You have not linked your buttons to an action listener, an thus are not able to perform any actions when the buttons are clicked.

    Introduction to Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    The above link discusses the use of action listeners.

    Regards,
    Chris

Similar Threads

  1. Sum of series program logical error
    By Neel0075 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2011, 11:26 AM
  2. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 06:26 PM
  3. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 01:17 AM
  4. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 07:58 AM