首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >多个jButtons上的一个操作

多个jButtons上的一个操作
EN

Stack Overflow用户
提问于 2018-06-11 07:10:25
回答 2查看 596关注 0票数 0

我想在50 jButtons上使用一个函数,假设它是数字麻将游戏,我想用一种方法把随机数字放到所有50个jbutton中。

代码语言:javascript
运行
复制
int r = randInt(1,50);
        jButton1.setText(Integer.toString(r));

这只是一个,但我不知道如何让它与其他50个相同。我所能想到的就是复制、粘贴50次,然后改变按钮的编号。

EN

回答 2

Stack Overflow用户

发布于 2018-06-11 07:44:10

您可以将这些按钮存储在一个数组中,然后可以使用一个简单的for循环在每个元素上执行该方法。

例如:

代码语言:javascript
运行
复制
//JButtonArray is an array of JButton objects
for(int i=0; i < JButtonArray.length; i++) {
    r = randInt(1,50);
    JButtonArray[i].setText(Integer.toString(r));
}
票数 1
EN

Stack Overflow用户

发布于 2018-06-11 18:36:17

另一种方法是使用Darryl Burke's SwingUtils class并在面板容器中找到所有JButtons,然后随心所欲地处理它们。这有更高的复杂性(但它不需要在某个地方“保存”所有的jbutton),这里不是必需的,但将来有一天您可能会发现它很有用。例如,要迭代JOptionPane的按钮。

使用此方法的SSCCE:

代码语言:javascript
运行
复制
package test;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class AllJButtons extends JFrame {
    public AllJButtons() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        getContentPane().setLayout(new GridLayout(10, 5));
        //Let's say somehow, we add the buttons
        for (int i = 0; i < 50; i++) {
            JButton btn = new JButton("Button ID=" + i);
            getContentPane().add(btn);
        }
        //Now iterate all buttons and add them red border.
        for (JButton button : getDescendantsOfType(JButton.class, getContentPane(), false)) {
            button.setBorder(BorderFactory.createLineBorder(Color.RED));
        }
        setLocationRelativeTo(null);
    }

    /**
    * Convenience method for searching below <code>container</code> in the
    * component hierarchy and return nested components that are instances of
    * class <code>clazz</code> it finds. Returns an empty list if no such
    * components exist in the container.
    * <P>
    * Invoking this method with a class parameter of JComponent.class
    * will return all nested components.
    * 
    * @param clazz the class of components whose instances are to be found.
    * @param container the container at which to begin the search
    * @param nested true to list components nested within another listed
    * component, false otherwise
    * @return the List of components
    */
    public static <T extends JComponent> List<T> getDescendantsOfType(Class<T> clazz, Container container, boolean nested) {
        List<T> tList = new ArrayList<T>();
        for (Component component : container.getComponents()) {
            if (clazz.isAssignableFrom(component.getClass())) {
                tList.add(clazz.cast(component));
            }
            if (nested || !clazz.isAssignableFrom(component.getClass())) {
                tList.addAll(AllJButtons.<T>getDescendantsOfType(clazz, (Container) component, nested));
            }
        }
        return tList;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new AllJButtons().setVisible(true);
        });
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50788818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档