java - JPanel removeAll 无法正常工作
问题描述
问题:代码中包含的 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();
}
}
}
面板类:
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);
}
}
解决方案
推荐阅读
- azure-sql-database - 具有分片管理的 Azure SQL 能否在不同区域拥有数据库?
- gatsby - 将 gatsbyJS 用于内容经常更新的站点
- google-apps-script - 复选框触发了 clearcontent 脚本,然后立即自动取消选中它
- styled-components - 带有样式组件的内联 CSS
- javascript - React-Redux 为什么当我设置中间件时对象在被调用时返回两次
- reactjs - 如何在 JSX 中获取数据并在页面上加载
- winapi - 如何打破 MFC 中的 ReadDirectoryChangesW 函数?
- arrays - 允许用户在选择文件到数组后完成并行/xargs命令(函数);在 printf 脚本中正确引用 nuls
- python - 无法将我的 JSON 保存到 python 字典中。将错误作为图片
- python - AttributeError:'bytes' 对象没有属性 'encode'