gpt4 book ai didi

java - 单击按钮时如何禁用面板的组件

转载 作者:行者123 更新时间:2023-12-02 03:22:09 26 4
gpt4 key购买 nike

我的代码有一些问题!...我能够使用下面的代码在 JPanel 中以循环形式创建 12 个 JButton 的数组!...我在每个 JButton 上设置一个 actionListener,并且我想在单击其中一个 Jbutton 后禁用其他按钮......然后启用它......为了更多理解,这是我的代码

int n = 10;


public Beginner() {

int radius = 200;
Point center = new Point (250, 250);

double angle = Math.toRadians(360 / n);

List <Point> points = new ArrayList<Point> ();

points.add(center);

for (int i = 0; i < n; i++) {
double theta = i * angle;

int dx = (int) (radius * Math.sin(theta));

int dy = (int) (radius * Math.cos(theta));

Point p = new Point (center.x + dx , center.y + dy);

points.add(p);
}

draw (points);

}
public void draw (List<Point> points) {

JPanel panels = new JPanel();

SpringLayout spring = new SpringLayout();

int count = 1;
for (Point point: points) {

quest = new JButton("Question " + count);
quest.setForeground(Color.BLACK);
Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
quest.setFont(fonte);

add (quest);
count++;

spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels );

spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels );

setLayout(spring);

panels.setOpaque(false);
panels.setVisible(true);
panels.setLocation(5,5);

add(panels);
quest.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent q) {
if (point.equals(points.get(0))) {

//Some action....
//It is at this point that every other Jbutton in the panel is to be disabled until the action ends..... It is here that I need help!!!

最佳答案

尝试这个解决方案:

            quest.addActionListener(new java.awt.event.ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
for(Component c : yourPanel.getComponents()){
if(c instanceof JButton && !c.equals(b)){
c.setEnabled(false);
}
}
}
});

关于java - 单击按钮时如何禁用面板的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39467370/

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