package com.huawei.response.improve.a.little;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import javax.swing.*;
public class FlowLayoutDemo {
static JToggleButton toggleBtn = null ;
public static int width = 100 ;
public static int height = 20;
public static int totalX ;
public static int totalY;
public static JScrollPane jsp= null ;
public static JPanel panel = null ;
public static int gap = 20 ;
public static int LevelNum = 3 ;
public static int dizeng = 0 ;
/**
* 创建开关效果的按钮
* @param x 按钮所在的横坐标
* @param y 按钮所在的纵坐标
* @param content 按钮显示的内容
* @param panel 按钮将添加的面板
*/
public static void createToggleButton(int x , int y ,String content, JPanel panel) {
toggleBtn = new JToggleButton(content);
toggleBtn.setBounds(x, y, width, height);
// 添加 toggleBtn 的状态被改变的监听
toggleBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
JToggleButton toggleBtn = (JToggleButton) arg0.getSource();
System.out.println(toggleBtn.getText() + " 是否选中: " + toggleBtn.isSelected());
}
});
panel.add(toggleBtn);
}
/**
* 为panel创建滚动视图
* @param panel 需要添加滚动效果的panel
* @return JScrollPane 在JFrame添加面板时,添加这个
*/
public static JScrollPane createJScrollPane(JPanel panel,int totalX , int totalY) {
jsp = new JScrollPane();
jsp.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.setPreferredSize(new Dimension(totalX, totalY));
jsp.setViewportView( panel );
return jsp ;
}
public static JPanel createJPanel() {
panel = new JPanel();
panel.setLayout(null);
panel.setSize(totalX, totalY);
return panel;
}
/**
* 创建JFrame
* @param fileName JFrame名称
* @param row JFrame的行数
* @param column JFrame的列数
* @return JFrame
*/
public static JFrame createJFrame(String fileName,int row , int column) {
JFrame jf = new JFrame(fileName);
GridLayout gy1 =new GridLayout(row,column);
jf.setLayout(gy1);
jf.setSize(2500, 2500);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
return jf;
}
/**
* 获取组件的横坐标
* @return
*/
public static int getX(ColumnParser column) {
int i =column.getLevel();
return gap*i+(i-1)*width ;
}
/**
* 获取组件的纵坐标
* @return
*/
public static int getY(ColumnParser column) {
int i =column.getLevel();
int result = 0 ;
if(i==LevelNum) {
if(dizeng==0) {
dizeng = gap ;
return dizeng;
}else {
result = (dizeng/gap)*height+gap*((dizeng/gap)+1);
column.setY(result);
dizeng+=gap;
}
}else {
result = reAdjustPanel(column);
}
return result ;
}
//获得非末尾级别的y坐标
public static int reAdjustPanel(ColumnParser column) {
List
//获取child中y坐标的最大值和最小值
int result = 0 ;
for(ColumnParser column_temp:list) {
result+=column_temp.getY();
}
return result/list.size();
}
/**
* 获取组件的横坐标
* @param FirstLevelName 一级列名
* @return
*/
public static int getTotalX() {
return LevelNum*width+(LevelNum+1)*gap;
}
/**
* 获取组件的纵坐标
* @param FirstLevelName 一级列名
* @return
*/
public static int getTotalY(ColumnParser column) {
int i = column.getTotalNum() ;
return i*height+(i+1)*gap;
}
public static void loadData(JPanel panel,ColumnParser column) {
createToggleButton(getX(column),getY(column),column.getColumnName(),panel);
if(column.childNum!=0) {
for(ColumnParser column_temp : column.getChilds()) {
loadData(panel,column_temp);
}
}
}
public static List
List
List
listStr.add("南京邮电大学####通院####专硕");
listStr.add("东南大学");
listStr.add("南京大学");
String str = "南京邮电大学####通院####专硕";
Map
ColumnParser columnParser = null ;
for (Map.Entry
List
for(String str1: list1) {
String[] strArray = str1.split("####");
for(int i = 1 ; i
columnParser = new ColumnParser(i,strArray[i-1],getChildNum(list1,i),list1.size());
list.add(columnParser);
}
for(int i =0 ; i
if( list.get(i).getChilds()!=null) {
if(!list.get(i).getChilds().contains(list.get(i+1))) {
list.get(i).getChilds().add(list.get(i+1));
}
}else {
List
list_temp.add(list.get(i+1));
}
}
}
}
}
List
return list ;
}
/**
* 通过String列表获取该级列有多少个子列
* @param list 属于同一key的list列表
* @param i
* @return
*/
public static int getChildNum(List
if(i!=LevelNum) {
Set
for(String str1: list) {
set.add(str1.split("####")[i]);
}
return set.size() ;
}else {
return 0 ;
}
}
public static void main(String[] args) {
List
JFrame jf = createJFrame("gao的文件拓扑结构",1,2);
List
for(ColumnParser column :columns ) {
panel = createJPanel();
loadData(panel,column);
panel.setBorder(BorderFactory.createTitledBorder(""));
jsp = createJScrollPane(panel,getTotalX(),getTotalY(column));
list.add(jsp);
}
//创建操作窗口的主面板,其他那些分面板都是在这个面板里。
JPanel panelAll = new JPanel();
GridLayout gy =new GridLayout(4,1);
JPanel panel = new JPanel();
panelAll.setLayout(gy);
panelAll.setPreferredSize(new Dimension(300, 300));
panelAll.setBorder(BorderFactory.createTitledBorder(""));
for(JScrollPane jsp : list) {
panelAll.add(jsp);
}
//数字显示面板
JPanel panel_num = new JPanel();
jf.add(panelAll);
jf.add(panel_num);
jf.setVisible(true);
}
}