Hallo,
wie kann ich mit Javafx einen butten erstellen bzw anzeigen?
Ich habe schon ein bisschen rumprobiert allerdings hat nichts geklappt, zumindest nicht wenn ich den button außerhalb der Gui erstellen wollte. Der Code:
Gui:
[ code= Java]package gui;[/code]
import java.util.List;
import actions.KeyPressed;
import actions.KeyReleased;
import actions.MouseDragged;
import actions.MouseMoved;
import actions.MousePressed;
import actions.MouseReleased;
import draw.Draw_Main;
import game.Gamestate;
import game.Gamestate_e;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.stage.*;
public class Gui {
public static Draw_Main dm;
public static Button button_shop;
public static int width = 1280, height = 720;
public static GraphicsContext gc_main;
public void init() {
dm = new Draw_Main();
}
public void create(Stage stage) {
Canvas canvas_main;
StackPane root = new StackPane();
int cWidth = width -10, cHeight = height - 10;
canvas_main = new Canvas(width, height);
gc_main = canvas_main.getGraphicsContext2D();
dm.draw(gc_main);
root.getChildren().add(canvas_main);
Scene scene = new Scene(root, cWidth, cHeight);
scene.setOnKeyPressed(new KeyPressed());
scene.setOnKeyReleased(new KeyReleased());
scene.setOnMouseMoved(new MouseMoved());
scene.setOnMouseDragged(new MouseDragged());
scene.setOnMousePressed(new MousePressed());
scene.setOnMouseReleased(new MouseReleased());
stage.setTitle("Space Adventure");
stage.setScene(scene);
stage.setResizable(false);
stage.centerOnScreen();
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Platform.exit();
System.exit(0);
}
});
}
}
Die andere Klasse(Pause):
public class Pause {
public static void Pause_btn(GraphicsContext g) {
if(Gamestate.state == Gamestate_e.pause) {
Gui.button_shop = new Button("Hallo");
Gui.button_shop.setMaxHeight(40);
Gui.button_shop.setMaxWidth(300);
Gui.button_shop.setVisible(true);
}
}
}
Wäre cool wenn mir wer helfen könnte
wie kann ich mit Javafx einen butten erstellen bzw anzeigen?
Ich habe schon ein bisschen rumprobiert allerdings hat nichts geklappt, zumindest nicht wenn ich den button außerhalb der Gui erstellen wollte. Der Code:
Gui:
[ code= Java]package gui;[/code]
import java.util.List;
import actions.KeyPressed;
import actions.KeyReleased;
import actions.MouseDragged;
import actions.MouseMoved;
import actions.MousePressed;
import actions.MouseReleased;
import draw.Draw_Main;
import game.Gamestate;
import game.Gamestate_e;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.stage.*;
public class Gui {
public static Draw_Main dm;
public static Button button_shop;
public static int width = 1280, height = 720;
public static GraphicsContext gc_main;
public void init() {
dm = new Draw_Main();
}
public void create(Stage stage) {
Canvas canvas_main;
StackPane root = new StackPane();
int cWidth = width -10, cHeight = height - 10;
canvas_main = new Canvas(width, height);
gc_main = canvas_main.getGraphicsContext2D();
dm.draw(gc_main);
root.getChildren().add(canvas_main);
Scene scene = new Scene(root, cWidth, cHeight);
scene.setOnKeyPressed(new KeyPressed());
scene.setOnKeyReleased(new KeyReleased());
scene.setOnMouseMoved(new MouseMoved());
scene.setOnMouseDragged(new MouseDragged());
scene.setOnMousePressed(new MousePressed());
scene.setOnMouseReleased(new MouseReleased());
stage.setTitle("Space Adventure");
stage.setScene(scene);
stage.setResizable(false);
stage.centerOnScreen();
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Platform.exit();
System.exit(0);
}
});
}
}
Die andere Klasse(Pause):
public class Pause {
public static void Pause_btn(GraphicsContext g) {
if(Gamestate.state == Gamestate_e.pause) {
Gui.button_shop = new Button("Hallo");
Gui.button_shop.setMaxHeight(40);
Gui.button_shop.setMaxWidth(300);
Gui.button_shop.setVisible(true);
}
}
}
Wäre cool wenn mir wer helfen könnte
Zuletzt bearbeitet: