[英]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
中,您可以执行以下操作:
使用Collections.shuffle(...)
方法将List
的按钮随机化。
然后,您从List
获得第一个按钮。
最后,您在按钮上调用doClick()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.