java基础-点击按钮更换按钮颜色,并设置默认被选中按钮

按钮组组件封装
本文介绍了一个使用按钮而非复选框来实现的选择组件。通过封装一个BaseButtnGroup类,可以自动生成一组排列整齐的按钮,并且实现了单选逻辑。每个按钮都有背景颜色变化的功能,以指示当前选择状态。

用按钮而不是复选框,实现封装一个组件。自动生成一排按钮

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 中,不同类型的按钮实现默认选中的方法有所不同。 对于普通按钮,通常没有“选中”的概念,不过可以通过设置其状态来模拟选中效果,例如设置背景颜色、边框等。以引用[1]中的按钮为例,可以在初始化时设置按钮的背景颜色或其他属性来表示选中状态: ```java import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class ButtonDefaultSelectionExample { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame f = new JFrame("Button Example"); f.setSize(400, 300); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton[] btn = new JButton[6]; for (int i = 0; i < 6; i++) { btn[i] = new JButton("Button " + i); f.add(btn[i]); } // 设置按钮 1 为“选中”状态,这里以设置背景颜色为例 btn[1].setBackground(Color.PINK); f.setLayout(null); for (int i = 0; i < 6; i++) { btn[i].setBounds(50, 50 + i * 30, 100, 25); } f.setVisible(true); }); } } ``` 对于单选按钮(`JRadioButton`),可以通过`setSelected(true)`方法来设置默认选中按钮。在一个`ButtonGroup`中,只能有一个单选按钮选中。示例代码如下: ```java import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JRadioButton; import javax.swing.SwingUtilities; public class RadioButtonDefaultSelectionExample { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame f = new JFrame("Radio Button Example"); f.setSize(300, 200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JRadioButton radioBtn1 = new JRadioButton("Option 1"); JRadioButton radioBtn2 = new JRadioButton("Option 2"); JRadioButton radioBtn3 = new JRadioButton("Option 3"); ButtonGroup group = new ButtonGroup(); group.add(radioBtn1); group.add(radioBtn2); group.add(radioBtn3); // 设置 radioBtn2 为默认选中 radioBtn2.setSelected(true); f.add(radioBtn1); f.add(radioBtn2); f.add(radioBtn3); f.setLayout(null); radioBtn1.setBounds(50, 50, 100, 25); radioBtn2.setBounds(50, 80, 100, 25); radioBtn3.setBounds(50, 110, 100, 25); f.setVisible(true); }); } } ``` 对于复选框(`JCheckBox`),同样可以使用`setSelected(true)`方法来设置默认选中: ```java import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class CheckBoxDefaultSelectionExample { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame f = new JFrame("CheckBox Example"); f.setSize(300, 200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox checkBox = new JCheckBox("Check me"); // 设置复选框为默认选中 checkBox.setSelected(true); f.add(checkBox); f.setLayout(null); checkBox.setBounds(50, 50, 100, 25); f.setVisible(true); }); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值