gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:54:44 27 4
gpt4 key购买 nike

我的项目中有 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. 然后您将获得列表中的第一个按钮。

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

关于java - 如何通过单击另一个 Jbutton 来激活 Jbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43295258/

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