JAVA chapter17. javaFX. 


17.10 JavaFX CSS 스타일



17.10.1 인라인(inline) 스타일


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sec10.exam01_inline;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
public class AppMain extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("root.fxml"));
        Scene scene = new Scene(root);
        
        primaryStage.setTitle("AppMain");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
 
<HBox xmlns:fx="http://javafx.com/fxml"
    prefHeight="50" prefWidth="400" alignment="CENTER" spacing="20">
   <children>
        <Label text="검정바탕,노란글씨"
            style="-fx-background-color: black; -fx-text-fill: yellow; -fx-padding: 5;"/>
        <Label text="파란바탕,흰글씨"
            style="-fx-background-color: blue; -fx-text-fill: white; -fx-padding: 5;"/>
        <Label text="파란바탕,흰글씨"
            style="-fx-background-color: blue; -fx-text-fill: white; -fx-padding: 5;"/>
   </children>
</HBox>
cs

17.10.2 외부 CSS 파일





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package sec10.exam02_css_file;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
 
public class AppMain extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("root.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("app.css").toString()); // 외부 css파일 적용
        
        primaryStage.setTitle("AppMain");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
cs

app.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* 전체 Label 선택 */ css 주석은 //이 아니라 /*로 시작되서 */로 끝난다
Label {
    -fx-padding: 5;
}
 
/* id="lblId" 을 가진 컨트롤 선택 */
#lblId {
    -fx-background-color: black;
    -fx-text-fill: yellow;
}
 
/* styleClass="lblClass"을 가진 컨트롤 선택 */
.lblClass {
    -fx-background-color: blue;
    -fx-text-fill: white;
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
 
<HBox xmlns:fx="http://javafx.com/fxml"
    prefHeight="50" prefWidth="400" alignment="CENTER" spacing="20">
   <children>
        <Label id="lblId" text="검정바탕,노란글씨"/>
        <Label styleClass="lblClass" text="파란바탕,흰글씨"/>
        <Label styleClass="lblClass" text="파란바탕,흰글씨"/>
   </children>
</HBox>
cs




1
2
3
4
5
6
7
8
9
10
11
12
13
14
TextField:focused {
    -fx-border-color: skyblue;
    -fx-background-color: yellow;
}
 
Button:hover {
    -fx-border-color: skyblue;
    -fx-background-color: yellow;    
}
 
Button:pressed {
    -fx-border-color: skyblue;
    -fx-background-color: red;    
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
 
<HBox xmlns:fx="http://javafx.com/fxml" spacing="10">                             
    <padding>                                                                
        <Insets topRightBottomLeft="10"/>
    </padding>
 
    <children>    
        <TextField prefWidth="200"/>                                                            
        <Button prefWidth="50" text="확인"/>
    </children>
</HBox>
cs




Posted by 너래쟁이
: