[英]How to activate a Jbutton by clicking another Jbutton
There are 7 buttons in my project. 我的项目中有7个按钮。 6 of them are categories and RandomSoru button is the one which randomly chooses one of the categories. 其中6个是类别,RandomSoru按钮是随机选择一个类别的按钮。 I want to access the chosen category. 我要访问选择的类别。 "r" is the random generator. “ 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");
....
Here is the Spor button 这是Spor按钮
Spor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
......
My expectation was like 我的期望就像
else if(current.hmap==tl.SporHmap)
JOptionPane.showMessageDialog(null,"Your Category is Spor");
Spor();
else if(current.hmap.....
One way is to add the 6 buttons to an ArrayList. 一种方法是将6个按钮添加到ArrayList。
Then in the ActionListener
of the random button you could do something like: 然后,在随机按钮的ActionListener
中,您可以执行以下操作:
Use the Collections.shuffle(...)
method to randomize the buttons in the List
. 使用Collections.shuffle(...)
方法将List
的按钮随机化。
Then you get the first button from the List
. 然后,您从List
获得第一个按钮。
Finally you invoke the doClick()
method on the button. 最后,您在按钮上调用doClick()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.