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());
}
}
javafx改变字体框
最新推荐文章于 2022-07-24 07:02:30 发布