import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
public class JButtonTest implements ActionListener{
JFrame frm = new JFrame("JButton 만들기");
Dimension dim = new Dimension(500, 500); //width, height 정수 저장 클래스
Rectangle rect = new Rectangle(100, 100, 500, 500); //x, y, width, height 정수 저장 클래스
///
ImageIcon ii1 = new ImageIcon("image/hyun logo III.jpg");
ImageIcon ii2 = new ImageIcon("image/car1.jpg");
ImageIcon ii3 = new ImageIcon("image/car2.jpg");
ImageIcon ii4 = new ImageIcon("image/car3.jpg");
ImageIcon ii5 = new ImageIcon("image/car4.jpg");
JPanel p1 = new JPanel();
JButton but1 = new JButton("첫번째");
JButton but2 = new JButton("두번째");
JButton but3 = new JButton("세번째");
JPanel p2 = new JPanel();
JLabel lbl = new JLabel("라벨값");
JLabel lbl2 = new JLabel(ii1);
//ImageButton
JPanel p3 = new JPanel();
JButton imgBtn1, imgBtn2, imgBtn3;
//Radio버튼
JPanel p4 = new JPanel();
JRadioButton rBtn1 = new JRadioButton("ONE");
JRadioButton rBtn2 = new JRadioButton("TWO");
JRadioButton rBtn3 = new JRadioButton("THREE");
ButtonGroup bg = new ButtonGroup();
//toggle버튼
JPanel p5 = new JPanel();
JToggleButton tBtn1 = new JToggleButton("복숭아");
JToggleButton tBtn2 = new JToggleButton("파인애플");
JToggleButton tBtn3 = new JToggleButton("바나나");
public JButtonTest() {
//레이아웃 바꾸기
frm.setLayout(new GridLayout(10, 1, 5, 5));
p1.add(but1); p1.add(but2); p1.add(but3);
frm.add(p1);
//라벯
p2.setLayout(new GridLayout(1, 1));
p2.setBackground(Color.white);
p2.add(lbl);
frm.add(p2);
frm.add(lbl2);
lbl2.setOpaque(true);//투명처리 해제(true:해제, false:설정)
lbl2.setBackground(Color.white);
//이미지 버튼
imgBtn1 = new JButton(ii3);
imgBtn2 = new JButton(ii4);
imgBtn3 = new JButton(ii5);
p3.add(imgBtn1); p3.add(imgBtn2); p3.add(imgBtn3);
frm.add(p3);
//버튼 선택시 이미지 변경
imgBtn2.setPressedIcon(ii2); //마우스 클릭시
imgBtn2.setRolloverIcon(ii1); //마우스 오버 시
imgBtn1.setEnabled(false);//활성화(true), 비활성화(false)
//radioButton
bg.add(rBtn1); bg.add(rBtn2); bg.add(rBtn3);
p4.add(rBtn1); p4.add(rBtn2); p4.add(rBtn3);
frm.add(p4);
//toggle버튼
p5.add(tBtn1); p5.add(tBtn2); p5.add(tBtn3);
frm.add(p5);
// TODO Auto-generated constructor stub
//frm.setSize(dim); //w, h
//frm.setBounds(rect); //x, y, w, h
frm.pack(); //컨테이너 내용만큼 창 크기 자동설정
frm.setVisible(true); //창 뛰우기
//주로 창 뛰운 뒤에 이벤트 등록
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
imgBtn1.addActionListener(this);
imgBtn2.addActionListener(this);
imgBtn3.addActionListener(this);
}
//overriding 위치
public void actionPerformed(ActionEvent ae){
//
//String msg = ae.getActionCommand();
//lbl.setText(msg);
Object eventObj = ae.getSource();
JButton eventBtn = (JButton)eventObj;
if(imgBtn1.equals(eventBtn)){
lbl.setText("Red버튼");
}else if(imgBtn2.equals(eventBtn)){
lbl.setText("Blue버튼");
}else if(imgBtn3.equals(eventBtn)){
lbl.setText("green버튼");
}else{
lbl.setText(eventBtn.getText());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JButtonTest bt = new JButtonTest();
}
}