📄 swingcolortext.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class swingcolortext extends JFrame
{
swingcolortext rgbcontrol,hsbcontrol;
JPanel swatch;
public swingcolortext()
{
super("coler text");
JPanel pane=new JPanel();
pane.setLayout(new GridLayout(1,3,5,15));
swatch=new JPanel();
swatch.setBackground(Color.black);
String[] rgblabels={"Red","Green","Blue"};
rgbcontrol=new SwingColorControls(this,rgblabels);
String [] hsblabels={"hue","saturation","brightness"};
hsbcontrol=new SwingColorControls(this,hsblabels);
pane.add(swatch);
pane.add(rgbcontrol);
pane.add(hsbcontrol);
setContentPane(pane);
}
public static void main(String[] args)
{
JFrame frame=new swingcolortext();
frame.addWindowListener(new quitEvent());
frame.pack();
frame.setVisible(true);
}
public Inserts getInsets()
{
return new Inserts(10,10,10,10);
}
void updata(SwingColorControls control)
{
Color c; // get values
int[] value=new int[3];
for (int i=0;i<3;i++)
{
value[i]=Integer.parseInt(control.tfield[i].getText());
if((value[i]<0)||(value[i]>255))
{
value[i]=0;
control.tfield[i].setText(""+value[i]);
}
}
if (control==rgbcontrol)
{ c=new Color(value[0],value[1],value[2]);
float[] HSB=Color.RGBtoHSB(value[0],value[1],value[2],(new float[3]));
HSB[0]*=360;
HSB[1]*=100;
HSB[2]*=100;
for(int i=0;i<3;i++)
{
hsbcontrol.tfield[i].setText(String.valueOf((int)HSB[i]));
}
}
else
{
c=Color.getHSBColor((float)value[0]/360,(float)value[1]/100,(float)value[2]/100);
rgbcontrol.tfield[0].setText(String.valueOf(c.getRed()));
rgbcontrol.tfield[1].setText(String.valueOf(c.getGreen()));
rgbcontrol.tfield[2].setText(String.valueOf(c.getBlue()));
}
swatch.setBackground(c);
swatch.repaint();
}
}
class SwingColorControls extends JPanel implements ActionListener, FocusListener
{
swingcolortext frame;
JTextField[] tfield =new JTextField[3];
SwingColorControls(swingcolortext parent,String[] label)
{
frame=parent;
setLayout(new GridLayout(3,2,10,10));
for(int i=0;i<3;i++)
{
tfield[i]=new JTextField("0");
tfield[i].addFocusListener(this);
tfield[i].addActionListener(this);
add(new JLabel(label[i],JLabel.RIGHT));
add(tfield[i]);
}
}
public Insets getInsets()
{ return new Insets(10,10,0,0);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() instanceof JTextField)
frame.updata(this);
}
public void focusLost(FocusEvent evt)
{
frame.updata(this);
}
public void focusGained(FocusEvent evt) {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -