龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

java使用颜色选择器示例分享

时间:2014-05-20 02:04来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了java使用颜色选择器示例,需要的朋友可以参考下

代码如下:

package com.liuxing.test;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ColorChooser extends JFrame {
  private JLabel sampleText = new JLabel("Label");
  private JButton chooseButton = new JButton("Choose Color");

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

  public ColorChooser() {
    this.setSize(300, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel1 = new JPanel();
    sampleText.setBackground(null);
    panel1.add(sampleText);

    chooseButton.addActionListener(new ButtonListener());
    panel1.add(chooseButton);

    this.add(panel1);
    this.setVisible(true);
  }

  private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Color c = JColorChooser.showDialog(null, "Choose a Color", sampleText.getForeground());
      if (c != null)
        sampleText.setForeground(c);
    }
  }
}

精彩图集

赞助商链接