gpt4 book ai didi

java - 检查 JToggleButton 是否被按下并禁用它

转载 作者:行者123 更新时间:2023-11-30 04:49:18 25 4
gpt4 key购买 nike

我有一个 JTogglebuttons 数组和一个 JButton,当按下 JButton 时,它会启动一个循环,迭代所有 JTogglebuttons 以查看它是否被按下,如果是,则应使其禁用/不可点击

这是有问题的代码片段

Public void actionPerformed(ActionEvent e){
Object pressed = e.getSource();
if (pressed == btnPurchase) {
for(int row = 0; row<8 ; row++){
for(int column = 0; column < 4; column++) {
if (seat[row][column].isPressed()) {
seat[row][column].setEnabled(false);
}
}
}
}

我收到此错误

cannot find symbol
symbol : method isPressed()
location: class javax.swing.JToggleButton
if (seat[row][column].isPressed()) {

如果我使用.isSelected

我明白

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at cinemaManager.actionPerformed(cinemaManager.java:174)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6375)

最佳答案

寻找ButtonModel,返回预期的方法,包括isSelected

import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JToggleButtonChangeListener {

public static void main(String args[]) {
JFrame frame = new JFrame("Selecting Toggle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JToggleButton toggleButton = new JToggleButton("Selecting Toggle / Deselacting Toggle");
toggleButton.addChangeListener(new ChangeListener() {

@Override
public void stateChanged(ChangeEvent e) {
ButtonModel buttonModel = toggleButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
}
});
frame.add(toggleButton);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
}

通知:背景的更改不适用于所有外观和感觉

关于java - 检查 JToggleButton 是否被按下并禁用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com