[英]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.