如何通过单击另一个 Jbutton 来激活 Jbutton

How to activate a Jbutton by clicking another Jbutton

我的项目中有 7 个按钮。其中 6 个是类别,RandomSoru 按钮是随机选择其中一个类别的按钮。我想访问所选类别。 "r" 是随机生成器。

RandomSoru.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        TriviaLinked tl = new TriviaLinked();


        tl.insertAtBack(tl.CogHmap);
        tl.insertAtBack(tl.TarihHmap);
        tl.insertAtBack(tl.SporHmap);
        tl.insertAtBack(tl.BilimHmap);
        tl.insertAtBack(tl.FilmHmap);
        tl.insertAtBack(tl.SanatHmap);

        TriviaNode current = tl.root;

        int n = r.nextInt(tl.sizeCounter());

        for (int i = 0; i < n; i++) {
            current = current.next;
        }
        if(current.hmap==tl.CogHmap)
            JOptionPane.showMessageDialog(null,"Your Category is Cografya");

        else if(current.hmap==tl.SporHmap)
            JOptionPane.showMessageDialog(null,"Your Category is Spor");
            ....

这是 Spor 按钮

Spor.addActionListener(new ActionListener() {


    public void actionPerformed(ActionEvent e) {
    ......

我的期望是

 else if(current.hmap==tl.SporHmap)
        JOptionPane.showMessageDialog(null,"Your Category is Spor");
        Spor();
else if(current.hmap.....

一种方法是将 6 个按钮添加到 ArrayList。

然后在随机按钮的 ActionListener 中,您可以执行以下操作:

  1. 使用Collections.shuffle(...)方法随机化List中的按钮。

  2. 然后你从List得到第一个按钮。

  3. 最后调用按钮上的 doClick() 方法。