java基础-点击按钮更换按钮颜色,并设置默认被选中按钮
2016-06-04 17:02
573 查看
用按钮而不是复选框,实现封装一个组件。自动生成一排按钮
public class BaseButtnGroup {
private LovoLabel brandLab;
private List<LovoButton> list = new ArrayList<LovoButton>();
public BaseButtnGroup(String groupName,String[] btnName,Container con,
int x,int y){
brandLab = new LovoLabel(" - - " + groupName + " - - ", x, y, con);
for(int i = 0 ; i < btnName.length ; i++){
x += 100;
LovoButton baseBtn = new LovoButton(btnName[i], x, y, con);
baseBtn.setBackground(Color.CYAN);
if(baseBtn.getText().equals("不限")){
baseBtn.setBackground(Color.green);
}
list.add(baseBtn);
baseBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changeColor(baseBtn);
JButton bBtn =(JButton)e.getSource();
service(bBtn.getText());
}
});
}
}
public void changeColor(LovoButton baseBtn){
for(LovoButton temp : list){
if(temp == baseBtn){
temp.setBackground(Color.green);
}else{
temp.setBackground(Color.CYAN);
}
}
}
public void service(String info){
}
}
public class BaseButtnGroup {
private LovoLabel brandLab;
private List<LovoButton> list = new ArrayList<LovoButton>();
public BaseButtnGroup(String groupName,String[] btnName,Container con,
int x,int y){
brandLab = new LovoLabel(" - - " + groupName + " - - ", x, y, con);
for(int i = 0 ; i < btnName.length ; i++){
x += 100;
LovoButton baseBtn = new LovoButton(btnName[i], x, y, con);
baseBtn.setBackground(Color.CYAN);
if(baseBtn.getText().equals("不限")){
baseBtn.setBackground(Color.green);
}
list.add(baseBtn);
baseBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changeColor(baseBtn);
JButton bBtn =(JButton)e.getSource();
service(bBtn.getText());
}
});
}
}
public void changeColor(LovoButton baseBtn){
for(LovoButton temp : list){
if(temp == baseBtn){
temp.setBackground(Color.green);
}else{
temp.setBackground(Color.CYAN);
}
}
}
public void service(String info){
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树