I can't understand the error.
When i try running my program i get this error:
java.lang.NoSuchMethodError: main
Exception in thread "main" Java Result: 1
This is my code:
Code Java:
package button_game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button_Game {
static int k;
private static class A1 implements ActionListener{
public void actionPerformed(ActionEvent e){
k = 1;
}
private static class B1 implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
//addComponentsToPane positions the buttons on the screen randomly
public static void addComponentsToPane(Container pane){
int r = 1;
while (r == 1)
{ r = r +1;
int x;
int y;
x = (int)(200*Math.random());
y = (int)(200*Math.random());
pane.setLayout(null);
int a = (int)(1000*Math.random());
int b = a%2;
if (b == 0){
String c = Integer.toString(a);
JButton A2 = new JButton(c);
JButton B2 = new JButton();
A1 listener = new A1();
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
Insets insets = pane.getInsets();
Dimension size = B2.getPreferredSize();
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
else if(b != 0);
int c = a +1;
String d = Integer.toString(c);
JButton A2 = new JButton(d);
JButton B2 = new JButton();
A1 listener = new A1();
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
Insets insets = pane.getInsets();
Dimension size = B2.getPreferredSize();
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
while (k == 1){
x = (int)(200*Math.random());
y = (int)(200*Math.random());
pane.setLayout(null);
if (b == 0){
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
else if(b != 0);
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
}
}
//this runs the game
private static void runGUI(){
JFrame window = new JFrame("Button Game");
addComponentsToPane(window.getContentPane());
window.setSize(300,300);
window.setVisible(true);
}
//this part runs the subroutine which runs the game
public static void main(String Args[]){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
runGUI();
}
});
}
}
}
Re: I can't understand the error.
You have to compile your code first. Since it doesn't compile you will need to fix the errors first before you can run your program.
Re: I can't understand the error.
what are the errors? forgive me for being a noob at java programming
Re: I can't understand the error.
I'm not a compiler. The compiler is better suited to tell you what they are and where they are. Does your code compile?
The error I thought was there was due to the browser not correctly displaying the underscore in your class name.
Re: I can't understand the error.
I can see a main method in this code and it compiled fine for me in Eclipse. It pops up the GUI with 2 buttons on but they don't do anything.
Re: I can't understand the error.
This looks like a continuation of a problem the OP posted on another thread. I think that The posted code is not what he was executing. The code from the other thread has a call to the main() which was left out of the code posted here. If any of you had your crystal balls working you would have known that.
Re: I can't understand the error.
main method defined in the Inner subclass.... check it out once
Quote:
Originally Posted by
Shivam24
When i try running my program i get this error:
java.lang.NoSuchMethodError: main
Exception in thread "main" Java Result: 1
This is my code:
Code Java:
package button_game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button_Game {
static int k;
private static class A1 implements ActionListener{
public void actionPerformed(ActionEvent e){
k = 1;
}
private static class B1 implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
//addComponentsToPane positions the buttons on the screen randomly
public static void addComponentsToPane(Container pane){
int r = 1;
while (r == 1)
{ r = r +1;
int x;
int y;
x = (int)(200*Math.random());
y = (int)(200*Math.random());
pane.setLayout(null);
int a = (int)(1000*Math.random());
int b = a%2;
if (b == 0){
String c = Integer.toString(a);
JButton A2 = new JButton(c);
JButton B2 = new JButton();
A1 listener = new A1();
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
Insets insets = pane.getInsets();
Dimension size = B2.getPreferredSize();
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
else if(b != 0);
int c = a +1;
String d = Integer.toString(c);
JButton A2 = new JButton(d);
JButton B2 = new JButton();
A1 listener = new A1();
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
Insets insets = pane.getInsets();
Dimension size = B2.getPreferredSize();
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
while (k == 1){
x = (int)(200*Math.random());
y = (int)(200*Math.random());
pane.setLayout(null);
if (b == 0){
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
else if(b != 0);
A2.addActionListener(listener);
B2.addActionListener(listener);
pane.add(B2);
pane.add(A2);
B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
size = A2.getPreferredSize();
A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
}
}
}
//this runs the game
private static void runGUI(){
JFrame window = new JFrame("Button Game");
addComponentsToPane(window.getContentPane());
window.setSize(300,300);
window.setVisible(true);
}
//this part runs the subroutine which runs the game
public static void main(String Args[]){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
runGUI();
}
});
}
}
}