์ฒดํฌ๋ฐ์ค
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class JCheckBoxEx extends JFrame{
JCheckBoxEx(){
setTitle("JCheckBoxEx");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
ImageIcon cherryIcon = new ImageIcon("cherry.jpg");
ImageIcon scherryIcon = new ImageIcon("selectedCherry.jpg");
JCheckBox apple = new JCheckBox("์ฌ๊ณผ");
JCheckBox pear = new JCheckBox("๋ฐฐ");
JCheckBox cherry = new JCheckBox("์ฒด๋ฆฌ",cherryIcon);
cherry.setBorderPainted(true);
cherry.setSelectedIcon(scherryIcon);
c.add(apple); c.add(pear); c.add(cherry);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new JCheckBoxEx();
}
}
์์ดํ ์ด๋ฒคํธ(์ฒดํฌ๋ฐ์ค, ๋ผ๋์ค๋ฒํผ)
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JCheckBoxEx extends JFrame{
JCheckBox [] fruits = new JCheckBox[3];
JLabel sumLa = new JLabel("ํ์ฌ 0์์
๋๋ค.");
int sum = 0;
JCheckBoxEx(){
setTitle("JCheckBoxEx");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new JLabel("์ฌ๊ณผ: 3000์, ๋ฐฐ: 2000์, ์ฒด๋ฆฌ: 10000์"));
String[] names = {"์ฌ๊ณผ", "๋ฐฐ", "์ฒด๋ฆฌ"};
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(new MyItemListener());
}
c.add(sumLa);
setSize(300,300);
setVisible(true);
}
class MyItemListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
if(e.getItem() == fruits[0])
sum += 3000;
else if(e.getItem() == fruits[1])
sum += 2000;
else
sum += 10000;
}
else{
if(e.getItem() == fruits[0])
sum -= 3000;
else if(e.getItem() == fruits[1])
sum -= 2000;
else
sum -= 10000;
}
sumLa.setText("ํ์ฌ ๊ฐ๊ฒฉ์"+sum+"์์
๋๋ค.");
}
}
public static void main(String[] args) {
new JCheckBoxEx();
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
public class JRadioButtonEx extends JFrame{
JRadioButton []radio = new JRadioButton[3];
JLabel imgLa = new JLabel();
ImageIcon []imgIcon = {new ImageIcon("apple.jpg"), new ImageIcon("banana.jpg"), new ImageIcon("kiwi.jpg")};
JRadioButtonEx(){
setTitle("JRadioButtonEx");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());
//๋ผ๋์ค ๋ฒํผ ํจ๋
JPanel radioPane = new JPanel();
radioPane.setBackground(Color.LIGHT_GRAY);
radioPane.setLayout(new FlowLayout());
//๋ฒํผ ๊ทธ๋ฃน ์์ฑ
ButtonGroup g = new ButtonGroup();
String []names = {"์ฌ๊ณผ","๋ฐ๋๋","ํค์"};
for(int i=0; i<radio.length; i++) {
radio[i] = new JRadioButton();
g.add(radio[i]);
radioPane.add(radio[i]);
radio[i].addItemListener(new MyItemListener());
}
radio[0].setSelected(true);
c.add(radioPane, BorderLayout.NORTH);
c.add(imgLa,BorderLayout.CENTER);
imgLa.setHorizontalAlignment(SwingConstants.CENTER);
setSize(300,300);
setVisible(true);
}
class MyItemListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.DESELECTED)
return;
if(radio[0].isSelected())
imgLa.setIcon(imgIcon[0]);
else if(radio[1].isSelected())
imgLa.setIcon(imgIcon[1]);
else
imgLa.setIcon(imgIcon[2]);
}
}
public static void main(String[] args) {
new JRadioButtonEx();
}
}
728x90
๋ฐ์ํ
0