Results 1 to 4 of 4
  1. #1
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Question Swing GUI Thread Error?

    I am trying to create gui in swing which has 3 buttons which ate start quit instructions i created my swing gui but i have no idea why is here a error in the program The error is about


    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method add(PopupMenu) in the type Component is not applicable for the arguments (JButton)
    The method add(PopupMenu) in the type Component is not applicable for the arguments (JButton)
    The method add(PopupMenu) in the type Component is not applicable for the arguments (JButton)
    Action cannot be resolved to a type
    at Samurai.Samurai.main(Samurai.java:27)


    there something wrong with the frame.add(); i think please have a look

    Java code:

    package Samurai;
    import java.util.Scanner;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;

    import java.awt.Component;
    import java.awt.event.*;
    public class Samurai {

    /**
    * @param args
    */
    public static JFrame frame= new JFrame ("Samurai Game By Dharav");
    public static void main(String[] args) {


    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(300,300);
    JPanel pane1=new JPanel();
    Component panel;
    frame.add(panel);
    JButton button=new JButton("Start");
    JButton button2=new JButton("Quit");
    JButton button3=new JButton("Instructions");
    panel.add(button);
    panel.add(button2);
    panel.add(button3);
    button.addActionListener(new Action());
    button2.addActionListener(new Action2());
    button3.addActionListener(new Action3());
    }

    static class Action3 implements ActionListener{
    public void actionPerformed(ActionEvent e){
    frame.setVisible(false);

    System.out.println("Follow the instructions on screen.\n When asked for a answer please use Y for yes or N for no. \n Also to explore Press Enter.")

    }
    }
    static class Action2 implements ActionListener{
    public void actionPerformed(ActionEvent e){
    frame.setVisible(false);
    System.exit(0);
    }

    //VARIABLES ONLY SECTION

    String nameofwarrior,cityname,hit,faith;

    int nameofcity,cityexplore,i,k,villan,boss;

    int Mob1[]={1,1},Player[]={0,0},ninjalord[]={24,4};
    String astricks[]= {"*","**","***","****","*****","******","****** *", "********","*********","**********","********* **", "************","*************","************** ","* **************","****************","************** ***"};
    String chooseyourdestiny[]= {"C","H","O","O","S","E"," ","Y","O","U","R"," ","D","E","S","T","I","N","Y"};


    //VARIABLES ONLY SECTION ENDS

    Scanner keyboard = new Scanner (System.in);

    System.out.println("Hey The Ol'e Wise Man Wishes to Know your NAME");

    nameofwarrior=keyboard.nextLine();

    System.out.println("*.**.* YOU ARE THE CHOOSEN ONE "+nameofwarrior+" *.**.* \n DESTINY AWAITS YOU ON YOUR JOURNEY ");
    try
    {
    Thread.sleep(1000); // a simple timer of 1 second (1000 milliseconds)
    }
    catch (InterruptedException e)
    {}
    System.out.println("You are Choosen TO be the one");
    try
    {
    Thread.sleep(3000); // a simple timer of 1 second (1000 milliseconds)
    }
    catch (InterruptedException e)
    {}
    System.out.println("Please Wait Prepare your self Relax You are About to become a Warrior "+nameofwarrior+", in the 15th Century");
    try
    {
    Thread.sleep(7000); // a simple timer of 1 second (1000 milliseconds)
    }
    catch (InterruptedException e)
    {}
    for (i=0;i<=16;i++)
    {

    System.out.println(astricks[i]);

    try
    {
    Thread.sleep(100); // a simple timer of 0.1 second (100 milliseconds)
    }
    catch (InterruptedException e)
    {}

    }
    }}

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default Re: Swing GUI Thread Error?

    1) first of all you can not add a jbutton to your component panel. so change the type from Component to JPanel and the compiler will be happy.
    2) In java all statements must end with a semicolon. The first System.out.println hasn't one.
    3) In button.addActionListener(new Action()) the class Action is not found.
    4) There is a bunch of other erros.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Swing GUI Thread Error?

    Quote Originally Posted by j2me64 View Post
    1) first of all you can not add a jbutton to your component panel. so change the type from Component to JPanel and the compiler will be happy.
    Yeah, mixing of awt and swing should also be avoided! But another problem here is, that you don`t instantiate the component/JComponent - so you will get a NPE in the next row :)

    And you should look at your brackets. Some statements are at the wrong place and have to move! Reformat your code :P

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Thread v swing timer
    By musico in forum New To Java
    Replies: 1
    Last Post: 06-23-2011, 09:51 PM
  2. Prompt for value in swing thread
    By dataphile in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-05-2010, 12:50 AM
  3. JComboBox ActionListener and Swing Thread safety
    By frenk_castle in forum AWT / Swing
    Replies: 6
    Last Post: 04-05-2010, 10:02 AM
  4. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  5. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •