본문 바로가기
프로그래밍/② JAVA(자바) 프로젝트

② JAVA(자바) 프로젝트-4 java swing JColorChooser 을 이용한 색선택하기 == Java swing Choosing colors with JColorChooser

by ronul 2017. 4. 23.
300x250

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ColorChoice extends JFrame {
 Container contentPane;
 JLabel lbl = new JLabel("Color");
 ColorChoice() {
  setTitle("선택");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  contentPane = getContentPane();
  lbl.setHorizontalAlignment(SwingConstants.CENTER);
  lbl.setFont(new Font("fontname", Font.ITALIC, 30));
  contentPane.add(lbl, BorderLayout.CENTER);
  createMenu();
  setSize(300,300);
  setVisible(true);
 }

 void createMenu() {
  JMenuBar manue = new JMenuBar();
  JMenuItem colorMenuItem = new JMenuItem("Color");
  JMenu fMnu = new JMenu("change");
  colorMenuItem.addActionListener(new MenuActionListener());
  fMnu.add(colorMenuItem);
  manue.add(fMnu);
  this.setJMenuBar(manue);
 }

 

 

 

 

 

  class MenuActionListener implements ActionListener {  
  JColorChooser choice = new JColorChooser();
  public void actionPerformed(ActionEvent e) {
   String cmd = e.getActionCommand();
   if(cmd.equals("Color")) {   
    Color selctColor = choice.showDialog(null,"Color",Color.YELLOW); 
    if(selctColor != null)
     lbl.setForeground(selctColor);
   }
  }
 }

 public static void main(String [] args) {
  new ColorChoice();
 }
}

 

간단하게 만든 색선택해서 바까주는 프로그램 입니다.~~

300x250

댓글