S
S
Simasik2015-02-18 20:49:29
Java
Simasik, 2015-02-18 20:49:29

Why can't the program start?

There is a code written in java:

import java.awt.*;
import java.awt.event.*;
class Party{
  public void buildInvite(){
    Frame f = new Frame();
    Label l= new Label("Вечеринка у Тими");
    Button b = new Button("Ваша ставка");
    Button c = new Button("Сбросить");
    Panel p = new Panel();
    p.add(l);
    
  }
}

The code was compiled, but when executing the "java Party" command through the console, it gives an error. What to do?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
IceJOKER, 2015-02-18
@IceJOKER

Learn to understand what the console gives out, if there is not enough knowledge, then write here what it gives out.
the entry point in java is the main method, if the code above is the only thing in the project, then that's the problem.
If you don't know how to write, you can just watch the video tutorials.
Yes, and you can drive the error code into the Internet, this method of solving the problem has never failed

V
Vyacheslav Tutrinov, 2015-02-18
@zhulikovatyi

As far as I know, the Java class we want to run must have an entry point in the form of a main method

class MyClass {

    public static void main(String[] args) {
        // call buiIdInvite method
    }

}

B
booogabooo, 2015-02-18
@booogabooo

+ to all this Panel needs to be added to Frame
f.getContentPane().add(p);

A
asd111, 2015-02-19
@asd111

Look at some book on Swing or on JavaFX.
Tutorial on Swing docs.oracle.com/javase/tutorial/uiswing/index.html
Tutorial on JavaFX docs.oracle.com/javase/8/javase-clienttechnologies.htm
Below is a HelloWorld JavaFX example

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class Party extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);

 Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
 public static void main(String[] args) {
        launch(args);
    }
}

I
Igor Kalashnikov, 2015-02-19
@zo0m

Why are you trying to do something without understanding the basic things and counting on success?
1. Read at least the beginning of a Java book.
2. Run a program that will print you Hello, world.
3. Piece by piece, drag your code into the program and recompile each time and wait until it starts, as soon as it doesn't start - that's your problem.
Alternative way for the lazy:
1. Google: "awt examples in java"
2. Literally the third link: www.tutorialspoint.com/javaexamples/gui_text.htm
3. There is the code:

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel{
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);
      Font font = new Font("Serif", Font.PLAIN, 96);
      g2.setFont(font);
      g2.drawString("Text", 40, 120);
   }
   public static void main(String[] args) {
      JFrame f = new JFrame();
      f.getContentPane().add(new Main());
      f.setSize(300, 200);
      f.setVisible(true);
   }
}

this is where to start tinkering.
I have not seen AWT for a long time, but it seems to me that you need to inherit the Party class from JPanel as in the example above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question