欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-23 21:14:46
复制代码 代码如下:package com.liuxing.test;import java.awt.color;import java.awt.event.actione...

复制代码 代码如下:

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);
    }
  }
}