点击任何jbuttons
时,是否可以将公共代码应用于框架中的多个jbutton
。
就像我点击一样,说jbutton80
,然后:
从jbutton1
到jbutton75
{
// common code that applies to all jbutton in loop
}
我在java中制作测验应用程序,jframe
中有大约70-80个按钮。每个按钮对应一个问题。问题分为几个部分。
所以我想:
//如果用户选择(或点击jbutton
)部分setvisible(true)
从jbutton1
到jbutton20
,并设置从jbutton21
到{jbutton 80
{1}}。
很抱歉,如果之前已经提出此问题。我试图搜索任何相关的帖子,但找不到。
答案 0 :(得分:1)
您是否尝试制作动作侦听器
ActionListener l = new ActionListener() { /* code here */ };
然后将相同的一个添加到所有JButton中?
for (JButton b : buttons) {
b.addActionListener(l);
}