javafx改变字体框

在这里插入图片描述

package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.scene.text.Font;

import java.awt.*;

public class Demo02 extends Application {
    HBox h1 = new HBox();
    HBox h2 = new HBox();
    VBox vb = new VBox();
    ComboBox<String> cb1 = new ComboBox<String>();
    ComboBox<String> cb2 = new ComboBox<String>();
    TextArea ta = new TextArea();
    CheckBox ckb1 = new CheckBox();
    CheckBox ckb2 = new CheckBox();
    String selectFontType;int selectFontSize=12;
    Font font;
    @Override
    public void start(Stage stage) throws Exception {

        h1.getChildren().addAll(new Label("Font Name"),cb1,new Label("Font Size"),cb2);
        cb1.setPromptText("字体");
        cb2.setPromptText("字体大小");
        h2.getChildren().addAll(ckb1,new Label("Bold"),ckb2,new Label("Italic"));
        h2.setAlignment(Pos.CENTER);
        vb.getChildren().addAll(h1,ta,h2);
//      h2.setPadding(new Insets(0));
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//      ge:代表 此电脑上的所有字体的 对象
        String[] getFontName = ge.getAvailableFontFamilyNames();
        cb1.getItems().addAll(getFontName);
        String []getFontSize=new String[100];
        for(int i=0;i<100;i++)
            getFontSize[i]=i+1+"";
        cb2.getItems().addAll(getFontSize);

        cb1.setOnAction(e->{
            selectFontType = cb1.getValue();
            setFont();
        });
        cb2.setOnAction(e->{
            selectFontSize = Integer.parseInt(cb2.getValue());
            setFont();
        });
        ckb1.setOnAction(e->{
            setFont();
        });
        ckb2.setOnAction(e->{
            setFont();
        });

        stage.setTitle("Exercise16_14");
        stage.setScene(new Scene(vb,500,200));
        stage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }

    public void setFont(){
        font = Font.font(selectFontType,selectFontSize);
        ta.setFont(Font.font(selectFontType,selectFontSize));
        if(ckb1.isSelected()&&ckb2.isSelected()){
            ta.setFont(Font.font(selectFontType, FontWeight.BOLD, FontPosture.ITALIC,selectFontSize));
        }
        else if(ckb1.isSelected()&&!ckb2.isSelected())
            ta.setFont(Font.font(selectFontType, FontWeight.BOLD,FontPosture.REGULAR,selectFontSize));
        else if(ckb2.isSelected()&&!ckb1.isSelected())
            ta.setFont(Font.font(selectFontType,FontWeight.NORMAL,FontPosture.ITALIC,selectFontSize));
        else if(!ckb1.isSelected()&&!ckb2.isSelected())
            ta.setFont(Font.font(selectFontType,FontWeight.NORMAL,FontPosture.REGULAR,selectFontSize));
        System.out.println(""+ckb1.isSelected()+ckb2.isSelected());
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值