如何将 JavaFX 场景函数包含到 public static void main
How to include JavaFX scene functions into public static void main
我想知道是否有可能将 JavaFX 场景函数包含到
public static void main (String args[]) {
…
}
public static void main
里面有launch(args)
;
helloworld.java
JavaFX 程序如下所示:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import static javafx.geometry.HPos.RIGHT;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Login extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Welcome");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Text scenetitle = new Text("Welcome");
scenetitle.setId("welcome-text");
// scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);
Label userName = new Label("User Name:");
grid.add(userName, 0, 1);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 1);
Label pw = new Label("Password:");
grid.add(pw, 0, 2);
PasswordField pwBox = new PasswordField();
grid.add(pwBox, 1, 2);
Button btn = new Button("Sign in");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
final Text actiontarget = new Text();
grid.add(actiontarget, 0, 6);
grid.setColumnSpan(actiontarget, 2);
grid.setHalignment(actiontarget, RIGHT);
actiontarget.setId("actiontarget");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// actiontarget.setFill(Color.FIREBRICK);
actiontarget.setId("actiontarget");
actiontarget.setText("Sign in button pressed");
}
});
Scene scene = new Scene(grid, 300, 275);
primaryStage.setScene(scene);
scene.getStylesheets().add(
Login.class.getResource("Login.css").toExternalForm());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我想使用 static void main
里面的整个程序,就像 java Swing,就像下面的程序:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class combotable {
static JFrame frame = new JFrame();
static JButton boton_nuevo=new JButton("nuevo");
public static void main(String[] args){
frame.setSize(400,400);
String[] petStrings={"Bird", "Cat", "Dog", "Rabbit", "Pig"};
//JComboBox comboBox = new JComboBox(petStrings);
JComboBox<String> combo = new JComboBox<>(petStrings);
combo.setSelectedIndex(4);
//comboBox.setSelectedIndex(4);
frame.add(combo, BorderLayout.PAGE_START);
frame.add (boton_nuevo);
boton_nuevo.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s = (String) combo.getSelectedItem();
if (s == "Bird")
{
System.out.println("Se selecciono Bird");
}
if (s== "Cat")
{
System.out.println("Se selecciono Cat");
}
if (s== "Dog")
{
System.out.println("Se selecciono Dog");
}
if (s== "Rabbit")
{
System.out.println("Se selecciono Rabbit");
}
if (s== "Pig")
{
System.out.println("Se selecciono Pig");
}
}
}
);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
有没有机会做到这一点?感谢任何帮助。
没有。您的 Swing 示例未正确同步,因为 Swing GUI 对象应该在 event dispatch thread. Your JavaFX Application
is correctly synchronized in that "modification of scene graph operations to live objects (those objects already attached to a scene) must be done on the JavaFX application thread 上 仅 构造和操作。”您可以覆盖 init()
以进行其他初始化或构造 JavaFX 对象 other 而不是 Scene
或 Stage
.
我想知道是否有可能将 JavaFX 场景函数包含到
public static void main (String args[]) {
…
}
public static void main
里面有launch(args)
;
helloworld.java
JavaFX 程序如下所示:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import static javafx.geometry.HPos.RIGHT;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Login extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Welcome");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Text scenetitle = new Text("Welcome");
scenetitle.setId("welcome-text");
// scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);
Label userName = new Label("User Name:");
grid.add(userName, 0, 1);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 1);
Label pw = new Label("Password:");
grid.add(pw, 0, 2);
PasswordField pwBox = new PasswordField();
grid.add(pwBox, 1, 2);
Button btn = new Button("Sign in");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
final Text actiontarget = new Text();
grid.add(actiontarget, 0, 6);
grid.setColumnSpan(actiontarget, 2);
grid.setHalignment(actiontarget, RIGHT);
actiontarget.setId("actiontarget");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// actiontarget.setFill(Color.FIREBRICK);
actiontarget.setId("actiontarget");
actiontarget.setText("Sign in button pressed");
}
});
Scene scene = new Scene(grid, 300, 275);
primaryStage.setScene(scene);
scene.getStylesheets().add(
Login.class.getResource("Login.css").toExternalForm());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我想使用 static void main
里面的整个程序,就像 java Swing,就像下面的程序:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class combotable {
static JFrame frame = new JFrame();
static JButton boton_nuevo=new JButton("nuevo");
public static void main(String[] args){
frame.setSize(400,400);
String[] petStrings={"Bird", "Cat", "Dog", "Rabbit", "Pig"};
//JComboBox comboBox = new JComboBox(petStrings);
JComboBox<String> combo = new JComboBox<>(petStrings);
combo.setSelectedIndex(4);
//comboBox.setSelectedIndex(4);
frame.add(combo, BorderLayout.PAGE_START);
frame.add (boton_nuevo);
boton_nuevo.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s = (String) combo.getSelectedItem();
if (s == "Bird")
{
System.out.println("Se selecciono Bird");
}
if (s== "Cat")
{
System.out.println("Se selecciono Cat");
}
if (s== "Dog")
{
System.out.println("Se selecciono Dog");
}
if (s== "Rabbit")
{
System.out.println("Se selecciono Rabbit");
}
if (s== "Pig")
{
System.out.println("Se selecciono Pig");
}
}
}
);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
有没有机会做到这一点?感谢任何帮助。
没有。您的 Swing 示例未正确同步,因为 Swing GUI 对象应该在 event dispatch thread. Your JavaFX Application
is correctly synchronized in that "modification of scene graph operations to live objects (those objects already attached to a scene) must be done on the JavaFX application thread 上 仅 构造和操作。”您可以覆盖 init()
以进行其他初始化或构造 JavaFX 对象 other 而不是 Scene
或 Stage
.