用Java编写小程序(包含组合框下拉和文本框)变换背景颜色
发布网友
发布时间:2022-05-12 02:05
我来回答
共1个回答
热心网友
时间:2023-11-03 02:21
代码如下:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class App extends JFrame {
private JComboBox<String> cbxColors;
private JTextField txtName;
private Map<String, Color> colorMap;
public App() {
this.setSize(300, 100);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
colorMap = new HashMap<>();
colorMap.put("红", Color.RED);
colorMap.put("绿", Color.GREEN);
colorMap.put("蓝", Color.BLUE);
colorMap.put("黄", Color.YELLOW);
cbxColors = new JComboBox(colorMap.keySet().toArray());
cbxColors.setPreferredSize(new Dimension(50, 23));
cbxColors.addItemListener(e -> {
Color color = colorMap.get(e.getItem());
txtName.setBackground(color);
});
this.add(cbxColors);
txtName = new JTextField();
txtName.setPreferredSize(new Dimension(100, 23));
txtName.setBackground(Color.RED);
this.add(txtName);
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果: