Results 1 to 10 of 10
  1. #1
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Swing applet problem

    The program compiles fine but then when I type it in exactly the way the class file is saved is says Exception in thread "main" java.lang.nosuchmethoderror: main
    Java Code:
    /*
    *This program was written by Josh Mueller
    *
    *The purpose of this program is to open up a GUI window with
    *a question and two buttons. The program will count the number
    *of yes and no responses like a vote.
    */
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ButtonCount
    {
    	public int Count1 = 0;
    	public int Count2 = 0;
    	JLabel label3;
    	JLabel label4;
    	public void createAndShowGUI()
    	{
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		JFrame frame = new JFrame("Vote!");//Creating a frame and naming it
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		panel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));//Creating a border around the frame
    		JLabel label1 = new JLabel("<html><font size=2 color=green face=sherwood>Click on your vote to the following question<br>and it will count the number of votes</font></html>");
    		label1.setBounds(40,30,100, 50);
    		frame.add(label1);
    		JLabel label2 = new JLabel("<html><font size=5 color=green face=sherwood>Do you like writing programs?</font></html>");
    		label2.setBounds(30,60,100, 50);
    		frame.add(label2);
    		JButton button1 = new JButton("<html><font face=sherwood><h3>YES</h3></font></html>");
    		button1.setBounds(30,100,100,100);
    		button1.setBackground(Color.green);
    		button1.addActionListener(new ButtonListener1());
    		/*This line adds the listener to the button so the program
    		will know when it is clicked. Later on, in the code to 
    		count the number of clicks, it will be implemented */
    		frame.add(button1);
    		JButton button2 = new JButton("<html><font face=sherwood><h3>NO</h3></font></html>");
    		button2.setBounds(135,100,100,100);
    		button2.setBackground(Color.red);
    		button2.addActionListener(new ButtonListener2());
    		frame.add(button2);
    		JLabel label3 = new JLabel("Number of Yes Votes:");
    		label3.setBounds(20,225,50,10);
    		frame.add(label3);
    		JLabel label4 = new JLabel("Number of No Votes:");
    		label4.setBounds(130,225,50,10);
    		frame.add(label4);
    		frame.pack();
    		frame.setVisible(true);
    	}
    	private class ButtonListener1 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent push)
    		{
    			Count1++;
    			label3.setText("Number of Yes Votes: " + Count1);
    		}
    	}
    	private class ButtonListener2 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent push)
    		{
    			Count2++;
    			label4.setText("Number of No Votes: " + Count2);
    		}
    	}
    	public void main(String[] args) 
    	{
            	javax.swing.SwingUtilities.invokeLater(new Runnable() 
    		{
                	public void run() 
    		{
                    	createAndShowGUI();
                	}
            });
        }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Java Code:
    public void main(String[] args)
    should be:

    Java Code:
    public static void main(String[] args)

  3. #3
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    I have done that but then it says cannot run non-static createAndshowGUI() method.
    If I try public static void createAndShowGUI() in the beginning then the compiler doesn't allow me to create a button listener

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

    Default

    I'm confused. Your title states "applet" problem, and I see no applet-related code in your post. Is it supposed to behave as an applet or as a stand-alone program?

  5. #5
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    once it is created it is supposed to open in an html document but it is being wierd

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

    Default

    Quote Originally Posted by link6790 View Post
    once it is created it is supposed to open in an html document but it is being wierd
    There's nothing weird about it -- it's not an applet and so won't work as intended. Before going further you'd best read a tutorial on how to make JApplets before proceeding. While you're at it, you'll want to read some general Java tutorials as well. You can start here: really big index

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    An idea: Move the code out of the main method into a constructor for ButtonCount so all of the code with be within an instance of the class and not in the static methods. Have main method create the ButtonCount object.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Read the Swing tutorial for working examples on how to create a simple program. It shows the proper way to use a "createAndShow()" method.

    Maybe start with the "LabelDemo" from: How to Use Labels (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  9. #9
    link6790 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    I tried what Norm said and when I ran it, nothing happened. It didn't produce any error. It just didn't do anything.
    Java Code:
    /*
    *This program was written by Josh Mueller
    *
    *The purpose of this program is to open up a GUI window with
    *a question and two buttons. The program will count the number
    *of yes and no responses like a vote.
    */
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ButtonCount
    {
    	public int Count1 = 0;
    	public int Count2 = 0;
    	JLabel label3;
    	JLabel label4;
    	public static void main(String[] args) 
    	{
            	ButtonCount BC = new ButtonCount();
            }
    	public void ButtonCount()
    	{
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		JFrame frame = new JFrame("Vote!");//Creating a frame and naming it
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		panel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));//Creating a border around the frame
    		JLabel label1 = new JLabel("<html><font size=2 color=green face=sherwood>Click on your vote to the following question<br>and it will count the number of votes</font></html>");
    		label1.setBounds(40,30,100, 50);
    		frame.add(label1);
    		JLabel label2 = new JLabel("<html><font size=5 color=green face=sherwood>Do you like writing programs?</font></html>");
    		label2.setBounds(30,60,100, 50);
    		frame.add(label2);
    		JButton button1 = new JButton("<html><font face=sherwood><h3>YES</h3></font></html>");
    		button1.setBounds(30,100,100,100);
    		button1.setBackground(Color.green);
    		button1.addActionListener(new ButtonListener1());
    		/*This line adds the listener to the button so the program
    		will know when it is clicked. Later on, in the code to 
    		count the number of clicks, it will be implemented */
    		frame.add(button1);
    		JButton button2 = new JButton("<html><font face=sherwood><h3>NO</h3></font></html>");
    		button2.setBounds(135,100,100,100);
    		button2.setBackground(Color.red);
    		button2.addActionListener(new ButtonListener2());
    		frame.add(button2);
    		JLabel label3 = new JLabel("Number of Yes Votes:");
    		label3.setBounds(20,225,50,10);
    		frame.add(label3);
    		JLabel label4 = new JLabel("Number of No Votes:");
    		label4.setBounds(130,225,50,10);
    		frame.add(label4);
    		frame.pack();
    		frame.setVisible(true);
    		javax.swing.SwingUtilities.invokeLater(new Runnable() 
    		{
                	public void run() 
    		{
                    	ButtonCount();
                	}
            });
    	}
    	private class ButtonListener1 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent push)
    		{
    			Count1++;
    			label3.setText("Number of Yes Votes: " + Count1);
    		}
    	}
    	private class ButtonListener2 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent push)
    		{
    			Count2++;
    			label4.setText("Number of No Votes: " + Count2);
    		}
    	}
    I also tried altering the code to fit the extends JApplet package but when the html document opens it is just a grey pane with nothing showing. I can post the code for it if you want.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Add some printlns to the main method and to the constructor to show what code is being executed.

Similar Threads

  1. Convert THE APPLET CODE TO SWING USING JPANEL
    By jammyjamsticy in forum AWT / Swing
    Replies: 2
    Last Post: 12-22-2011, 09:16 AM
  2. Snowman Applet - SWING
    By Angry@Java in forum New To Java
    Replies: 1
    Last Post: 05-05-2011, 02:19 AM
  3. Change Applet to Swing
    By aryowap in forum AWT / Swing
    Replies: 4
    Last Post: 07-08-2010, 09:03 PM
  4. starting a Swing application from an applet
    By FakeRabbit in forum Java Applets
    Replies: 9
    Last Post: 09-21-2008, 07:00 PM
  5. Create Windows, Applet & Swing
    By Eric in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 06:36 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
  •