'[예제] JRadioButton'에 해당되는 글 1건

  1. 2016.12.09 [예제] JRadioButton

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


import javax.swing.ButtonGroup;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;


public class JRadioButtonEx extends JFrame implements ActionListener{

JPanel p = new JPanel(new GridLayout(4, 1));

JRadioButton r1 = new JRadioButton("suicide squad"), 

r2 = new JRadioButton("Harly"), 

r3 = new JRadioButton("deadshot"), 

r4 = new JRadioButton("joker");

ButtonGroup bg = new ButtonGroup();

JLabel lbl;

ImageIcon ii1 = new ImageIcon("image/ssa.jpg");

ImageIcon ii2 = new ImageIcon("image/ssh.jpg");

ImageIcon ii3 = new ImageIcon("image/ssd.jpg");

ImageIcon ii4 = new ImageIcon("image/ssj.jpg");

public JRadioButtonEx() {

p.add(r1); p.add(r2); p.add(r3); p.add(r4);

bg.add(r1); bg.add(r2); bg.add(r3); bg.add(r4);

r1.setSelected(true); //라디오버튼 선택

r1.addActionListener(this);

r2.addActionListener(this);

r3.addActionListener(this);

r4.addActionListener(this);

add(p, "West");

lbl = new JLabel(ii1);

add(lbl, "Center");

setSize(500, 300);

setVisible(true);

}

public void actionPerformed(ActionEvent ae){

Object obj = ae.getSource();

if(obj == r1){

lbl.setIcon(ii1);

}else if(obj == r2){

lbl.setIcon(ii2);

}else if(obj == r3){

lbl.setIcon(ii3);

}else if(obj == r4){

lbl.setIcon(ii4);

}

}

public static void main(String[] args) {

new JRadioButtonEx();


}


}



'응용 SoftWare > JAVA' 카테고리의 다른 글

JSlider  (0) 2016.12.12
메모장 (글자 굵기, 기울기, 초기화, 색, 종료창)  (0) 2016.12.12
JList  (0) 2016.12.09
JComboBox  (0) 2016.12.09
Vector  (0) 2016.12.09
Posted by Hyun CHO
,