正文

JColorChooser用法2005-09-17 00:55:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hurongliang/4843.html

分享到:

import java.awt.Dimension;
import javax.swing.JColorChooser;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import java.awt.FlowLayout;

public class JColorChooserDemo implements ActionListener
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame ("JColorChooserDemo");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  MyPanel panel = new MyPanel();
  frame.getContentPane ().add (panel);
  frame.pack();
  frame.show();
 }
  
 public void actionPerformed(ActionEvent e)
 {
 }
}

class MyPanel extends JPanel implements ActionListener
{
 private JButton button, rgb, red, green, blue;
 private Color color = new Color (0, 0, 0);
 public MyPanel()
 {
  button = new JButton ("Get Color");
  rgb = new JButton ("RGB: ");
  red = new JButton ("Red: ");
  green = new JButton ("Green: ");
  blue = new JButton ("Blue: ");
  button.addActionListener (this);
  
  setPreferredSize (new Dimension (550,250));
  setLayout (new FlowLayout (FlowLayout.CENTER, 5,5));
  setBackground (color);
  add (button);
  add (rgb);
  add (red);
  add (green);
  add (blue);
 }
 
 public void actionPerformed(ActionEvent e)
 {
  color = JColorChooser.showDialog(this, "Choose Color", color);
  setBackground (color);
  button.setText ("Get again");
  rgb.setText ("RGB: " + color.getRGB());
  red.setText ("Red: " + color.getRed());
  green.setText ("Green: " + color.getGreen());
  blue.setText ("Blue: " + color.getBlue());
 }
}

阅读(4827) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册