본문 바로가기

오늘의 공부 & 기록

3월 21일 javafx

1. CheckBox

보통 다중 선택하는 경우 사용

 

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class CheckBoxEx extends Application {
	
	public static void main(String[] args) {
		launch(args);
	}

	/*
	 * 체크박스는 다중 선택할 때 사용
	 */
	@Override
	public void start(Stage primaryStage) throws Exception {
		CheckBox check1 = new CheckBox("체크1");
		CheckBox check2 = new CheckBox("체크2");
		
		check1.setText("멍때리기"); // setText()없으면 위에 생성할때 넣은 이름으로 
		check2.setText("침대와 혼연일체");
		
		check2.setSelected(true); // check2에 체크(true)
		
		System.out.println("chek1: " + check1.isSelected()); // isSelected()메서드로 체크 확인
		System.out.println("chek2: " + check2.isSelected());
		
		
		HBox box = new HBox(10);
		box.getChildren().addAll(check1, check2);
		box.setAlignment(Pos.CENTER);
		
		primaryStage.setTitle("CheckBoxEx");
		primaryStage.setScene(new Scene(box, 300, 100));
		primaryStage.show();
	}

}

 

 

 

2. RadioButton

 

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class RadioButtonEx extends Application{
	
	public static void main(String[] args) {
		launch(args);
	}
	/*
	 * 라디오 버튼은 단일 선택할 때 사용(ToggleGroup 사용 필요, 사용 안할 시 다중 선택)
	 */

	@Override
	public void start(Stage primaryStage) throws Exception {
		RadioButton radio1 = new RadioButton("라디오1");
		RadioButton radio2 = new RadioButton("라디오2");
		
		// 단일 선택을 할 수 있는 기능을 제공해주는 ToggleGroup
		ToggleGroup group = new ToggleGroup();
		radio1.setToggleGroup(group);
		radio2.setToggleGroup(group);
		
		radio1.setText("멍때리기"); // setText()없으면 위에 생성할때 넣은 이름으로 
		radio2.setText("침대와 혼연일체");
		
		radio1.setSelected(true); // radio1 선택(true)이 기본 선택
		
		System.out.println("radio1: " + radio1.isSelected()); // isSelected()메서드로 체크 확인
		System.out.println("radio2: " + radio2.isSelected());
		
		
		HBox box = new HBox();
		box.getChildren().addAll(radio1, radio2);
		box.setAlignment(Pos.CENTER);
		box.setSpacing(10); // Spacing 설정(HBox box = new HBox(10); 이렇게 하는 것도 가능) 
		
		primaryStage.setTitle("RadioButtonEx");
		primaryStage.setScene(new Scene(box, 300, 100));
		primaryStage.show();
	}

}

 

 

3. ToggleButton

 

 

 

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ToggleButtonEx extends Application{
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		ToggleButton tb1 = new ToggleButton();
		ToggleButton tb2 = new ToggleButton();
		
		// 단일 선택을 할 수 있는 기능을 제공해주는 ToggleGroup
		ToggleGroup group = new ToggleGroup();
		tb1.setToggleGroup(group);
		tb2.setToggleGroup(group);
		
		tb1.setText("눌림 버튼1");
		tb2.setText("눌림 버튼2");
		
		HBox box = new HBox();
		box.getChildren().addAll(tb1, tb2);
		box.setAlignment(Pos.CENTER);
		box.setSpacing(10);
		
		primaryStage.setTitle("ToggleButtonEx1");
		primaryStage.setScene(new Scene(box, 200, 150));
		primaryStage.show();
	}

}

 

