用AWT和布局管理器设计调色板界面,有rgb三个颜色。点击加减按钮能改变颜色值中各色值的分量,下面面板的颜色同步改变。Java语言
时间: 2024-01-21 19:16:19 浏览: 89
下面是一个使用AWT和GridLayout布局管理器设计的调色板界面,其中包含三个滑动条分别用于调整红、绿、蓝三种颜色的值,加减按钮用于改变颜色值中各色值的分量,下面的面板会与之同步改变。
```java
import java.awt.*;
import java.awt.event.*;
public class ColorPalette extends Frame implements AdjustmentListener, ActionListener {
private Scrollbar redScrollbar, greenScrollbar, blueScrollbar;
private Label redLabel, greenLabel, blueLabel;
private Button plusRedButton, minusRedButton, plusGreenButton, minusGreenButton, plusBlueButton, minusBlueButton;
private Panel colorPanel;
public ColorPalette() {
super("Color Palette");
// 创建三个滑动条和对应的标签
redScrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 127, 1, 0, 255);
greenScrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 127, 1, 0, 255);
blueScrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 127, 1, 0, 255);
redLabel = new Label("R: " + redScrollbar.getValue());
greenLabel = new Label("G: " + greenScrollbar.getValue());
blueLabel = new Label("B: " + blueScrollbar.getValue());
// 创建加减按钮
plusRedButton = new Button("+");
minusRedButton = new Button("-");
plusGreenButton = new Button("+");
minusGreenButton = new Button("-");
plusBlueButton = new Button("+");
minusBlueButton = new Button("-");
// 创建颜色面板
colorPanel = new Panel();
colorPanel.setBackground(new Color(redScrollbar.getValue(), greenScrollbar.getValue(), blueScrollbar.getValue()));
// 设置布局管理器
setLayout(new GridLayout(4, 3));
// 添加组件
add(redLabel);
add(redScrollbar);
add(plusRedButton);
add(greenLabel);
add(greenScrollbar);
add(plusGreenButton);
add(blueLabel);
add(blueScrollbar);
add(plusBlueButton);
add(new Label());
add(new Label());
add(minusRedButton);
add(new Label());
add(new Label());
add(minusGreenButton);
add(new Label());
add(new Label());
add(minusBlueButton);
add(new Label());
add(new Label());
add(colorPanel);
// 添加事件监听器
redScrollbar.addAdjustmentListener(this);
greenScrollbar.addAdjustmentListener(this);
blueScrollbar.addAdjustmentListener(this);
plusRedButton.addActionListener(this);
minusRedButton.addActionListener(this);
plusGreenButton.addActionListener(this);
minusGreenButton.addActionListener(this);
plusBlueButton.addActionListener(this);
minusBlueButton.addActionListener(this);
// 设置窗口大小和可见性
setSize(400, 200);
setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
// 更新颜色面板和标签的颜色值显示
colorPanel.setBackground(new Color(redScrollbar.getValue(), greenScrollbar.getValue(), blueScrollbar.getValue()));
redLabel.setText("R: " + redScrollbar.getValue());
greenLabel.setText("G: " + greenScrollbar.getValue());
blueLabel.setText("B: " + blueScrollbar.getValue());
}
public void actionPerformed(ActionEvent e) {
// 根据按钮的不同,改变对应颜色值滑动条的值
if (e.getSource() == plusRedButton) {
redScrollbar.setValue(redScrollbar.getValue() + 1);
} else if (e.getSource() == minusRedButton) {
redScrollbar.setValue(redScrollbar.getValue() - 1);
} else if (e.getSource() == plusGreenButton) {
greenScrollbar.setValue(greenScrollbar.getValue() + 1);
} else if (e.getSource() == minusGreenButton) {
greenScrollbar.setValue(greenScrollbar.getValue() - 1);
} else if (e.getSource() == plusBlueButton) {
blueScrollbar.setValue(blueScrollbar.getValue() + 1);
} else if (e.getSource() == minusBlueButton) {
blueScrollbar.setValue(blueScrollbar.getValue() - 1);
}
}
public static void main(String[] args) {
new ColorPalette();
}
}
```
阅读全文