Results 1 to 10 of 10
Thread: Swing applet problem
- 05-24-2011, 03:06 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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(); } }); } }
- 05-24-2011, 03:16 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
should be:Java Code:public void main(String[] args)
Java Code:public static void main(String[] args)
- 05-24-2011, 03:25 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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
-
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?
- 05-24-2011, 03:29 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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
- 05-24-2011, 03:35 AM #7
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.
- 05-24-2011, 03:41 AM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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)
- 05-24-2011, 04:31 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
I tried what Norm said and when I ran it, nothing happened. It didn't produce any error. It just didn't do anything.
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.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); } }
- 05-24-2011, 02:31 PM #10
Similar Threads
-
Convert THE APPLET CODE TO SWING USING JPANEL
By jammyjamsticy in forum AWT / SwingReplies: 2Last Post: 12-22-2011, 09:16 AM -
Snowman Applet - SWING
By Angry@Java in forum New To JavaReplies: 1Last Post: 05-05-2011, 02:19 AM -
Change Applet to Swing
By aryowap in forum AWT / SwingReplies: 4Last Post: 07-08-2010, 09:03 PM -
starting a Swing application from an applet
By FakeRabbit in forum Java AppletsReplies: 9Last Post: 09-21-2008, 07:00 PM -
Create Windows, Applet & Swing
By Eric in forum AWT / SwingReplies: 1Last Post: 07-05-2007, 06:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks