관리 메뉴

moozi

Gallery02 본문

TIS_2018/응용sw2018_2기

Gallery02

moozi 2018. 8. 21. 10:21

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Gallery02 extends JFrame{
 
 private String[] fruits= {"사과","배","체리"};
 private JComboBox<String> strCombo=new JComboBox<String>(fruits);
 private JLabel imageLabel=new JLabel();
 private ImageIcon[] image= {
        new ImageIcon("images/apple.png"),
        new ImageIcon("images/pear.png"),
        new ImageIcon("images/cherry.png")
        };
 
 public Gallery02() {
  this.setTitle("갤러리");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  Container c=this.getContentPane();
  
  //라디오버튼이 올라갈 패널
  JPanel radioPanel=new JPanel();
  radioPanel.setBackground(Color.GRAY);
  
  //콤포박스 이벤트처리
  this.strCombo.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
    JComboBox<String> cb=(JComboBox<String>)e.getSource();
    int index=cb.getSelectedIndex();
    imageLabel.setIcon(image[index]);
    
   }});
  
  
  //라디오패널에 콤포박스추가
  radioPanel.add(strCombo);
  
  
  
  //첫번째 이미지 선택.기본값.
  this.imageLabel.setIcon(image[0]);
  this.imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
    
  c.add(radioPanel, BorderLayout.NORTH); 
  c.add(imageLabel, BorderLayout.CENTER);
  this.setSize(500, 500);
  this.setVisible(true);
  
 }
 
 
 public static void main(String[] args) {
  new Gallery02();

 }
}

'TIS_2018 > 응용sw2018_2기' 카테고리의 다른 글

자바연습문제15  (0) 2018.08.23
자바연습문제14  (0) 2018.08.21
자바연습문제13  (0) 2018.08.20
이미지버튼  (0) 2018.08.20
이벤트처리  (0) 2018.08.20
Comments