Code Error Java?
public class Main {
public static void main(String[] args) {


    GUI gui = new GUI();
    
    }
    
   public static int fak (int n) {
        if (n == 0) {
            return 1;
        }
        return n * fak(n - 1);
    }
    
}

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class GUI extends JFrame {
    JButton button = new JButton("Berechnen");
    JLabel label = new JLabel("Bitte geben Sie die Zahl ein: ");
    JTextField textfield = new JTextField(2);
    JOptionPane popup = new JOptionPane();
    JPanel panel = new JPanel();
    
    public GUI () {
        setSize(300, 300);
        setTitle("Fakultätsrechner");
        setLayout(new FlowLayout());
        setVisible(true);
        
        panel.add(textfield);
        panel.add(label);
        panel.add(button);
        add(panel);
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);


        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    String input = textfield.getText();
                    int number = Integer.parseInt(input);
                    int result = Main.fak(number);
                    JOptionPane.showMessageDialog(null, "Die Fakultät von " + number + " ist " + result);
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "Bitte geben Sie eine gültige Ganzzahl ein.");
                }
            }
        });
        
    }





}

Error: java.lang.ClassNotFoundException: Main

Aber wieso findet er denn die Main-Klasse nicht? Verstehe ich nicht.

App, Java, Minecraft, Array, Code, Eclipse, Minecraft Server, Programmiersprache, Swing, game-development, Bukkit, Spigot, Java Swing, Android Studio
GUI in Taschenrechner integrieren (Java)?
import java.util.Scanner;
import javax.swing.JTextField;
public class App {
    public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);


        new GUI();
        
        try {
        System.out.println("Bitte geben Sie ihre Berechnung ein: ");
        String eingabe = scanner.nextLine();
        String[] teile = eingabe.split(" ");
        
        double zahl1 = Double.parseDouble(teile[0]);
        double zahl2 = Double.parseDouble(teile[2]);
        char operator = teile[1].charAt(0);
        
        System.out.println(taschenrechner(eingabe, zahl1, zahl2, operator));
        
    } catch (NumberFormatException e) {
            System.out.println("Bitte geben Sie einen gültigen Wert ein!");
        } finally {
            scanner.close();
        }
    }
    
    static double taschenrechner (String eingabe, double zahl1, double zahl2, char operator) {


        double ergebnis = 0.0;


        if (operator == '/' && zahl2 == 0) {
            throw new ArithmeticException("Das dividieren durch 0 ist nicht erlaubt!");
        }
        
        switch (operator) {
            case '-': ergebnis = zahl1 - zahl2; break;
            case '+': ergebnis = zahl1 + zahl2 ; break;
            case '*': ergebnis = zahl1 * zahl2; break;
            case '/': ergebnis = zahl1 / zahl2; break;
        }
            return ergebnis;
    }    
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;


public class GUI extends App{


    private JTextField textFieldZahl1;
    private JTextField textFieldZahl2;
    private JTextField textFieldOperator;
    private JLabel labelErgebnis;
    private JButton buttonBerechnen;
    
    public GUI () {


    JFrame frame = new JFrame("Taschenrechner");
        frame.setLayout(new FlowLayout());
        frame.setSize(500, 500);
        frame.add(new JButton("+"));
        frame.add(new JButton("-"));
        frame.add(new JButton("*"));    
        frame.add(new JButton("/"));
        frame.add(new JTextField(20));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Wie kann ich die GUI jetzt mit der Taschenrechner-Logik verknüpfen?

Java, Code, Eclipse, Programmiersprache, Algorithmus, Java Swing

Meistgelesene Beiträge zum Thema Programmiersprache