mojo's Blog

JCheckBox / JRadioButton 연습하기 본문

Java

JCheckBox / JRadioButton 연습하기

_mojo_ 2021. 7. 28. 02:35

JCheckBox 구현

import javax.swing.*; // JFrame
import java.awt.*; // Container
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.*;

public class MyFrame extends JFrame {
	
	private JLabel la;
	
	public MyFrame() {
		setTitle("Open Challenge 10");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		ImageIcon icon=new ImageIcon("C:\\Temp/apple.jpg");
		
		JCheckBox apple=new JCheckBox("사과", icon);
		
		apple.setBorderPainted(true); // 외곽선이 보이도록
		apple.setSelectedIcon(icon);
		
		c.add(apple);
		
		setSize(300, 150); // 프레임 크기 300x300
		setVisible(true); // 프레임 출력
		
		c.setFocusable(true);
		c.requestFocus();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyFrame();
	}

}

 

ItemEvent 를 활용하여 체크박스로 가격 합산하기

import javax.swing.*; // JFrame
import java.awt.*; // Container
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.*;

public class MyFrame extends JFrame {
	
	private JCheckBox []fruits=new JCheckBox[3];
	private String[] names= {"사과","배","체리"};
	private JLabel sumLabel;
	
	class MyItemListener implements ItemListener{ // 체크박스 선택을 위한 listener
		private int sum=0;
		
		@Override
		public void itemStateChanged(ItemEvent e) { // check state가 변경될 경우 실행
			// TODO Auto-generated method stub
			if(e.getStateChange()==ItemEvent.SELECTED) { // 체크박스가 눌린 경우
				if(e.getItem()==fruits[0]) sum+=100;
				else if(e.getItem()==fruits[1]) sum+=500;
				else if(e.getItem()==fruits[2]) sum+=20000;
			}
			else { // 체크박스를 해제한 경우
				if(e.getItem()==fruits[0]) sum-=100;
				else if(e.getItem()==fruits[1]) sum-=500;
				else if(e.getItem()==fruits[2]) sum-=20000;
			}
			sumLabel.setText("현재 "+sum+"원 입니다.");
		}
		
	}
	
	public MyFrame() {
		setTitle("11장 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
	
		c.add(new JLabel("사과  100원, 배 500원, 체리 20000원"));
		
		MyItemListener listener=new MyItemListener();
		for(int i=0;i<fruits.length;i++) {
			fruits[i]=new JCheckBox(names[i]);
			fruits[i].setBorderPainted(true); //체크박스 외곽선이 보이도록
			c.add(fruits[i]);
			fruits[i].addItemListener(listener);
		}
		
		sumLabel=new JLabel("현재 0원 입니다.");
		c.add(sumLabel);
		
		setSize(250, 200); // 프레임 크기 300x300
		setVisible(true); // 프레임 출력
		
		c.setFocusable(true);
		c.requestFocus();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyFrame();
	}

}

 

JRadioButton 

import javax.swing.*; // JFrame
import java.awt.*; // Container
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.*;

public class MyFrame extends JFrame {
	
	public MyFrame() {
		setTitle("11장 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
	
		ImageIcon narshaIcon = new ImageIcon("C:\\Temp/Narsha.jpg");
		
		ButtonGroup g=new ButtonGroup(); //3개의 라이도 버튼을 묶을 버튼 그룹 객체를 생성함 (하나만 선택되도록 함)
		
		JRadioButton apple = new JRadioButton("사과");
		JRadioButton pear = new JRadioButton("배",true);
		JRadioButton narsha = new JRadioButton("나르샤",narshaIcon);
		
		narsha.setBorderPainted(true);
		
		g.add(apple);
		g.add(pear);
		g.add(narsha);
		
		c.add(apple);
		c.add(pear);
		c.add(narsha);
		
		setSize(250, 200); // 프레임 크기 300x300
		setVisible(true); // 프레임 출력
		
		c.setFocusable(true);
		c.requestFocus();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyFrame();
	}

}

 

JRadioButton과 Item 이벤트 처리 code

import javax.swing.*; // JFrame
import java.awt.*; // Container
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.*;

public class MyFrame extends JFrame {
	
	private JRadioButton []radio=new JRadioButton[3];
	private String []text= {"사과","배","체리"};
	private ImageIcon []image= {
			new ImageIcon("C:\\Temp/apple.jpg"),
			new ImageIcon("C:\\Temp/bear.jpg"),
			new ImageIcon("C:\\Temp/cherry.png")
	};
	private JLabel imageLabel=new JLabel();
	
	class MyItemListener implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			if(e.getStateChange() == ItemEvent.DESELECTED) return;
			if(radio[0].isSelected()) {
				imageLabel.setIcon(image[0]);
			}
			else if(radio[1].isSelected()) {
				imageLabel.setIcon(image[1]);
			}
			else if(radio[2].isSelected()) {
				imageLabel.setIcon(image[2]);
			}
		}
		
	}
	
	public MyFrame() {
		setTitle("11장 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
	
		JPanel radioPanel=new JPanel();
		radioPanel.setBackground(Color.GRAY);
		
		ButtonGroup g=new ButtonGroup();
		for(int i=0;i<radio.length;i++) {
			radio[i]=new JRadioButton(text[i]);
			g.add(radio[i]);
			radioPanel.add(radio[i]);
			radio[i].addItemListener(new MyItemListener());
		}
		
		radio[2].setSelected(true); // 체리 라디오버튼을 클릭한 상태로 함
		c.add(radioPanel, BorderLayout.NORTH);
		c.add(imageLabel, BorderLayout.CENTER);
		imageLabel.setHorizontalAlignment(SwingConstants.CENTER); // 이미지 중앙정렬
		
		setSize(250, 200); // 프레임 크기 300x300
		setVisible(true); // 프레임 출력
		
		c.setFocusable(true);
		c.requestFocus();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyFrame();
	}

}

 

'Java' 카테고리의 다른 글

JList, JList의 아이템 변경하기  (0) 2021.07.29
JTextField, JTextArea  (0) 2021.07.29
명품 JAVA Programming 제 10장 실습문제 5번  (0) 2021.07.28
명품 JAVA Programming 제 10장 Open Challenge  (0) 2021.07.28
KeyListener / MouseListener  (0) 2021.07.28
Comments