java编写小应用程序画圆,分别用单选钮,列表,下拉列表,菜单实现颜色选择并用此颜色填充圆
时间: 2024-03-20 07:38:53 浏览: 54
好的,这个问题我知道怎么回答。这里我提供一种使用Java Swing编写的解决方案,具体实现如下:
1. 创建一个新的Java项目,并添加Swing库。
2. 创建主类,并在其中添加以下代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame implements ActionListener {
private JRadioButton redButton, greenButton, blueButton;
private JComboBox<String> colorComboBox;
private JList<String> colorList;
private JMenuBar menuBar;
private JMenu colorMenu;
private JMenuItem redMenuItem, greenMenuItem, blueMenuItem;
private Color[] colors = {Color.RED, Color.GREEN, Color.BLUE};
private Color selectedColor;
public Main() {
setTitle("Circle Color");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//创建单选钮
redButton = new JRadioButton("Red");
greenButton = new JRadioButton("Green");
blueButton = new JRadioButton("Blue");
//添加单选钮到一个组中
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(redButton);
buttonGroup.add(greenButton);
buttonGroup.add(blueButton);
//添加事件监听器
redButton.addActionListener(this);
greenButton.addActionListener(this);
blueButton.addActionListener(this);
//创建下拉列表
colorComboBox = new JComboBox<>();
colorComboBox.addItem("Red");
colorComboBox.addItem("Green");
colorComboBox.addItem("Blue");
//添加事件监听器
colorComboBox.addActionListener(this);
//创建列表
colorList = new JList<>();
colorList.setModel(new DefaultListModel<>());
((DefaultListModel<String>) colorList.getModel()).addElement("Red");
((DefaultListModel<String>) colorList.getModel()).addElement("Green");
((DefaultListModel<String>) colorList.getModel()).addElement("Blue");
//添加事件监听器
colorList.addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
int index = colorList.getSelectedIndex();
selectedColor = colors[index];
}
});
//创建菜单
menuBar = new JMenuBar();
colorMenu = new JMenu("Color");
redMenuItem = new JMenuItem("Red");
greenMenuItem = new JMenuItem("Green");
blueMenuItem = new JMenuItem("Blue");
//添加事件监听器
redMenuItem.addActionListener(this);
greenMenuItem.addActionListener(this);
blueMenuItem.addActionListener(this);
//将菜单项添加到菜单中
colorMenu.add(redMenuItem);
colorMenu.add(greenMenuItem);
colorMenu.add(blueMenuItem);
//将菜单添加到菜单栏中
menuBar.add(colorMenu);
//将组件添加到窗口中
setLayout(new GridLayout(5, 1));
add(redButton);
add(greenButton);
add(blueButton);
add(colorComboBox);
add(colorList);
//设置默认选中颜色
selectedColor = colors[0];
}
public void actionPerformed(ActionEvent e) {
//处理事件
if (e.getSource() == redButton) {
selectedColor = Color.RED;
} else if (e.getSource() == greenButton) {
selectedColor = Color.GREEN;
} else if (e.getSource() == blueButton) {
selectedColor = Color.BLUE;
} else if (e.getSource() == colorComboBox) {
int index = colorComboBox.getSelectedIndex();
selectedColor = colors[index];
} else if (e.getSource() == redMenuItem) {
selectedColor = Color.RED;
} else if (e.getSource() == greenMenuItem) {
selectedColor = Color.GREEN;
} else if (e.getSource() == blueMenuItem) {
selectedColor = Color.BLUE;
}
}
public void paint(Graphics g) {
//画圆并填充颜色
super.paint(g);
g.setColor(selectedColor);
g.fillOval(100, 100, 100, 100);
}
public static void main(String[] args) {
Main main = new Main();
main.setJMenuBar(main.menuBar);
main.setVisible(true);
}
}
```
3. 运行程序,你将看到一个窗口,其中包含单选钮、下拉列表、列表和菜单。通过选择不同的颜色,你可以改变圆的填充颜色。
值得一提的是,上面的代码虽然可以实现所需功能,但还有很多可以改进的地方,例如添加更多颜色选项,使其更灵活。
阅读全文
相关推荐













