为什么你不能将监听器添加到作为参数的对象?
panelThird.add(new JTextField( "Write here !" ).addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
dlm.addElement(e.getActionCommand());
}
}),
BorderLayout.SOUTH);
答案 0 :(得分:2)
addActionListener
的返回类型为void - 所以你不能写:
panelThird.add(new JTextField(...).addActionListener(...));
相反,你需要:
JTextField field = new JTextField(...);
field.addActionListener(...);
panelThird.add(field);
答案 1 :(得分:0)
当您将监听器添加到文本字段时,返回值为void,并且您无法向panelThird添加void。
JTextField tf = new JTextField( "Write here !" ); tf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { dlm.addElement(e.getActionCommand()); } }) panelThird.add(tf, BorderLayout.SOUTH);