package MainEntrancy;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JTree;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class Tab1Panel extends JPanel {
private static final long serialVersionUID = 1L;
int iTime = 5;
Timer timer = null;
AppFrame appFrame = null;
public Tab1Panel(final AppFrame appFrame) {
this.appFrame = appFrame;
this.setSize(450,200);
this.setPreferredSize(this.getSize());
this.setLayout(new GridLayout(0, 3));
JButton butTest = new JButton("开始");
butTest.setPreferredSize(new Dimension(50,30));
this.add(butTest);
JCheckBox checkBox = new JCheckBox();
checkBox.setText("刷新背景色");
this.add(checkBox);
final JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addItem("com1"); comboBox.addItem("com2");
comboBox.addItem("com3"); comboBox.addItem("com4");
comboBox.addItem("com5"); comboBox.addItem("com6");
this.add(comboBox);
comboBox.setSelectedIndex(2); // 默认选中项
final JEditorPane editorPane = new JEditorPane();
this.add(editorPane);
JButton butAdd = new JButton("添加");
this.add(butAdd);
final JLabel label = new JLabel("姓名", JLabel.CENTER);
this.add(label);
butAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str = editorPane.getText();
label.setText(str);
comboBox.addItem(str);
comboBox.setSelectedIndex(comboBox.getItemCount()-1);
}
});
JList<String> list = new JList<String>();
list.setBackground(Color.GRAY);
DefaultListModel<String> listModel = new DefaultListModel<String>();
list.setModel(listModel);
listModel.addElement("list1"); listModel.addElement("list2");
listModel.addElement("list3"); listModel.addElement("list4");
listModel.addElement("list5"); listModel.addElement("list6");
/*list.setLayoutOrientation(JList.HORIZONTAL_WRAP);*/
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(3); // 改变值,观察结果变化
this.add(list);
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
passwordField.setForeground(Color.RED);
passwordField.setBackground(Color.DARK_GRAY);
this.add(passwordField);
final JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
progressBar.setValue(10);
progressBar.setStringPainted(true);
this.add(progressBar);
timer = new Timer(100,new ActionListener() {
int value = progressBar.getValue();
@Override
public void actionPerformed(ActionEvent e) {
if(value < 100){
progressBar.setValue(++value);
}
else {
timer.stop();
}
}
});
butTest.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
timer.start();
}
});
ButtonGroup group = new ButtonGroup();
JRadioButton radioButton1 = new JRadioButton("Green");
this.add(radioButton1);
group.add(radioButton1);
JRadioButton radioButton2 = new JRadioButton("Red");
this.add(radioButton2);
group.add(radioButton2);
JRadioButton radioButton3 = new JRadioButton("Gray");
this.add(radioButton3);
group.add(radioButton3);
radioButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
appFrame.getContentPane().setBackground(Color.GREEN);
}
});
radioButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
appFrame.getContentPane().setBackground(Color.RED);
}
});
radioButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
appFrame.getContentPane().setBackground(Color.GRAY);
}
});
JButton butAddNode = new JButton("增加节点");
this.add(butAddNode);
JButton butIterNode = new JButton("遍历节点");
this.add(butIterNode);
JButton butDelNode = new JButton("删除节点");
this.add(butDelNode);
// 为树添加节点
butAddNode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JTree tree = appFrame.leftPanel.tab2Panel.tree;
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
Enumeration<?>enumeration = root.preorderEnumeration();
while (enumeration.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration.nextElement();
// 获取节点的字符串名称
String str = node.toString();
if(str.equals("河南")){
DefaultMutableTreeNode dmAnyang = new DefaultMutableTreeNode("安阳");
node.add(dmAnyang);
tree.updateUI();
// 由于最后的节点为叶节点,所以函数expandPath无效
TreePath path = new TreePath(dmAnyang.getPath());
System.out.println(path.toString());
TreePath parTh = new TreePath(node.getPath());
tree.expandPath(parTh);
break;
}
}
}
});
// 为树遍历节点
butIterNode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取的是树中可以看见的节点数目,也就是行数,隐藏的没有计算在内
int iRowCount = appFrame.leftPanel.tab2Panel.tree.getRowCount();
// 只能展开二级节点的行数,三级节点无法展开
for(int i = 0;i < iRowCount;i++){
appFrame.leftPanel.tab2Panel.tree.expandRow(i);
}
// 首先获取树的根节点,然后获取根节点的子节点数目
TreeModel model = appFrame.leftPanel.tab2Panel.tree.getModel();
int iChildCount = model.getChildCount(model.getRoot());
System.out.println(String.valueOf("根节点上的子节点个数是: " + iChildCount));
label.setText(String.valueOf(iRowCount));
// 需要使用递归函数遍历子节点
ExpandChildNode(appFrame.leftPanel.tab2Panel.tree, new TreePath((TreeNode)model.getRoot()),true);
}
});
// 为树删除指定节点
butDelNode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JTree tree = appFrame.leftPanel.tab2Panel.tree;
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
Enumeration<?>enumeration = root.preorderEnumeration();
while (enumeration.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration.nextElement();
// 获取节点的字符串名称
String str = node.toString();
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
if(str.equals("安阳")){
if(node.isRoot())
model.setRoot(null);
else
model.removeNodeFromParent(node);
break;
}
}
}
});
JButton butAddRow = new JButton("