Javafx-text

提供:Dev Guides
移動先:案内検索

JavaFX-テキスト

さまざまな形状と同様に、JavaFXでテキストノードを作成することもできます。 テキストノードは、パッケージ javafx.scene.text に属する Text という名前のクラスで表されます。

このクラスには、JavaFXでテキストを作成し、その外観を変更するためのいくつかのプロパティが含まれています。 このクラスは、パッケージ javafx.scene.shape に属するShapeクラスも継承します。

したがって、フォント、配置、行間隔、テキストなどのテキストのプロパティに加えて また、* strokeFill、stroke、strokeWidth、strokeTypeなどの基本的なシェイプノードプロパティを継承します。

テキストノードの作成

パッケージ javafx.scene.text のクラスTextはJavaFXのテキストノードを表すため、次のようにこのクラスをインスタンス化することでテキストを作成できます-

Text text = new Text();

クラスTextには、作成されるテキストを表す文字列型の text という名前のプロパティが含まれています。

Textクラスをインスタンス化した後、以下に示すように* setText()*メソッドを使用してこのプロパティに値を設定する必要があります。

String text = "Hello how are you"
Text.setText(text);

また、次のコードブロックに示すように、それぞれのセッターメソッド、すなわち* setX()および setY()*を使用してプロパティxおよびyに値を指定することにより、テキストの位置(原点)を設定することもできます-

text.setX(50);
text.setY(50);

次のプログラムは、JavaFXでテキストノードを作成する方法を示す例です。 このコードを TextExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Text;

public class TextExample extends Application {
   @Override
   public void start(Stage stage) {
     //Creating a Text object
      Text text = new Text();

     //Setting the text to be added.
      text.setText("Hello how are you");

     //setting the position of the text
      text.setX(50);
      text.setY(50);

     //Creating a Group object
      Group root = new Group(text);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Sample Application");

     //Adding scene to the stage
      stage.setScene(scene);

     //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。

javac TextExample.java
java TextExample

実行すると、上記のプログラムは、次のように指定されたテキストを表示するJavaFXウィンドウを生成します-

サンプルアプリケーションテキスト

テキストの位置とフォント

デフォルトでは、テキストクラスによって作成されたテキストは、フォント…、サイズ…、および黒色です。

  • setFont()メソッドを使用して、テキストのフォントサイズと色を変更できます。 このメソッドは、 *Font クラスのオブジェクトを受け入れます。

パッケージ javafx.scene.textFont という名前のクラスは、テキストのフォントを定義するために使用されます。 このクラスには、* font()*という名前の静的メソッドが含まれています。

このメソッドは、4つのパラメータを受け入れます-

  • family -これは文字列型で、テキストに適用するフォントのファミリーを表します。
  • 重量-このプロパティは、フォントの重量を表します。 - FontWeight.BLACK、FontWeight.BOLD、FontWeight.EXTRA_BOLD、FontWeight.EXTRA_LIGHT、LIGHT、MEDIUM、NORMAL、SEMI_BOLD、THIN の9つの値を受け入れます。
  • posture -このプロパティは、フォントの姿勢(通常または斜体)を表します。 2つの値 FontPosture.REGULAR および FontPosture.ITALIC を受け入れます。
  • サイズ-このプロパティはdouble型で、フォントのサイズを表します。

次の方法を使用して、テキストにフォントを設定できます-

text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));

次のプログラムは、JavaFXでテキストノードのフォントを設定する方法を示す例です。 ここでは、フォントをVerdana、太さを太字、姿勢を標準、サイズを20に設定しています。

このコードを TextFontExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class TextFontExample extends Application {
   @Override
   public void start(Stage stage) {
     //Creating a Text object
      Text text = new Text();

     //Setting font to the text
      text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));

     //setting the position of the text
      text.setX(50);
      text.setY(130);

     //Setting the text to be added.
      text.setText("Hi how are you");

     //Creating a Group object
      Group root = new Group(text);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Setting Font to the text");

     //Adding scene to the stage
      stage.setScene(scene);

     //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。

javac TextFontExample.java
java TextFontExample

実行すると、上記のプログラムは、次のように指定されたフォントでテキストを表示するJavaFXウィンドウを生成します-

フォントをテキストに設定

ストロークと色

Textクラスは、パッケージのShapeクラスも継承します。 したがって、 javafx.scene.shape を使用して、テキストノードにストロークと色を設定することもできます。

次のように、シェイプ(継承)クラスの* setFill()*メソッドを使用して、テキストに色を設定できます-

text.setFill(Color.BEIGE);

同様に、メソッド* setStroke()を使用して、テキストのストロークの色を設定できます。 ストロークの幅は、次のようにメソッド setStrokeWidth()*を使用して設定できます-

//Setting the color
text.setFill(Color.BROWN);

//Setting the Stroke
text.setStrokeWidth(2);

//Setting the stroke color
text.setStroke(Color.BLUE);

次のプログラムは、テキストノードの色、strokeWidth、およびstrokeColorを設定する方法を示す例です。 このコードでは、ストロークの色を青、テキストの色を茶色、ストロークの幅を2に設定しています。

このコードを StrokeExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class StrokeExample extends Application {
   @Override
   public void start(Stage stage) {
     //Creating a Text object
      Text text = new Text();

     //Setting font to the text
      text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 50));

     //setting the position of the text
      text.setX(50);
      text.setY(130);

     //Setting the color
      text.setFill(Color.BROWN);

     //Setting the Stroke
      text.setStrokeWidth(2);

     //Setting the stroke color
      text.setStroke(Color.BLUE);

     //Setting the text to be added.
      text.setText("Hi how are you");

     //Creating a Group object
      Group root = new Group(text);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Setting font to the text");

     //Adding scene to the stage
      stage.setScene(scene);

     //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。

javac StrokeExample.java
java StrokeExample

実行すると、上記のプログラムは、次のように指定されたストロークと色の属性を持つテキストを表示するJavaFXウィンドウを生成します-

テキストストロークの例

テキストへの装飾の適用

取り消し線などの装飾を適用することもできます。この場合、テキストを介して行が渡されます。 Text クラスのメソッドを使用して、テキストに下線を引くことができます。

メソッド* setStrikethrough()を使用してテキストを打ち消すことができます。 これはブール値を受け入れ、次のコードボックスに示すようにテキストを打ち消すためにこのメソッドに値 *true を渡します-

//Striking through the text
text1.setStrikethrough(true);

同様に、次のように値 true をメソッド* setUnderLine()*に渡すことにより、テキストに下線を引くことができます-

//underlining the text
text2.setUnderline(true);

次のプログラムは、 underlinestrike through などの装飾をテキストに適用する方法を示す例です。 このコードを DecorationsExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class DecorationsExample extends Application {
   @Override
   public void start(Stage stage) {
     //Creating a Text_Example object
      Text text1 = new Text("Hi how are you");

     //Setting font to the text
      text1.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));

     //setting the position of the text
      text1.setX(50);
      text1.setY(75);

     //Striking through the text
      text1.setStrikethrough(true);

     //Creating a Text_Example object
      Text text2 = new Text("Welcome to finddevguides");

     //Setting font to the text
      text2.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));

     //setting the position of the text
      text2.setX(50);
      text2.setY(150);

     //underlining the text
      text2.setUnderline(true);

     //Creating a Group object
      Group root = new Group(text1, text2);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Decorations Example");

     //Adding scene to the stage
      stage.setScene(scene);

     //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。

javac DecorationsExample.java
java DecorationsExample

実行すると、上記のプログラムは以下に示すようにJavaFXウィンドウを生成します-

装飾の例