Java语言程序设计基础篇_编程练习题*16.12(演示TextArea的属性)

目录

题目:*16.12(演示TextArea的属性)

习题思路:

 代码示例

结果展示 


题目:*16.12(演示TextArea的属性)

编写一个程序,演示文本域的属性。程序使用复选框表明文本是否换行,如图16-41a所示。

  • 习题思路:
  1. 新建一个TextArea,创建一个ScrollPane,传入参数TextArea
  2. 创建一个HBox,新建两个复选框,Editable与Wrap,表示可编辑和换行
  3. 创建一个BorderPane,把ScrollPane设置在中心,把HBox设置在底部
  4. 为两个复选框注册事件,当复选框被勾选时把TextArea对应的方法设置为true
  5. 创建Scene并运行代码
  •  代码示例

编程练习题16_12PropertiesOfTextArea.java

package chapter_16;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class 编程练习题16_12PropertiesOfTextArea extends Application{
	@Override
	public void start(Stage primaryStage) throws Exception {
		TextArea textArea = new TextArea();
		textArea.setEditable(false);
		textArea.setWrapText(false);
		ScrollPane scrollPane = new ScrollPane(textArea);
		
		HBox hBox = new HBox(10);
		hBox.setAlignment(Pos.CENTER);
		CheckBox chkEditable = new CheckBox("Editable");
		CheckBox chkWrap = new CheckBox("Wrap");
		hBox.getChildren().addAll(chkEditable, chkWrap);
		
		BorderPane borderPane = new BorderPane();
		borderPane.setCenter(scrollPane);
		borderPane.setBottom(hBox);
		
		chkEditable.setOnAction(e ->{
			if(chkEditable.isSelected()) {
				textArea.setEditable(true);
			}else 
				textArea.setEditable(false);
		});
		chkWrap.setOnAction(e ->{
			if(chkWrap.isSelected()) {
				textArea.setWrapText(true);
			}else 
				textArea.setWrapText(false);
		});
		
		Scene scene = new Scene(borderPane,530, 180);
		primaryStage.setTitle("编程练习题16_12PropertiesOfTextArea");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	public static void main(String[] args) {
		Application.launch(args);
	}
}
  • 结果展示 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值