ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] JRadionButton, 라디오버튼 컴포넌트, JTextField, 텍스트필드 컴포넌트, JTextArea, 텍스트영역 컴포넌트
    Languages/Java 2021. 5. 4. 16:31
    반응형

     

     

     

     

     

     

     

     

    JRadionButton

     

    라디오 버튼이란?

     

    여러 버튼으로 그룹을 형성하고,

    하나만 선택되는 버튼

    다른 버튼이 선택되면 이전에 선택된

    버튼은 자동으로 해제됨

     

    체크박스와의 차이점

     

    체크박스는 각 체크박스마다 선택/해제가 가능

    라디오버튼은 그룹에 속한 버튼 중 하나만 선택 상태가 됨

     

    이미지를 가진 라디오버튼의 생성 및 다루기는 체크박스와 완전히 동일

     

     

     

    생성자

     

     

    JRadioButton() 빈 라디오버튼

    JRadioButton( Icon image ) 이미지 라디오버튼

    JRadioButton( Icon image, boolean selected ) 이미지 라디오버튼

    JRadioButton(String text ) 문자열 라디오버튼

    JRadioButton(String text, boolean selected ) 문자열 라디오버튼

    JRadioButton(String text, Icon image ) 문자열과 이미지를 가진 라디오버튼

    JRadioButton(String text, Icon image, boolean selected ) 문자열과 이미지를 가진 라디오버튼

     

    **selected : true이면 선택 상태로 초기화, 디폴트는 해제 상태

     

     

     

     

     

     

    라디오버튼 생성과정

     

     

    ButtonGroup group = new ButtonGroup(); // 버튼 그룹 객체 생성(중요)
    
    JRadioButton apple = new JRadioButton("사과");
    JRadioButton pear = new JRadioButton("배");
    JRadioButton cherry = new JRadioButton("체리"); // 라디오 컴포넌트 생성
    
    group.add(apple);
    group.add(pear);
    group.add(cherry); // 라디오 버튼을 버튼 그룹에 삽입(중요)
    
    container.add(apple);
    container.add(pear);
    container.add(cherry); //라디오 버튼을 컨테이너에 삽입
    

     

     

     

     

    라디오버튼 생성 예제

     

    package project;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class RadioButtonEx extends JFrame {
    	public RadioButtonEx() {
    		setTitle("라디오버튼 만들기 예제");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container c = getContentPane();
    		c.setLayout(new FlowLayout());
    		ImageIcon cherryIcon = new ImageIcon("images/cherry.j pg" );
    		ImageIcon selectedCherryIcon = 
    				new ImageIcon("images/selectedCherry.jpg"); 
    		ButtonGroup g = new ButtonGroup();
    		JRadioButton apple = new JRadioButton("사과");
    		JRadioButton pear = new JRadioButton("배", true);
    		JRadioButton cherry = new JRadioButton("체리", cherryIcon);
    		cherry.setBorderPainted(true);
    		cherry.setSelectedIcon(selectedCherryIcon);
    		g.add(apple);
    		g.add(pear);
    		g.add(cherry);
    		c.add(apple);
    		c.add(pear);
    		c.add(cherry);
    		setSize(250,150);
    		setVisible(true);
    	}
    public static void main(String [] args) {
    	new RadioButtonEx();
    	}
    }

     

     

     

     

     

     

     

     

     

    JTextField, 텍스트필트 컴포넌트

     

    한 줄 짜리 텍스트 입력 창을 구현한 컴포넌트

    텍스트 입력 도중 <enter> 키가 입력되면 action 이벤트 발생

    입력 가능한 문자 개수와 입력 창의 크기는 서로 다름

     

     

     

    생성자

     

    JTextField() 빈 텍스트필드

    JTextField(int cols) 입력 창의 열의 개수가 cols개인 텍스트필드

    JTextField(String text) text 문자열로 초기화된 텍스트필드

    JTextField(String text, int cols) 입력 창의 열의 개수는 cols개이고 text 문자열로 초기화된 텍스트필드

     

     

     

     

     

     

     

    간단한 텍스트 필드 만들기 예제

     

     

     

    package project;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class TextFieldEx extends JFrame {
    	public TextFieldEx() {
    		setTitle("텍스트필드 만들기 예제");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container c = getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		c.add(new JLabel("이름 "));
    		JTextField tfName = new JTextField(20);
    		c.add(tfName);
    		c.add(new JLabel("학과 "));
    		JTextField tfDept = new JTextField("컴퓨터공학과", 20);
    		c.add(tfDept);
    		c.add(new JLabel("주소 "));
    		JTextField tfAddr = new JTextField("서울시 ...",20);
    		c.add(tfAddr);
    		JPanel pnBtn = new JPanel();
    		JButton btnOk = new JButton("ok");
    		JButton btnCancel = new JButton("cancel");
    		btnOk.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				String inName = tfName.getText();
    				String inDept = tfDept.getText();
    				String inAddr = tfAddr.getText();
    				System.out.println("ok버튼이 눌러짐..."+ inName + "  " + inDept + "  " + inAddr);
    				
    			}
    		});
    		btnCancel.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				tfName.setText("");tfDept.setText(""); tfAddr.setText("");
    			}
    		});
    		pnBtn.add(btnOk);	
    		pnBtn.add(btnCancel);
    		c.add(pnBtn);
    		setSize(300,150);
    		setVisible(true);
    	}
    	
    public static void main(String [] args) {
    	new TextFieldEx();
    	}
    }
    

     

     

     

     

    실행결과

     

     

     

     

     

     

     

     

    JTextField의 주요 메소드

     

    문자열 편집 불가능하게 하기  JTextField.setEditable(false);

    입력 창에 문자열 출력  JTextField.setText("hello");

    문자열의 폰트 지정  JTextField.setFont(new Font("고딕체", Font.ITALIC, 20)

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    JTextArea, 텍스트영역 컴포넌트

     

     

     

    여러 줄을 입력할 수 있는 텍스트 입력 창

    JScrollPane 컴포넌트에 삽입하면 스크롤바 지원 됨

     

     

     

    생성자

     

     

    JTextArea() 빈 텍스트 영역

    JTextArea( int rows, int cols ) 입력 창이 rows x cols개의 문자 크기인 텍스트영역

    JTextArea( String text ) text 문자열로 초기화된 텍스트영역

    JTextArea( String text, int rows, int cols ) 입력 창이 rows x cols 개의 문자 크기이며 text 문자열로 초기화된 텍스트영역

     

     

     

     

    텍스트 영역 컴포넌트 만들기 예제

     

     

    package project;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class JTextAreaEx extends JFrame {
    	private JTextField tf = new JTextField(20);
    	private JTextArea ta = new JTextArea(7, 20);
    	public JTextAreaEx() {
    		setTitle("텍스트영역 만들기 예제");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container c = getContentPane();
    		c.setLayout(new FlowLayout());
    		c.add(new JLabel("입력 후 <Enter> 키를 입력하세요"));
    		c.add(tf);
    		c.add(new JScrollPane(ta));
    		tf.addActionListener(new ActionListener() {
    			
    			public void actionPerformed(ActionEvent e) {
    				JTextField t = (JTextField)e.getSource();
    				ta.append(t.getText() + "\n");
    				t.setText("");
    			}
    		});
    		setSize(300,300);
    		setVisible(true);
    	}
    	
    public static void main(String [] args) {
    	new JTextAreaEx();
    	}
    }
    

     

     

     

     

    실행결과

     

    이름을 입력받아 엔터키를 누르면 문자열 추가

     

     

     

    반응형

    댓글

Designed by Tistory.