- 토글 버튼에 이미지 넣기

 

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ToggleButtonEx2 extends Application{
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		ToggleButton tb1 = new ToggleButton();
		ToggleButton tb2 = new ToggleButton();
		ToggleButton tb3 = new ToggleButton();
		
		ToggleGroup group = new ToggleGroup();
		tb1.setToggleGroup(group);
		tb2.setToggleGroup(group);
		tb3.setToggleGroup(group);
		
		tb1.setGraphic(new ImageView("/img/magi.png"));
		tb2.setGraphic(new ImageView("/img/rogue.png"));
		tb3.setGraphic(new ImageView("/img/warrior.png"));
		
		HBox box = new HBox();
		box.getChildren().addAll(tb1, tb2, tb3);
		box.setAlignment(Pos.CENTER);
		box.setSpacing(10);
		
		primaryStage.setTitle("ToggleButtonEx2");
		primaryStage.setScene(new Scene(box, 400, 200));
		primaryStage.show();
	}

}

 

 

4. TextField

 

 

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextFieldEx extends Application{
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		TextField text1 = new TextField();
		TextField text2 = new TextField();
		
		text1.setMaxSize(180, 60);
		text2.setMaxSize(180, 60);
		
		text1.setText("아이디");
		text2.setText("이름");
		
		System.out.println("text1: " + text1.getText());
		System.out.println("text2: " + text2.getText());
		
		VBox box = new VBox();
		box.getChildren().addAll(text1, text2);
		box.setAlignment(Pos.CENTER);
		box.setSpacing(10);
		
		primaryStage.setTitle("TextFieldEx");
		primaryStage.setScene(new Scene(box, 300, 200));
		primaryStage.show();
	}

}

 

 

5. PasswordField

필드에 입력된 내용을 특수문자로 바꿔서 처리해줌

 

 

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class PasswordFieldEx extends Application{
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		PasswordField text1 = new PasswordField();
		PasswordField text2 = new PasswordField();
		
		text1.maxWidth(200);
		text1.setPrefHeight(40);
		
		text1.setText("아이디");
		text2.setText("이름");
		
		System.out.println("text1: " + text1.getText());
		System.out.println("text2: " + text2.getText());
		
		VBox box = new VBox();
		box.getChildren().addAll(text1, text2);
		box.setAlignment(Pos.CENTER);
		box.setSpacing(10);
		
		primaryStage.setTitle("PasswordFieldEx");
		primaryStage.setScene(new Scene(box, 300, 200));
		primaryStage.show();
		
	}

}

 

 

6. ComboBox

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ComboBoxEx extends Application {
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		ComboBox<String> combo = new ComboBox<String>();
		combo.getItems().addAll("멍때리기", "침대와 혼연일체", "축구는 눈으로만 시청하기");
		combo.setValue("침대와 혼연일체");
		
		HBox box = new HBox();
		box.getChildren().add(combo);
		box.setAlignment(Pos.CENTER);
		
		Scene scene = new Scene(box, 400, 100);
		primaryStage.setTitle("ComboBoxEx");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

 

 

7. TextArea

 

 

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class TextAreaEx extends Application{
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		TextArea area = new TextArea();
		area.setMaxSize(300, 150);
		
		HBox box = new HBox();
		box.getChildren().addAll(area);
		box.setAlignment(Pos.CENTER);
		
		Scene scene = new Scene(box, 400, 200);
		primaryStage.setTitle("TextAreaEx");
		primaryStage.setScene(scene);
		primaryStage.show();
				
	}

}

 

 

8. DatePicker

 

package controls;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class DatePickerEx extends Application{
	
	public static void main(String[] args) {
		launch(args);
	}
	

	@Override
	public void start(Stage primaryStage) throws Exception {
		DatePicker date = new DatePicker();
		
		HBox box = new HBox();
		box.getChildren().add(date);
		box.setAlignment(Pos.CENTER);
		
		Scene scene = new Scene(box, 300, 300);
		
		primaryStage.setTitle("DatePickerEx");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

'오늘의 공부 & 기록' 카테고리의 다른 글

3월 23일 Scene Builder  (0) 2023.03.23
3월 22일 javaFx  (0) 2023.03.22
3월 20일 javajx  (0) 2023.03.20
3월 17일 javaFX  (0) 2023.03.17
3월 16일 JDBC  (0) 2023.03.16