Java

Java Choice

승모근뭉치 2023. 2. 3. 10:59

프로젝트 내 src 내 javabasic 패키지 내 Ex1Choice.java

package javabasic;

import java.awt.Choice;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class Ex1Choice extends JFrame {
	Choice choice;
	JComboBox<String> combo;

	String[] title = { "빨강", "파랑", "초록", "분홍", "오랜지", "검정" };
	Color[] color = { Color.red, Color.blue, Color.green, Color.pink, Color.orange, Color.black };

	JLabel lblMessage;

	public Ex1Choice(String title) {
		// TODO Auto-generated constructor stub
		super(title);
		this.setBounds(1500, 100, 300, 300);// 시작위치x,y,크기 w,h
		// super로 해도 되고 this로 해도 됨 super는 조상
		// this로 해도 상속을 받아서 괜찮음
		// this.getContentPane().setBackground(Color.orange);//프레임위에 있는 패널의 색상 변경
		this.getContentPane().setBackground(new Color(211, 225, 208));// 프레임위에 있는 패널의 색상 변경
		this.setDesign();// 디자인 코드
		this.setVisible(true);// 보이게 하기
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 프로그램을 종료해주는 메서드
	}

	public void setDesign() {
		this.setLayout(null);
		choice = new Choice();
		for (String t : title)
			choice.add(t);
//		초기값 설정
		choice.select(5);// black으로
		choice.setBounds(20, 20, 80, 30);
		this.add(choice);

		combo = new JComboBox<String>(title);
		combo.setBounds(130, 20, 100, 30);
		this.add(combo);

//		label
		lblMessage = new JLabel("Choice & JComboBox", JLabel.CENTER);
		lblMessage.setBorder(new LineBorder(Color.gray, 3));
		lblMessage.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
		lblMessage.setBounds(20, 150, 250, 50);
		this.add(lblMessage);

//		choice 이벤트
		choice.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				int idx = choice.getSelectedIndex();
				String item = choice.getSelectedItem();
//				글자색 변경
				lblMessage.setForeground(color[idx]);
//				메세지 출력
//				lblMessage.setText("index :"+idx+" select");
				System.out.println(item);
			}
		});

//		combo 이벤트
//		combo.addActionListener(new ActionListener() {
//			
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				// TODO Auto-generated method stub
//				int idx=combo.getSelectedIndex();
//				Object item=combo.getSelectedItem();
//				lblMessage.setForeground(color[idx]);
//				System.out.println(item);
//			}
//		});

//		combo.addItemListener(new ItemListener() {
//			
//			@Override
//			public void itemStateChanged(ItemEvent e) {
//				// TODO Auto-generated method stub
//				int idx=combo.getSelectedIndex();
//				lblMessage.setForeground(color[idx]);
//				System.out.println(idx);//2번 출력
//			}
//		});

		combo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int idx = combo.getSelectedIndex();
				lblMessage.setForeground(color[idx]);
				System.out.println(idx);
			}
		});

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Ex1Choice("choice컴포넌트");
	}

}

 

javax.swing.JComboBox.JComboBox<String>(String[] items)

JComboBox<String>(String[] items) 생성자 함수는

지정된 배열 안에있는 요소들을 포함하는 하나의  JComboBox 를 생성한다.

기본적으로, 배열(그리고 그러므로 그 데이타 모델) 내의 첫 번째 아이템이 선택된다.

JComboBox<String>(String[] items) 생성자 함수에서 파라메터 String[] items 는

콤보 상자에 삽입하기 위한 객체들의 배열이다.