为什么不说 这个 ColorFormat 是做什么用的呢?
这样的话大家都会做>要求:
> 继承java.text.Format类并实现其以下抽象方法class ColorFormat extends java.text.Format{
format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {}
formatToCharacterIterator(Object obj) {}
parseObject(String source, ParsePosition pos) {}
}这就算是达到了你的要求
这样的话大家都会做>要求:
> 继承java.text.Format类并实现其以下抽象方法class ColorFormat extends java.text.Format{
format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {}
formatToCharacterIterator(Object obj) {}
parseObject(String source, ParsePosition pos) {}
}这就算是达到了你的要求
What is a ColorFormat?
A DateFormat formats the text for Dates,
A NumberFormat formats the text for Numbers,
And A ColorFormat formats the text for Colors like "Red", "Orange", "Yellow" and so on?
好像染色体是Chrom....
首先建议参考JColorChooser,
其次我不认为用JSpinner有什么好,建议用JComboBox,让它可以编辑即可,
IMHO,JSpinner的好处是有一个上下箭头,输入的值可以随箭头而改变,
但我看不出 #000000(BLACK) 按上箭头应该变成什么,无论是#000001还是#010101都没有太大意义,(三种都颜色太接近了,几乎无法分辨)如果要按箭头,从BLACK到#FFFFFF(WHITE)(0-F 不用0-z,因为用16进制表示方便,且是24位色),要按多数次我建议用JComboBox的原因是,你可以先将一个预设的调色板放进去,用户可随意选择,
如果想微调,可以在上面更改数字,并将新值放入原来的调色板中
他们和你没有任何关系,我是这么认为的
的到3个0-255即3个两位16进制的数,R,G,B
Color c=new Color(R,G,B);
22530630
import javax.swing.*;public class Application1 extends JFrame {
public static void main(String[] args) {
Application1 frame = new Application1(); } public Application1() { JSpinner s = new JSpinner(new SpinnerColorModel());
( (JSpinner.DefaultEditor) s.getEditor()).getTextField().setEditable(true);
getContentPane().add(s);
this.pack();
this.show();
}
}
class SpinnerColorModel extends AbstractSpinnerModel { private Color value; public SpinnerColorModel(Color value) {
if (value == null) {
throw new IllegalArgumentException("value is null");
} this.value = value;
} public SpinnerColorModel() {
this(new Color(0x000000));
} public Object getNextValue() {
int rgb = 0x00ffffff & value.getRGB();
return (rgb < 0xFFFFFF) ? new Color(rgb + 1) : null; } public Object getPreviousValue() {
int rgb = 0x00ffffff & value.getRGB();
System.out.println(rgb);
return (rgb > 0x000000) ? new Color(rgb - 1) : null; } public Color getColor() {
return value;
} public Object getValue() {
String str = Integer.toHexString(value.getRGB()).toUpperCase();
str = str.substring(2);
return '#' + str;
} public void setValue(Object value) {
if ( (value == null) || ! (value instanceof Color)) {
throw new IllegalArgumentException("null value");
}
if (!value.equals(this.value)) {
this.value = (Color) value;
fireStateChanged();
}
}
}当我在JFormattedTextField输入一个合法的Color格式的值后并没又改变value的值,怎么才能解决这个问题?
if (value == null) {
throw new IllegalArgumentException("null value");
}
if (!value.equals(this.value)) {
if(value instanceof String){
String s=(String)value;
this.value=new Color(Integer.parseInt(s.substring(1),16));
}else if(value instanceof Color){
this.value = (Color) value;
}
System.out.println(this.value);
fireStateChanged();
}
}
另外,如果可以你的方法中的参数不要和字段重名,否则每次this.value=value;太累了
为什么不setValue(Object v)
我前几天还写了一个 还可以取屏幕颜色
(主要是由于 ColorPad 到期了 又找不到好的取色器 只好自己写了)
好像还用到了 shine333(shine) 的 MouseDrag 方法 :)
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Colorer extends JFrame { GridBagLayout gridBagLayout1 = new GridBagLayout();
JPanel jPanel1 = new JPanel();
JScrollBar jScrollBar1 = new JScrollBar();
JScrollBar jScrollBar2 = new JScrollBar();
JScrollBar jScrollBar3 = new JScrollBar();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JButton jButton1 = new JButton();
Border border1; public static void main(String[] args) {
Colorer c = new Colorer();
c.setTitle("Color Palette");
c.setLocation(100, 30);
c.setSize(350, 150);
c.show();
} public Colorer() {
try {
jbInit();
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//SwingUtilities.updateComponentTreeUI(this);
} catch(Exception e) {
e.printStackTrace();
}
} private void jbInit() throws Exception {
border1 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(171, 172, 172));
this.getContentPane().setLayout(gridBagLayout1);
jScrollBar1.setMaximum(265);
jScrollBar1.setOrientation(JScrollBar.HORIZONTAL);
jScrollBar1.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
jScrollBar1_adjustmentValueChanged(e);
}
});
jScrollBar2.setMaximum(265);
jScrollBar2.setOrientation(JScrollBar.HORIZONTAL);
jScrollBar2.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
jScrollBar2_adjustmentValueChanged(e);
}
});
jScrollBar3.setMaximum(265);
jScrollBar3.setOrientation(JScrollBar.HORIZONTAL);
jScrollBar3.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
jScrollBar3_adjustmentValueChanged(e);
}
});
jButton1.setToolTipText("Copy selected text");
jButton1.setText("Copy");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jPanel1.setBorder(border1);
jPanel1.setToolTipText("Drag to the color !");
jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
jPanel1_mouseDragged(e);
}
});
jTextField1.setSelectionStart(0);
jTextField1.setText("0");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField1_actionPerformed(e);
}
});
jTextField2.setText("0");
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField2_actionPerformed(e);
}
});
jTextField3.setText("0");
jTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField3_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, new GridBagConstraints(0, 0, 1, 3, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 2, 5, 2), 50, 50));
this.getContentPane().add(jScrollBar1, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 100, 0));
this.getContentPane().add(jScrollBar2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
this.getContentPane().add(jScrollBar3, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
this.getContentPane().add(jTextField1, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 20, 0));
this.getContentPane().add(jTextField2, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 20, 0));
this.getContentPane().add(jTextField3, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 20, 0));
this.getContentPane().add(jTextField4, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 2, 5, 2), 0, 0));
this.getContentPane().add(jButton1, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 5), 0, 0)); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); } void jButton1_actionPerformed(ActionEvent e) {
this.jTextField4.copy();
} void jScrollBar1_adjustmentValueChanged(AdjustmentEvent e) {
this.jTextField1.setText(this.jScrollBar1.getValue()+"");
this.jScrollBar1.setBackground(new Color(this.jScrollBar1.getValue(),0,0));
this.indicateColor();
} void jScrollBar2_adjustmentValueChanged(AdjustmentEvent e) {
this.jTextField2.setText(this.jScrollBar2.getValue()+"");
this.jScrollBar2.setBackground(new Color(0,this.jScrollBar2.getValue(),0));
this.indicateColor();
} void jScrollBar3_adjustmentValueChanged(AdjustmentEvent e) {
this.jTextField3.setText(this.jScrollBar3.getValue()+"");
this.jScrollBar3.setBackground(new Color(0,0,this.jScrollBar3.getValue()));
this.indicateColor();
}//----------------------------------------------------------
void jTextField1_actionPerformed(ActionEvent e) {
this.indicateColor();
} void jTextField2_actionPerformed(ActionEvent e) {
this.indicateColor();
} void jTextField3_actionPerformed(ActionEvent e) {
this.indicateColor();
} void jPanel1_mouseDragged(MouseEvent e) {
int x = ((Component)e.getSource()).getLocationOnScreen().x + e.getX();
int y = ((Component)e.getSource()).getLocationOnScreen().y + e.getY();
Color c = this.getColor(x,y);
this.jPanel1.setBackground(c);
this.formatScroolBar(c.getRed(),c.getGreen(),c.getBlue());
} public Color getColor(int x,int y){
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException ex) {
System.out.println(ex);
} return robot.getPixelColor(x, y);
}
private void indicateColor(){
String RGBstr = this.jTextField1.getText() +","+ this.jTextField2.getText() +","+ this.jTextField3.getText() ;
try{
int R = Integer.parseInt( this.jTextField1.getText() );
int G = Integer.parseInt( this.jTextField2.getText() );
int B = Integer.parseInt( this.jTextField3.getText() );
this.jPanel1.setBackground(new Color(R,G,B));
this.jTextField4.setText("RGB: "+R +","+ G +","+ B +" HEX:"+ this.getFormatedHex(R) + this.getFormatedHex(G) + this.getFormatedHex(B));
this.formatScroolBar(R,G,B);
}catch(Exception e){
this.jTextField4.setText("NumberFormatException: '" + RGBstr + "' is illegal");
} } private void formatScroolBar(int R,int G,int B){
this.jScrollBar1.setValue(R);
this.jScrollBar2.setValue(G);
this.jScrollBar3.setValue(B);
} private String getFormatedHex(int i){
String s = Integer.toHexString(i);
if(i < 16)
s = "0" + s;
return s.toUpperCase();
}}