我有一個JCheckBox
,並且實現了動作監聽器。Java Swing在一個JCheckBox上的兩個Action Listeners
這裏,當它被選中時,會出現一些東西,當沒有選中時,外觀就會消失。
要實現這一點,我應該有兩個動作監聽器?我如何實現這一點?
我有一個JCheckBox
,並且實現了動作監聽器。Java Swing在一個JCheckBox上的兩個Action Listeners
這裏,當它被選中時,會出現一些東西,當沒有選中時,外觀就會消失。
要實現這一點,我應該有兩個動作監聽器?我如何實現這一點?
你有一個單一的ActionListener
,你通過使用它的isSelected
屬性檢查JCheckBox
的選擇狀態。 A ActionListener
無法區分它自己的狀態,它只是響應用戶輸入
有關更多詳細信息,請參閱How to Use Buttons, Check Boxes, and Radio Buttons和How to Write an Action Listeners。
您可以使用ActionListener
,這和當行動被觸發,你可以檢查它的選擇或不
final JCheckBox checkBox = new JCheckBox("My checkbox");
checkBox.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// check if checkBox is selected or not
if(checkBox.isSelected()){
// here you can fire an event in which your checkbox is mark as selected
// and you can display the value you want to display
} else{
// checkBox is not selected so you can fire an event in which your checkbox is not selected
}
}
}
謝謝你的例子:)! –
@YongGonEvanPark歡迎:) – Luffy
的這個任何的例子嗎?我真的找不到一個關於stackoverflowflow –
的例子是什麼? – MadProgrammer