问题:代码中没有包含lPanel.bListPanel.removeAll()来删除按钮。
我的目标:单击按钮时,我需要旧按钮消失,并需要新的按钮列表填充。
注意:此按钮侦听器与lPanel处于单独的类中。
谢谢!
RoPanel类:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class RoPanel extends JPanel {
public LPanel lPanel;
public RPanel rPanel;
private Ly l;
private ArrayList<JButton> bButtonList;
private JTextField newLoadField;
private JButton theButton;
public String bText;
private Border simpleBorder;
public RoPanel() {
this.setLayout(new BorderLayout());
simpleBorder = BorderFactory.createLineBorder(Color.GRAY);
l = new Ly();
lPanel = new LPanel();
rPanel = new RPanel();
this.setBorder(simpleBorder);
this.add(lPanel, BorderLayout.WEST);
this.add(rPanel, BorderLayout.EAST);
titleBooks = new ArrayList();
lPanel.getLoadButton();
lPanel.getLoadButton().addActionListener(new LoadButtonListener());
newLoadField = lPanel.getLoadField();
bookButtonList = new ArrayList();
bookText = new String();
}
private class LoadButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String loadStringfile = newLoadField.getText();
l.loadFromCSV(loadStringfile);
lPanel.bListPanel.removeAll();
// for loop that creates buttons
for (int i = 0; i < l.size(); i++) {
BButton b = new BButton();
bButtonList.add(theButton);
lPanel.bListPanel.add(theButton);
}
lPanel.bListPanel.revalidate();
lPanel.bListPanel.repaint();
}
}
}
Lanel类:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.*;
public class LPanel extends JPanel {
private JTextField loadField;
private JButton loadButton;
private Border simpleBorder;
private Border lowerBorder;
JPanel bListPanel;
JPanel importPanel;
public LPanel() {
this.setLayout(new BorderLayout());
simpleBorder = BorderFactory.createLineBorder(Color.GRAY);
lowerBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
bListPanel = new JPanel();
bListPanel.setLayout(new BoxLayout(bListPanel, BoxLayout.Y_AXIS));
importPanel = new JPanel();
this.add(importPanel, BorderLayout.SOUTH);
// create a jscrollpane that contains all buttons
JScrollPane scrollPane = new JScrollPane();
scrollPane.setLocation(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getViewport().add(bListPanel);
this.add(scrollPane);
// loadfield and load button for iPanel
loadField = new JTextField();
loadField.setPreferredSize(new Dimension(170, 25));
importPanel.add(loadField);
loadButton = new JButton();
loadButton.setText("Load");
importPanel.add(loadButton);
}
}