Chapter 20: Oooey GUI Was a Worm
In this chapter:
The order of the statements is somewhat flexible. Here are two constraints:
- In the
main
method, a variable must be declared before it's used.
- You must add everything that you're going to add to the frame before you call the frame's
pack
method.
So, for example, you can't use the frame
variable before you've declared the frame
variable:
// Bad code:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame frame = new JFrame();
You shouldn't call the frame
object's pack
method before you've added the label to the object:
// Bad code:
frame.pack();
frame.add(label);
Here's the root.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="199.0" prefWidth="280.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="application.Main">
<children>
<Button layoutX="111.0" layoutY="73.0" mnemonicParsing="false" onAction="#onClick" text="Copy" />
<TextField fx:id="textField" layoutX="57.0" layoutY="27.0" />
<TextField fx:id="textField2" layoutX="57.0" layoutY="123.0" />
</children>
</AnchorPane>
Here's the Main.java file:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// BorderPane root = new BorderPane();
Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
@FXML
private TextField textField, textField2;
@FXML
protected void onClick(ActionEvent event) {
textField2.setText(textField.getText());
}
}
Here's the root.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="199.0" prefWidth="280.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
<children>
<Button layoutX="57.0" layoutY="73.0" mnemonicParsing="false" onAction="#onClickCopyDown" text="Copy down" />
<TextField fx:id="textField" layoutX="57.0" layoutY="27.0" prefHeight="27.0" prefWidth="179.0" />
<TextField fx:id="textField2" layoutX="57.0" layoutY="123.0" prefHeight="27.0" prefWidth="179.0" />
<Button layoutX="165.0" layoutY="73.0" mnemonicParsing="false" onAction="#onClickCopyUp" text="Copy up" />
</children>
</AnchorPane>
Here's the Main.java file:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// BorderPane root = new BorderPane();
Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
@FXML
private TextField textField, textField2;
@FXML
protected void onClickCopyDown(ActionEvent event) {
textField2.setText(textField.getText());
}
@FXML
protected void onClickCopyUp(ActionEvent event) {
textField.setText(textField2.getText());
}
}
Here's the root.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="199.0" prefWidth="280.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
<children>
<Button layoutX="111.0" layoutY="73.0" mnemonicParsing="false" onAction="#onClick" text="Copy" />
<TextField fx:id="textField" layoutX="57.0" layoutY="27.0" />
<Label fx:id="label" layoutX="66.0" layoutY="131.0" text="New text displayed here" />
</children>
</AnchorPane>
Here's the Main.java file:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// BorderPane root = new BorderPane();
Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
@FXML
private TextField textField;
@FXML
private Label label;
@FXML
protected void onClick(ActionEvent event) {
label.setText(textField.getText());
}
}
Here's the root.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
<children>
<AnchorPane prefHeight="194.0" prefWidth="331.0">
<children>
<TextField fx:id="addend1" layoutX="26.0" layoutY="78.0" prefHeight="27.0" prefWidth="67.0" />
<Button layoutX="204.0" layoutY="78.0" mnemonicParsing="false" onAction="#onClick" text="=" />
<TextField fx:id="addend2" layoutX="121.0" layoutY="78.0" prefHeight="27.0" prefWidth="67.0" />
<Label layoutX="104.0" layoutY="83.0" text="+" />
<Label fx:id="sum" layoutX="240.0" layoutY="83.0" />
</children>
</AnchorPane>
</children>
</AnchorPane>
Here's the Main.java file:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
//BorderPane root = new BorderPane();
Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
@FXML
private TextField addend1, addend2;
@FXML
private Label sum;
@FXML
protected void onClick(ActionEvent event) {
int number1 = Integer.parseInt(addend1.getText());
int number2 = Integer.parseInt(addend2.getText());
sum.setText(Integer.toString(number1 + number2));
}
}