在JavaFX2.2中,如何设置在输入字段和表标题中输入的文本的字体大小? [英] In JavaFX2.2, how to set the font size for text entered in input fields and table headers?

查看:55
本文介绍了在JavaFX2.2中,如何设置在输入字段和表标题中输入的文本的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JavaFX2.2 fxml程序中,我发现字体无法正确缩放.结果,表头和输入数据字段过大.

In my JavaFX2.2 fxml program, I am finding that fonts do not scale properly. As a result, table headers and input data fields are disproportionately large.

是否可以在输入字段中为输入的文本设置字体大小?

Is there any way to set the font size for text entered in the input fields?

是否可以设置表格标题中显示的文本的字体大小?

Is there any way to set the font size for text displayed in the table headers?

SCCE

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.cell.*?> 
<?import javafx.collections.*?> 
<?import fxmltableview.*?> 
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<?import javafx.collections.*?>
<?import java.lang.*?>
<?import fxmltableview.Person?>

<Scene xmlns:fx="http://javafx.com/fxml" >

    <GridPane alignment="center" hgap="10" vgap="10">
        <padding>
            <Insets top="10" right="10" bottom="10" left="10"/>
        </padding>
        <Label  text="Address Book: This text is in font size 12 on Win7" GridPane.columnIndex="0" GridPane.rowIndex="0">
            <font>
                <Font size="12.0"/>
            </font>
        </Label>
        <TextField fx:id="textField" GridPane.columnIndex="0" GridPane.rowIndex="1">
            input text field. See how large I am!!!
        </TextField>
        <TableView GridPane.columnIndex="0" GridPane.rowIndex="2">
            <columns>
                <TableColumn text="First Name">
                    <cellValueFactory>
                        <PropertyValueFactory property="firstName" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Last Name">
                    <cellValueFactory>
                        <PropertyValueFactory property="lastName" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Email Address">
                    <cellValueFactory>
                        <PropertyValueFactory property="email" />
                    </cellValueFactory>
                </TableColumn>
            </columns> 
            <items>
                <FXCollections fx:factory="observableArrayList">
                    <Person firstName="Jacob" lastName="Smith"  
                            email="jacob.smith@example.com"/>
                    <Person firstName="Isabella" lastName="Johnson" 
                            email="isabella.johnson@example.com"/>
                    <Person firstName="Ethan" lastName="Williams" 
                            email="ethan.williams@example.com"/>
                    <Person firstName="Emma" lastName="Jones"
                            email="emma.jones@example.com"/>
                    <Person firstName="Michael" lastName="Brown" 
                            email="michael.brown@example.com"/>
                </FXCollections>
            </items> 
        </TableView>
    </GridPane>
</Scene>

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fxmltableview;

import javafx.beans.property.SimpleStringProperty;

public class Person {
   private final SimpleStringProperty firstName = new SimpleStringProperty("");
   private final SimpleStringProperty lastName = new SimpleStringProperty("");
   private final SimpleStringProperty email = new SimpleStringProperty("");

public Person() {
        this("", "", "");
    }

    public Person(String firstName, String lastName, String email) {
        setFirstName(firstName);
        setLastName(lastName);
        setEmail(email);
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String fName) {
        firstName.set(fName);
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String fName) {
        lastName.set(fName);
    }

    public String getPrimary() {
        return getEmail();
    }

    public String getSecondary() {
        return getEmail();
    }

    public String getEmail() {
        return email.get();
    }

    public void setEmail(String fName) {
        email.set(fName);
    }
}

public class FXMLTableViewController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}

package fxmltableview;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author 
 */
public class FXMLTableView extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
       primaryStage.setTitle("FXML TableView Example");
       primaryStage.setScene
       ((Scene)FXMLLoader.load(getClass().getResource("fxml_tableview.fxml")));
       primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

推荐答案

我通过 Marco 所以我

1)手动更改表格列的宽度

1) manually changed the width of the table column

        <TableColumn text="First Name" prefWidth="90" >
            <cellValueFactory>
                <PropertyValueFactory property="firstName" />
            </cellValueFactory>
        </TableColumn>
        <TableColumn text="Last Name" prefWidth="90" >
            <cellValueFactory>
                <PropertyValueFactory property="lastName" />
            </cellValueFactory>
        </TableColumn>

2)在fxml文件中添加了一个链接

2) Added a link in the fxml file

  <stylesheets>
    <URL value="@tffontsize.css" />
  </stylesheets>

3)创建了一个css文件tfffontsize.css

3) created a css file tfffontsize.css

.text-field {
    -fx-font-size: 12pt;
}
.table-view .column-header{
    -fx-font-size: 14;
}

.table-cell {
    -fx-font-size: 12px;
}

这篇关于在JavaFX2.2中,如何设置在输入字段和表标题中输入的文本的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