java语言程序设计 第十四章 (样例代码)

程序小白,希望和大家多交流,共同学习
java语言程序设计 第十四章 (样例代码)_第1张图片
按钮Button使用样例

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.scene.control.Button;

public class ShowButton extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Button okbt = new Button("Hello world!");
        Pane pane = new StackPane();
        pane.getChildren().add(okbt);
        Scene scene = new Scene(pane, 250, 250);
        primaryStage.setTitle("Hello wrold!");
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    public static void main(String [] args)
    {
        Application.launch(args);
    }
    //main方法不是必须的,但是在不完全支持javafx的IDE中启动javafx的话,
    //launch的方法就是必须的。
    //一般没有launch,在启动javafx之后,JVM会自动调用.
    //launch的实现是在Application类中。javafx启动的时候会使用他的无参构造方法创建一个实例
    //同时调用他的start方法
}

弧Arc使用样例

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;

public class ShowArc extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Pane pane = new Pane();
        Arc arc1 = new Arc(150, 100, 80, 80, 30, 35);
        arc1.setFill(Color.RED);
        arc1.setType(ArcType.ROUND);
        arc1.setStroke(Color.BLACK);
        pane.getChildren().add(new Text(210, 40, "arc1: round"));
        pane.getChildren().add(arc1);

        Arc arc2 = new Arc(150, 100, 80, 80, 30 + 90, 35);
        arc2.setFill(Color.WHITE);
        //没有设置就会默认为黑色
        arc2.setType(ArcType.OPEN);
        arc2.setStroke(Color.BLACK);
        pane.getChildren().add(new Text(20, 40, "arc2 : open"));
        pane.getChildren().add(arc2);

        Arc arc3 = new Arc(150, 100, 80, 80, 30 + 180, 35);
        arc3.setFill(Color.WHITE);
        arc3.setType(ArcType.CHORD);
        arc3.setStroke(Color.BLACK);
        pane.getChildren().add(new Text(20, 170, "arc3 : chord"));
        pane.getChildren().add(arc3);

        Arc arc4 = new Arc(150, 100, 80, 80, 30 + 270, 35);
        arc4.setFill(Color.GREEN);
        arc4.setType(ArcType.CHORD);
        arc4.setStroke(Color.BLACK);
        pane.getChildren().add(new Text(210, 170, "arc4 : open"));
        pane.getChildren().add(arc4);

        Scene scene = new Scene(pane);
        primaryStage.setTitle("ShowArc");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

面板HBox和VBox使用样例

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.control.Button;
import javafx.geometry.Insets;
import javafx.scene.image.ImageView;


public class ShowHBoxAndVBox extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        BorderPane pane = new BorderPane();
        pane.setTop(getHBox());
        pane.setLeft(getVBox());

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ShowHBoxAndVBox");
        primaryStage.show();
    }

    public HBox getHBox()
    {

        HBox hBox = new HBox(15);
        hBox.setPadding(new Insets(15, 15, 15, 15));
        hBox.setStyle("-fx-border-color: red; -fx-background-color: navy");
        hBox.getChildren().addAll(new Button("China"), new Button("中国"));
        ImageView imageView = new ImageView(new Image("2.jpg"));
        hBox.getChildren().add(imageView);
        return hBox;
    }

    public VBox getVBox()
    {
        VBox vBox = new VBox(15);
        vBox.setPadding(new Insets(15, 5, 5, 5));
        vBox.getChildren().add(new Label("Course"));

        Label[] courses = {new Label("CSCI 1301"), new Label("CSCI 1302"),
            new Label("CSCI 2410"), new Label("CSCI 3720")};
        for (Label course : courses)
        {
            vBox.setMargin(course, new Insets(0, 0, 0, 15));
            vBox.getChildren().add(course);
        }
        return vBox;
    }
}

线line使用样例

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;

public class ShowLine extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Scene scene = new Scene(new LinePane(), 200, 200);
        primaryStage.setTitle("ShowLine");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
