JAVA chapter17. javaFX.


17.8 JavaFX 메뉴바와 툴바


17.8.1 MenuBar 컨트롤



17.8.2 Toolbar 컨트롤


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.input.*?>
<?import javafx.scene.image.*?>
<?import javafx.collections.*?><!-- fxml파일 이므로 import가 따로 필요하다 -->
<?import java.lang.*?<!-- fxml파일 이므로 import가 따로 필요하다 -->
 
<BorderPane xmlns:fx="http://javafx.com/fxml" 
fx:controller="sec08.exam01_menubar_toolbar.RootController"
    prefHeight="200.0" prefWidth="400.0" >
    <top>
        <VBox>
            <children>
                <MenuBar><!--  -->
                    <menus>
                        <Menu text="파일">
                            <items>
                                <MenuItem text="새로만들기" onAction="#handleNew" > <!--handleNew라는 메소드 실행-->
                                    <accelerator>
                                        <KeyCodeCombination alt="DOWN" code="N" control="UP" meta="UP" shift="DOWN" shortcut="UP" /<!--6개 생성자 똑같이 맞춰야됨-->
                                    </accelerator>
                                    <graphic>
                                        <ImageView><image><Image url="@icons/new.png" /></image></ImageView>
                                    </graphic>
                                </MenuItem>
                                <MenuItem text="열기"  onAction="#handleOpen" >
                                    <accelerator>
                                        <KeyCodeCombination alt="UP" code="O" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
                                    </accelerator>
                                    <graphic>
                                        <ImageView><image><Image url="@icons/open.png" /></image></ImageView>
                                    </graphic>
                                </MenuItem>
                                <MenuItem text="저장" onAction="#handleSave" >
                                    <accelerator>
                                        <KeyCodeCombination alt="UP" code="S" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
                                    </accelerator>
                                    <graphic>
                                        <ImageView><image> <Image url="@icons/save.png" /></image> </ImageView>
                                    </graphic>                     
                                </MenuItem>
                                <SeparatorMenuItem />
                                <MenuItem text="끝내기" onAction="#handleExit"/>
                            </items>
                        </Menu>
                    </menus>
                </MenuBar><!--  -->
              
                <ToolBar><!--  -->
                    <items>
                        <Button onAction="#handleNew"> <!--handleNew라는 메소드 실행-->
                            <graphic>
                                <ImageView><image><Image url="@icons/new.png" /></image></ImageView>
                            </graphic>
                        </Button>
                        <Button onAction="#handleOpen">
                            <graphic>
                                <ImageView><image><Image url="@icons/open.png" /></image></ImageView>
                            </graphic>
                        </Button>
                        <Button onAction="#handleSave">
                            <graphic>
                                <ImageView><image><Image url="@icons/save.png" /></image></ImageView>
                            </graphic>
                        </Button>
                        <ComboBox prefWidth="100" promptText="선택" >
                            <items>
                                <FXCollections fx:factory="observableArrayList">
                                    <String fx:value="공개"/>
                                    <String fx:value="비공개"/>
                                </FXCollections>
                            </items>
                        </ComboBox>                        
                    </items>
                </ToolBar><!--  -->
            </children>
        </VBox>
    </top>
    
    <center>
        <TextArea fx:id="textArea"/>
    </center>
</BorderPane>
 
cs


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
26
27
28
29
30
31
32
33
34
package sec08.exam01_menubar_toolbar;
 
import java.net.URL;
import java.util.ResourceBundle;
 
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
 
public class RootController implements Initializable {
    @FXML private TextArea textArea;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }
    
    public void handleNew(ActionEvent e) {
        textArea.appendText("New\n");
    }
    
    public void handleOpen(ActionEvent e) {
        textArea.appendText("Open\n");
    }
    
    public void handleSave(ActionEvent e) {
        textArea.appendText("Save\n");
    }
    
    public void handleExit(ActionEvent e) {
        Platform.exit();
    }
}
cs




Posted by 너래쟁이
: