文件路径界面实现代码如下:
package panel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import util.CommonUtil;
import util.PathBean;
public class FilePathPanel extends JPanel {
private static final long serialVersionUID = 1L;
private int gridRows = 10;
private JPanel contentPanel = null;
private List<PathPanel> pathList = new ArrayList<PathPanel>();
public FilePathPanel() {
super();
setLayout(new BorderLayout());
//添加放置按钮的面板
JPanel operPanel = new JPanel();
operPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton addBtn = new JButton("添加", CommonUtil.getImg("image/add.png"));
addBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputValue = JOptionPane.showInputDialog("请输入映射分类,比如:海南测试环境。");
PathPanel pathPanel = new PathPanel((inputValue != null && !"".equals(inputValue.trim()))
? inputValue : "未知分类");
pathList.add(pathPanel);
if(pathList.size() > gridRows) {
contentPanel.setLayout(new GridLayout(pathList.size(), 1));
}
contentPanel.add(pathPanel);
contentPanel.validate();
contentPanel.repaint();
}
});
operPanel.add(addBtn);
JButton delBtn = new JButton("删除", CommonUtil.getImg("image/del.png"));
delBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(pathList.size() > 0) {
List<PathPanel> checkedList = new ArrayList<PathPanel>();
//遍历存在的分类
for(PathPanel pathObj : pathList) {
if(pathObj.getPathBox().isSelected()) {
checkedList.add(pathObj);
}
}
//若勾选了,则删除分类
if(checkedList.size() > 0) {
for(PathPanel checkedObj : checkedList) {
pathList.remove(checkedObj);
}
contentPanel.removeAll();
if(pathList.size() > gridRows) {
contentPanel.setLayout(new GridLayout(pathList.size(), 1));
} else {
contentPanel.setLayout(new GridLayout(gridRows, 1));
}
for(PathPanel noCheckObj : pathList) {
contentPanel.add(noCheckObj);
}
contentPanel.validate();
contentPanel.repaint();
} else {
CommonUtil.showMsgWin("请选择需要删除的映射分类!", JOptionPane.WARNING_MESSAGE);
}
} else {
CommonUtil.showMsgWin("暂无可选择映射分类!", JOptionPane.INFORMATION_MESSAGE);
}
}
});
operPanel.add(delBtn);
add(operPanel, BorderLayout.NORTH);
//路径配置
JPanel pathInfoPanel = new JPanel();
pathInfoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "路径映射"));
pathInfoPanel.setLayout(new GridLayout(1, 1));
contentPanel = new JPanel();
contentPanel.setLayout(new GridLayout(gridRows, 1));
pathInfoPanel.add(contentPanel);
add(new JScrollPane(pathInfoPanel), BorderLayout.CENTER);
}
public void addCom(List<PathBean> pathBeanList) {
if(pathBeanList.size() > gridRows) {
contentPanel.setLayout(new GridLayout(pathBeanList.size(), 1));
}
PathPanel pathPanel = null;
for(PathBean pathBean : pathBeanList) {
pathPanel = new PathPanel((pathBean.getType() != null && !"".equals(pathBean.getType().trim()))
? pathBean.getType()
: "未知分类");
pathPanel.setIndex(pathBean.getIndex());
pathPanel.getHostPath().setText(pathBean.getHostPath());
pathPanel.getLocalPath().setText(pathBean.getLocalPath());
pathList.add(pathPanel);
contentPanel.add(pathPanel);
}
contentPanel.validate();
contentPanel.repaint();
}
public List<PathPanel> getPathList() {
return pathList;
}
public void setPathList(List<PathPanel> pathList) {
this.pathList = pathList;
}
public JPanel getContentPanel() {
return contentPanel;
}
public void setContentPanel(JPanel contentPanel) {
this.contentPanel = contentPanel;
}
}
package panel;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import util.DBSequence;
public class PathPanel extends JPanel {
private static final long serialVersionUID = 1L;
private String index = null;
private JCheckBox pathBox = null;
private JTextField hostPath = null;
private JTextField localPath = null;
public PathPanel(String desc) {
super();
setLayout(new FlowLayout(FlowLayout.LEFT));
index = DBSequence.getInstance().getSequence();
add(new JLabel("服务器路径:"));
hostPath = new JTextField(25);
add(hostPath);
add(new JLabel("本地路径:"));
localPath = new JTextField(25);
add(localPath);
pathBox = new JCheckBox(desc);
add(pathBox);
}
public JCheckBox getPathBox() {
return pathBox;
}
public void setPathBox(JCheckBox pathBox) {
this.pathBox = pathBox;
}
public JTextField getHostPath() {
return hostPath;
}
public void setHostPath(JTextField hostPath) {
this.hostPath = hostPath;
}
public JTextField getLocalPath() {
return localPath;
}
public void setLocalPath(JTextField localPath) {
this.localPath = localPath;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
}