📄 menucolor.java
字号:
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;
TextField colorTextField;
boolean changed = true;
boolean synchronism = false;
Color[] fbgc = new Color[2];
Color[] fbgcOld = new Color[2];
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("Foreground", true, gp);
back = new Checkbox("Background", 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("Red");
gbc.gridx = 0;
gbc.gridy = 1;
getContentPane().add(labelRed, gbc);
scrollbarRed = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
scrollbarRed.setValue(fgc.getRed());
gbc.gridx = 1;
gbc.gridy = 1;
getContentPane().add(scrollbarRed, gbc);
scrollbarRed.addAdjustmentListener(this);
textFieldRed = new TextField(3);
textFieldRed.setText(scrollbarRed.getValue() + "");
textFieldRed.addTextListener(this);
// textFieldRed.setEditable(false);
gbc.gridx = 2;
gbc.gridy = 1;
getContentPane().add(textFieldRed, gbc);
Label labelGreen = new Label("Green");
gbc.gridx = 0;
gbc.gridy = 2;
getContentPane().add(labelGreen, gbc);
scrollbarGreen = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
scrollbarGreen.setValue(fgc.getGreen());
gbc.gridx = 1;
gbc.gridy = 2;
getContentPane().add(scrollbarGreen, gbc);
scrollbarGreen.addAdjustmentListener(this);
textFieldGreen = new TextField(3);
textFieldGreen.setText(scrollbarGreen.getValue() + "");
// textFieldGreen.addTextListener(this);
textFieldGreen.setEditable(false);
gbc.gridx = 2;
gbc.gridy = 2;
getContentPane().add(textFieldGreen, gbc);
Label labelBlue = new Label("Blue");
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;
getContentPane().add(scrollbarBlue, gbc);
scrollbarBlue.addAdjustmentListener(this);
textFieldBlue = new TextField(3);
textFieldBlue.setText(scrollbarBlue.getValue() + "");
// textFieldBlue.addTextListener(this);
textFieldBlue.setEditable(false);
gbc.gridx = 2;
gbc.gridy = 3;
getContentPane().add(textFieldBlue, gbc);
// 2 Buttons: Ok and Cancel
colorButtonOk = new Button("Ok");
gbc.gridx = 4;
gbc.gridy = 1;
gbc.fill = gbc.NONE;
getContentPane().add(colorButtonOk, gbc);
colorButtonOk.addActionListener(this);
colorButtonCancel = new Button("Cancel");
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 TextField("Java awt");
// have to disable this because of 70% decrease in color when
// setEditable(false)
// and color can′t be corrected by increasing 1/(70%) since >255
// 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.fill = gbc.BOTH;
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();
}
}
}
// else if(te.getSource() == textFieldGreen) {
// } conflict with textFieldRed
// else if(te.getSource() == textFieldBlue) {
// }
}// end of TextListener
private Color setColorVarSrollbar() {
return new Color(scrollbarRed.getValue(), scrollbarGreen.getValue(),
scrollbarBlue.getValue());
}
}// end of class MenuColor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -