我在列表中有动态数量的JButton,需要帮助将它们连接到actionListener}>
我首先基于一个名为alt的列表创建按钮:
for(int i =0;i<alt.size();i++) { JButton button = new JButton (alt.get(i)); button.addActionListener(this); buttonList.add(button); }
稍后添加类似这样的按钮
private void gui(List<JButton> bList) { f = new JFrame("window"); f.setLayout(new BorderLayout()); f.setVisible(true); JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.PAGE_AXIS)); for(int i =0;i<bList.size();i++) { buttonPane.add(bList.get(i)); } f.add(buttonPane, BorderLayout.SOUTH); }
我知道按钮永远不会超过4个。那么如何连接到ActionListener中的右键?是否将它们声明为不在gui范围内或单独命名?
public void actionPerformed(ActionEvent e) {
if(e.getSource() == ?) {
} else if (e.getSource() == ?) {
} else if (e.getSource() == ?){
}else if (e.getSource() == ?){
}
}
我在列表中有动态数量的JButton,需要帮助将它们连接到actionListener,我首先基于一个名为alt的列表创建按钮:for(int i = 0; i
e.getSource() == ?
中,?
应该是JButton实例,例如:e.getSource() == bList.get(0)