2022. 8. 16. 01:03ㆍ3층 1구역 - 개발의 장/JAVAfx - GUI
1. 서론
AnchorPane에 대해 알아보도록 하자.
2.본론
2-1. setTopAnchor, setLeftAnchor, setRightAnchor, setBottomAnchor을 이용한 오브젝트(컨트롤)이동 시키기
AnchorPane은 좌측 최상단(0,0)을 기준으로 표현하고자 하는 내용 및 오브젝트(컨트롤)를
이동시키는 것을 말한다.
여기서는 setTopAnchor, setLeftAnchor, setRightAnchor, setBottomAnchor를 통하여 이동시켜 볼 것이다.
고찰에서 BorderPane에 대해 다룬 적이 있었다.
2022.08.15 - [필기노트/문제의 고찰] - JAVA FX - Pane(컨테이너)의 원리의 대한 고찰(Scene, Label, BorderPane) (2022-08-15)
JAVA FX - Pane(컨테이너)의 원리의 대한 고찰(Scene, Label, BorderPane) (2022-08-15)
오늘은 광복절이다. 오늘날 '대한민국' 이라는 나라가 있게 해준 영웅 분들께 감사드립니다...(_ _) 1.서론 이번의 고찰은 Pane에 대한 것이다. 보통 '컨테이너' 라고 하는데... 부트캠프에서 배우다
sukw9512.tistory.com
여기서 TOP과 REFT는 좌측최상단, RIGHT는 우측최상단, BOTTOM은 좌측최하단에 적용되는 것을 볼 수 있다.
이것 또한 동일한 위치에서 내려가거나 올라가거나 옆으로 이동하거나 한다.
사용한 코드는 아래와 같다.
package layouts_0812;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class AnchorPaneEx extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button[] buttons = new Button[4];
String[] strings = {"TOP", "LEFT", "RIGHT", "BOTTOM"};
for(int i = 0; i < buttons.length; i++) {
buttons[i] = new Button(strings[i]);
}
buttons[0].setPrefSize(100, 50);
buttons[1].setPrefSize(100, 50);
buttons[2].setPrefSize(100, 50);
buttons[3].setPrefSize(100, 50);
AnchorPane.setTopAnchor(buttons[0], 60.0);
AnchorPane.setLeftAnchor(buttons[1], 100.0);
AnchorPane.setRightAnchor(buttons[2], 50.0);
AnchorPane.setBottomAnchor(buttons[3], 150.0);
AnchorPane anchor = new AnchorPane();
anchor.getChildren().addAll(buttons[0], buttons[1],buttons[2], buttons[3]);
Scene scene = new Scene(anchor, 600, 600);
primaryStage.setTitle("AnchorPane");
primaryStage.setScene(scene);
primaryStage.show();
}
}
결과는 어떻게 나올까?
각각 입력한 수만큼 이동했다는 결과를 얻을 수 있다.!!!
2-2. 진짜 0,0에서 이동시켜보자.
AnchorPane은 좌측 최상단(0,0)을 기준으로 표현하고자 하는 내용 및 오브젝트(컨트롤)를
이동시키는 것을 말한다.
위에서 말했던 이 내용을 한번 실현시켜보자!!
이번엔 오브젝트를 버튼이 아니라 하나하나 선택해볼 수 있는 View컬렉션을 통해 만들어보자.
코드는 다음과 같다.
package layouts_0812;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class AnchorPaneEx2 extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
ListView<String> lv = new ListView<String>();
lv.getItems().addAll("item1", "item2", "item3", "item4", "item5");
lv.setPrefSize(100, 150);
lv.setLayoutX(0);
lv.setLayoutY(0);
AnchorPane anchor = new AnchorPane();
anchor.getChildren().add(lv);
Scene scene = new Scene(anchor, 300, 200);
primaryStage.setTitle("AnchorPane");
primaryStage.setScene(scene);
primaryStage.show();
}
}
결과를 보자.
뭔가 좌측 최상단에 착 달라붙어있다. 즉 저기가 0, 0지점인 것이다.
setLayoutX,Y를 바꾸어보면 어떨까?
ListView<String> lv = new ListView<String>();
lv.getItems().addAll("item1", "item2", "item3", "item4", "item5");
lv.setPrefSize(100, 150);
lv.setLayoutX(25);
lv.setLayoutY(25);
25, 25로 바꾸어보자.
오오....이동했다
'3층 1구역 - 개발의 장 > JAVAfx - GUI' 카테고리의 다른 글
JAVA fx - JAVA fx 컨테이너 - H,Vbox (2022-08-15) (0) | 2022.08.16 |
---|---|
JAVA fx - JAVA fx 컨테이너 - StackPane (2022-08-15) (0) | 2022.08.16 |
JAVA fx - JAVA fx 컨테이너 - 구역 구분하는 퀴즈 (2022-08-15) (0) | 2022.08.16 |
JAVA fx - JAVA fx 컨테이너 - BorderPane (2022-08-15) (0) | 2022.08.15 |
JAVA fx - JAVA fx 컨테이너 - GridPane(2) (2022-08-15) (0) | 2022.08.15 |