//创建新的面板,用来显示两条交叉线
class LinePane extends Pane
{
    public LinePane()
    {
        Line line1 = new Line(10, 10, 10, 10);
        line1.endXProperty().bind(widthProperty().subtract(10));
        line1.endYProperty().bind(heightProperty().subtract(10));
        line1.setStrokeWidth(5);
        line1.setStroke(Color.GREEN);
        getChildren().add(line1);

        Line line2 = new Line(10, 10, 10, 10);
        line2.startXProperty().bind(widthProperty().subtract(10));
        line2.endYProperty().bind(heightProperty().subtract(10));
        line2.setStroke(Color.RED);
        line2.setStrokeWidth(5);
        getChildren().add(line2);

    }
}

多边形Polygon使用样例

import javafx.application.Application;
import javafx.collections.ObservableList;
//ObservableList的路径
//对比java.util.Collections
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;

public class ShowPolygon extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Pane pane = new Pane();
        Polygon polygon = new Polygon();
        pane.getChildren().add(polygon);
        polygon.setFill(Color.WHITE);
        polygon.setStroke(Color.BLACK);
        ObservableList list = polygon.getPoints();

        //特别之处在于,多边形它会自动闭合。
        //真正存在的是在点的坐标设定
        // Polyline 的使用和 Ploygon 的使用完全一样,只是Polyline 不会自动闭合。
        // 注意line的l
        final double WIDTH = 200, HEIGHT = 200;
        double centerX = WIDTH / 2, centerY = HEIGHT / 2;
        double radius = Math.min(WIDTH, HEIGHT) * 0.4;

        for (int i = 0; i < 6; i++)
        {
            list.add(centerX + radius * Math.cos(2 * i * Math.PI / 6));
            list.add(centerY - radius * Math.sin(2 * i * Math.PI / 6));
        }

        Scene scene = new Scene(pane);
        primaryStage.setTitle("ShowPolygon");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

矩形Rectangle使用样例

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;

public class ShowRectangle extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Pane pane = new Pane();
        Rectangle r1 = new Rectangle(25, 10, 60, 30);
        r1.setStroke(Color.BLACK);
        r1.setFill(Color.WHITE);
        pane.getChildren().add(new Text(10, 27, "r1"));
        pane.getChildren().add(r1);

        Rectangle r2 = new Rectangle(25, 50, 60, 30);
        pane.getChildren().add(new Text(10, 67, "r2"));
        pane.getChildren().add(r2);

        Rectangle r3 = new Rectangle(25, 90, 60, 30);
        r3.setArcWidth(15);
        r3.setArcHeight(25);
        pane.getChildren().add(new Text(10, 107, "r3"));
        pane.getChildren().add(r3);

        for (int i = 0; i < 4; i++)
        {
            Rectangle r = new Rectangle(100, 50, 100, 10);
            r.setRotate(i * 360 / 8);
            r.setStroke(Color.color(Math.random(), Math.random(), Math.random()));
            //r.setFill(Color.WHITE);
            r.setFill(null);
            pane.getChildren().add(r);
        }

        Scene scene = new Scene(pane);
        primaryStage.setTitle("ShowRectangle");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
//文本Text、矩形Rectangle都存在开始位置

文本Text使用样例

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;

public class ShowText extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Pane pane = new Pane();
        pane.setPadding(new Insets(5, 5, 5, 5));
        Text text1 = new Text(20, 20, "Programming is fun");
        //确定位置,然后书写内容
        text1.setFont(Font.font("Courier", FontWeight.BOLD, FontPosture.ITALIC, 15));
        pane.getChildren().add(text1);

        Text text2 = new Text(60, 60, "Programming is fun\nDisplay text");
        pane.getChildren().add(text2);
        text2.setUnderline(true);

        Text text3 = new Text(10, 100, "Programming is fun\nDisplay text");
        text3.setFill(Color.RED);
        text3.setUnderline(true);
        text3.setStrikethrough(true);
        pane.getChildren().add(text3);

        Scene scene = new Scene(pane);
        primaryStage.setTitle("ShowText");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
//shape是一个抽象基类。包含属性有:fill, stroke, strokeWidth
//Text类:javafx.scene.text.Text;(javafx.scene.text.Font; javafx.scen.text.FontWeight; 
//javafx.scene.text.FonntPosture;)并不是在shape中

你可能感兴趣的:(JavaFX)