📄 menucolor.java
字号:
//MenuColor version 1.0 alpha1
//09/13/2001
//author countryman@263.net
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuColor extends JDialog
implements ItemListener,
ActionListener,
TextListener,
AdjustmentListener {
CheckboxGroup gp;
Checkbox fore, back;
Scrollbar scrollbarRed, scrollbarGreen, scrollbarBlue;
TextField textFieldRed, textFieldGreen, textFieldBlue;
Button colorButtonOk, colorButtonCancel;
Checkbox colorCheckbox;
JTextField colorTextField;
Color[] fbgc = new Color[2];
Color[] fbgcOld = new Color[2];
boolean changed = true;
boolean synchronism = false;
boolean isFore = true;
MenuColor(Frame frame, boolean modal) {
super(frame, modal);
}
public Color[] myLayout(Color fgc, Color bgc) {
fbgc[0] = fgc;
fbgc[1] = bgc;
fbgcOld[0] = fgc;
fbgcOld[1] = bgc;
this.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridheight =1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = gbc.HORIZONTAL;
gbc.anchor = gbc.CENTER;
//CheckboxGroup for fore and back ground
gp = new CheckboxGroup();
fore = new Checkbox("前景色", true, gp);
back = new Checkbox("背景色", false, gp);
gbc.gridx = 1;
gbc.gridy = 0;
getContentPane().add(fore, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
getContentPane().add(back, gbc);
fore.addItemListener(this);
back.addItemListener(this);
// 3 groups of Label + ScrollBar + TextField
Label labelRed = new Label("红色");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = gbc.NONE;
getContentPane().add(labelRed, gbc);
scrollbarRed = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
scrollbarRed.setValue(fgc.getRed());
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = gbc.HORIZONTAL;
getContentPane().add(scrollbarRed, gbc);
scrollbarRed.addAdjustmentListener(this);
textFieldRed = new TextField(1);
textFieldRed.setText(scrollbarRed.getValue() + "");
textFieldRed.addTextListener(this);
//textFieldRed.setEditable(false);
gbc.gridx = 2;
gbc.gridy = 1;
gbc.fill = gbc.NONE;
getContentPane().add(textFieldRed, gbc);
Label labelGreen = new Label("绿色");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = gbc.NONE;
getContentPane().add(labelGreen, gbc);
scrollbarGreen = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
scrollbarGreen.setValue(fgc.getGreen());
gbc.gridx = 1;
gbc.gridy = 2;
gbc.fill = gbc.HORIZONTAL;
getContentPane().add(scrollbarGreen, gbc);
scrollbarGreen.addAdjustmentListener(this);
textFieldGreen = new TextField(1);
textFieldGreen.setText(scrollbarGreen.getValue() + "");
//textFieldGreen.addTextListener(this);
textFieldGreen.setEditable(false);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.fill = gbc.NONE;
getContentPane().add(textFieldGreen, gbc);
Label labelBlue = new Label("蓝色");
gbc.fill = gbc.NONE;
gbc.gridx = 0;
gbc.gridy = 3;
getContentPane().add(labelBlue, gbc);
scrollbarBlue = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
scrollbarBlue.setValue(fgc.getBlue());
gbc.gridx = 1;
gbc.gridy = 3;
gbc.fill = gbc.HORIZONTAL;
getContentPane().add(scrollbarBlue, gbc);
scrollbarBlue.addAdjustmentListener(this);
textFieldBlue = new TextField(1);
textFieldBlue.setText(scrollbarBlue.getValue() + "");
//textFieldBlue.addTextListener(this);
textFieldBlue.setEditable(false);
gbc.gridx = 2;
gbc.gridy = 3;
gbc.fill = gbc.NONE;
getContentPane().add(textFieldBlue, gbc);
//2 Buttons: Ok and Cancel
colorButtonOk = new Button(" 确定 ");
gbc.gridx = 4;
gbc.gridy = 1;
gbc.fill = gbc.NONE;
getContentPane().add(colorButtonOk,gbc);
colorButtonOk.addActionListener(this);
colorButtonCancel = new Button("取消");
gbc.gridx = 4;
gbc.gridy = 2;
getContentPane().add(colorButtonCancel,gbc);
colorButtonCancel.addActionListener(this);
//a Checkbox for locking RGB Scrollbar to synchronize
colorCheckbox = new Checkbox("Lock RGB", false);
gbc.gridx = 3;
gbc.gridy = 3;
getContentPane().add(colorCheckbox,gbc);
colorCheckbox.addItemListener(this);
//a TextField for demo the color
colorTextField = new JTextField("画图");
//for user who intends to use pure AWT
//have to change colorTextField from TextField class to JTextField
//in order to avoid the 70% decrease in color
//when setEditable(false) and color neet to be corrected
//by increasing 1/(70%) since >255 some case
//need todo... maybe depend on os and jre version.
colorTextField.setEditable(false);
colorTextField.setSize(90, 60);
colorTextField.setForeground(fgc);
colorTextField.setBackground(bgc);
colorTextField.setFont(new Font("Courier", Font.BOLD + Font.ITALIC, 36));
gbc.gridx = 3;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight =2;
gbc.fill = gbc.BOTH;
//gbc.fill = gbc.HORIZONTAL;
getContentPane().add(colorTextField, gbc);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
this.setLocation(120, 120);
this.setResizable(false);
this.setSize(480,160);
this.setVisible(true);
if(changed) {
return fbgc;
}
else
return fbgcOld;
}//end of myLayout
//ActionListener
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == colorButtonOk) {
changed = true;
dispose();
}
else if(ae.getSource() == colorButtonCancel) {
changed = false;
dispose();
}
}//end of ActionListener
//ItemListener
public void itemStateChanged(ItemEvent ie) {
if(ie.getSource() == fore) {
isFore = true;
scrollbarRed.setValue(fbgc[0].getRed());
textFieldRed.setText(scrollbarRed.getValue() + "");
scrollbarGreen.setValue(fbgc[0].getGreen());
textFieldGreen.setText(scrollbarGreen.getValue() + "");
scrollbarBlue.setValue(fbgc[0].getBlue());
textFieldBlue.setText(scrollbarBlue.getValue() + "");
colorTextField.setForeground(setColorVarSrollbar());
}
else if(ie.getSource() == back) {
isFore = false;
scrollbarRed.setValue(fbgc[1].getRed());
textFieldRed.setText(scrollbarRed.getValue() + "");
scrollbarGreen.setValue(fbgc[1].getGreen());
textFieldGreen.setText(scrollbarGreen.getValue() + "");
scrollbarBlue.setValue(fbgc[1].getBlue());
textFieldBlue.setText(scrollbarBlue.getValue() + "");
colorTextField.setBackground(setColorVarSrollbar());
}
else if(ie.getSource() == colorCheckbox) {
if(colorCheckbox.getState()) {
synchronism = true;
}
else {
synchronism = false;
}
}
}//end of ItemListener
//AdjustmentListener
public void adjustmentValueChanged(AdjustmentEvent ade) {
if(ade.getSource() == scrollbarRed) {
if(synchronism) {
textFieldRed.setText(scrollbarRed.getValue() + "");
scrollbarGreen.setValue(scrollbarRed.getValue());
textFieldGreen.setText(scrollbarRed.getValue() + "");
scrollbarBlue.setValue(scrollbarRed.getValue());
textFieldBlue.setText(scrollbarRed.getValue() + "");
if(isFore) {
colorTextField.setForeground(setColorVarSrollbar());
fbgc[0] = setColorVarSrollbar();
}
else {
colorTextField.setBackground(setColorVarSrollbar());
fbgc[1] = setColorVarSrollbar();
}
}
else {
textFieldRed.setText(scrollbarRed.getValue()+"");
if(isFore) {
colorTextField.setForeground(setColorVarSrollbar());
fbgc[0] = setColorVarSrollbar();
}
else {
colorTextField.setBackground(setColorVarSrollbar());
fbgc[1] = setColorVarSrollbar();
}
}
}
else if(ade.getSource() == scrollbarGreen) {
if(synchronism) {
textFieldGreen.setText(scrollbarGreen.getValue() + "");
scrollbarBlue.setValue(scrollbarGreen.getValue());
textFieldBlue.setText(scrollbarGreen.getValue() + "");
scrollbarRed.setValue(scrollbarGreen.getValue());
textFieldRed.setText(scrollbarGreen.getValue() + "");
if(isFore) {
colorTextField.setForeground(setColorVarSrollbar());
fbgc[0] = setColorVarSrollbar();
}
else {
colorTextField.setBackground(setColorVarSrollbar());
fbgc[1] = setColorVarSrollbar();
}
}
else {
textFieldGreen.setText(scrollbarGreen.getValue() + "");
if(isFore) {
colorTextField.setForeground(setColorVarSrollbar());
fbgc[0] = setColorVarSrollbar();
}
else {
colorTextField.setBackground(setColorVarSrollbar());
fbgc[1] = setColorVarSrollbar();
}
}
}
else if(ade.getSource() == scrollbarBlue) {
if(synchronism) {
textFieldBlue.setText(scrollbarBlue.getValue() + "");
scrollbarRed.setValue(scrollbarBlue.getValue());
textFieldRed.setText(scrollbarBlue.getValue() + "");
scrollbarGreen.setValue(scrollbarBlue.getValue());
textFieldGreen.setText(scrollbarBlue.getValue() + "");
if(isFore) {
colorTextField.setForeground(setColorVarSrollbar());
fbgc[0] = setColorVarSrollbar();
}
else {
colorTextField.setBackground(setColorVarSrollbar());
fbgc[1] = setColorVarSrollbar();
}
}
else {
textFieldBlue.setText(scrollbarBlue.getValue() + "");
if(isFore) {
colorTextField.setForeground(setColorVarSrollbar());
fbgc[0] = setColorVarSrollbar();
}
else {
colorTextField.setBackground(setColorVarSrollbar());
fbgc[1] = setColorVarSrollbar();
}
}
}
}//end of AdjustmentListener
//TextListener
public void textValueChanged(TextEvent te) {
if(te.getSource() == textFieldRed) {
int i = 0;
try {
i = Integer.parseInt(textFieldRed.getText());
} catch(NumberFormatException nfe) {
}
if(i < 0)
i = 0;
if(i > 255)
i = 255;
scrollbarRed.setValue(i);
if(synchronism) {
scrollbarGreen.setValue(scrollbarRed.getValue());
textFieldGreen.setText(scrollbarRed.getValue() + "");
scrollbarBlue.setValue(scrollbarRed.getValue());
textFieldBlue.setText(scrollbarRed.getValue() + "");
if(isFore) {
colorTextField.setForeground(setColorVarSrollbar());
fbgc[0] = setColorVarSrollbar();
}
else {
colorTextField.setBackground(setColorVarSrollbar());
fbgc[1] = setColorVarSrollbar();
}
}
else {
if(isFore) {
colorTextField.setForeground(setColorVarSrollbar());
fbgc[0] = setColorVarSrollbar();
}
else {
colorTextField.setBackground(setColorVarSrollbar());
fbgc[1] = setColorVarSrollbar();
}
}
}
//conflicts with textFieldRed, because of TextListener::textValueChanged()
//neet todo..
//else if(te.getSource() == textFieldGreen) {
//}
//else if(te.getSource() == textFieldBlue) {
//}
}//end of TextListener
private Color setColorVarSrollbar() {
return new Color(scrollbarRed.getValue(),
scrollbarGreen.getValue(),
scrollbarBlue.getValue());
}
public static void main(String[] args) {
JFrame jfr = new JFrame();
final JTextArea ta = new JTextArea(10,10);
jfr.getContentPane().add("Center", ta);
JButton jb = new JButton("SetColor");
jfr.getContentPane().add("South", jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MenuColor mc = new MenuColor(null, true);
Color[] fbgc = new Color[2];
fbgc = mc.myLayout(ta.getForeground(), ta.getBackground());
ta.setForeground(fbgc[0]);
ta.setBackground(fbgc[1]);
ta.setCaretColor(fbgc[0]);
}
});
jfr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jfr.setSize(320,240);
jfr.show();
}
}//end of class MenuColor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -