ArrayList.remove(int index) 正在使用 ActionListener 类的匿名实例:-
删除模块.java:-
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class MyFrame extends JFrame{
private ArrayList<String> list = new ArrayList<String>() ;
private JButton btn = new JButton("Enter index to delete : ") ;
private JTextField fld = new JTextField() ;
MyFrame(){
populateList() ;
setLayout(new GridLayout(1, 2)) ;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;
setSize(400, 60) ;
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
list.remove( Integer.parseInt( fld.getText() ) ) ;
JOptionPane.showConfirmDialog(null, list, "Total Elements : " + list.size(), JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE) ;
}
});
add(btn) ;
add(fld) ;
setTitle("Total Elements : " + list.size()) ;
setVisible(true) ;
}
private void populateList(){
for(int i = 1 ; i <= 5 ; ++i){
list.add("Key " + i) ;
}
}
}
public class DeleteModule {
public static void main(String[] args) {
new MyFrame() ;
}
}
但是当我将它与原始模块集成时(下面是修改后的精简源代码,带有 DeleteFromPoolListener.class 的非匿名实例),它返回 false 以进行删除。我真的不知道为什么它不起作用。
演示.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class MyFrame extends JFrame{
private ArrayList<Pair> list = new ArrayList<Pair>() ;
private JButton deleteBtn = new JButton("Delete Pair at index : ") ;
private JTextField deleteIndexText = new JTextField() ;
MyFrame(){
populateList() ;
setTitle("List size : " + list.size()) ;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;
JPanel lowerPanel = new JPanel() ;
lowerPanel.setLayout(new GridLayout(2, 1)) ;
deleteBtn.addActionListener(new DeleteFromPoolListener()) ; lowerPanel.add(deleteBtn) ;
lowerPanel.add(deleteIndexText) ;
add(lowerPanel) ;
pack() ;
setVisible(true) ;
}
private void populateList(){
for(int i = 1 ; i <= 5 ; ++i){
list.add( new Pair( "Key " + i, "Value " + i ) ) ;
}
}
class DeleteFromPoolListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
Integer i = null ;
try{
i = Integer.parseInt(deleteIndexText.getText().trim()) ;
boolean b = list.remove(i) ;
JOptionPane.showConfirmDialog(null, list + "\n\n" + "Deleted : " + b + " from index " + i, "List size : " + list.size(), JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE) ;
setTitle("Total Pairs : " + list.size()) ;
deleteIndexText.setText("") ;
}
catch(NumberFormatException nfe){
JOptionPane.showConfirmDialog(null, "Enter numeric value in range.", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE) ;
deleteIndexText.setText("") ;
}
}
}
}
public class Demo {
public static void main(String[] args) {
new MyFrame() ;
}
}
class Pair{
private String key ;
private String value ;
Pair(String k, String v){
key = k ;
value = v ;
}
public String toString() {
return "[" + value + "]" ;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
我真的很困惑为什么它不起作用...... :(