Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1681091
  • 博文数量: 210
  • 博客积分: 10013
  • 博客等级: 上将
  • 技术积分: 2322
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 15:56
文章分类

全部博文(210)

文章存档

2011年(34)

2010年(121)

2009年(37)

2008年(18)

我的朋友

分类: Java

2011-03-15 18:27:45

package com.utstar.factory;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;

public class TestTreeTable extends JFrame{
    private static final long serialVersionUID = 6819222900970457455L;
    private JTree tree;
    private JScrollPane treePane;
    private JScrollPane tablePane;
    public JTable table;
    private Object[] node = {"汽车","房子"};
    private String[] colum1 = {"车型","产地"};
    private String[][] data1 = {{"奥迪","德国"}};
    private String[] colum2 = {"户型","开发商"};
    private String[][] data2 = {{"三室一厅","绿城"}};
    private JPanel bottomPane;
    private JButton button,addButton,deleteButton;
    private DefaultTableModel tableModel1,tableModel2;
    private String name = node[0].toString();
    public TestTreeTable(){
        this.setSize(new Dimension(600,400));
        init();
        addEvent(this);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    private void init(){
        bottomPane = new JPanel();
        bottomPane.setBorder(BorderFactory.createTitledBorder("按钮"));
//        button = new JButton();

        addButton = new JButton("增加");
        deleteButton = new JButton("删除");
        FlowLayout buttonLayout = new FlowLayout();
        bottomPane.setLayout(buttonLayout);
        bottomPane.add(addButton);
        bottomPane.add(deleteButton);
        tableModel1 = new DefaultTableModel(data1,colum1);
        tableModel2 = new DefaultTableModel(data2,colum2);
        table = new JTable(tableModel2);
        tree = new JTree(node);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        treePane = new JScrollPane();
        treePane.setBorder(BorderFactory.createTitledBorder("选择"));
        treePane.getViewport().add(tree);
        
        tablePane = new JScrollPane();
        tablePane.getViewport().add(table);
//        this.getContentPane().add(button,BorderLayout.NORTH);

        this.getContentPane().add(treePane,BorderLayout.WEST);
        this.getContentPane().add(tablePane,BorderLayout.EAST);
        this.getContentPane().add(bottomPane,BorderLayout.SOUTH);
    }
    private void addEvent(final TestTreeTable value){
        tree.addTreeSelectionListener(new TreeSelectionListener(){
            public void valueChanged(TreeSelectionEvent e){
                JTree tree = (JTree)e.getSource();
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                String name = node.toString();
                value.name = name;
                if(name.equalsIgnoreCase("房子")){
                    value.table.setModel(value.tableModel2);
                    value.table.validate();
                }
                else {
                    value.table.setModel(value.tableModel1);
                    value.table.validate();
                }
            }
        });
        addButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                DefaultTableModel model = (DefaultTableModel)value.table.getModel();
System.out.println(value.name==null);
                if(value.name.equalsIgnoreCase("房子")){
                    LittleWindow factory = new LittleWindow("创建房子");
                    factory.setSuper(value);
                }
                else if(value.name.equalsIgnoreCase("汽车")){
                    LittleWindow car = new LittleWindow("创建汽车");
                    car.setSuper(value);
                }
                else{
                    model.addRow(new Object[]{"12","33"});
                }
                
            }
        });
        deleteButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                
            }
        });
    }
    public static void main(String args[]){
        TestTreeTable test = new TestTreeTable();
//        System.out.println(test.toString());

    }
}


package com.utstar.factory;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;

import com.utstar.factorymanager.*;

public class LittleWindow extends JFrame{
    private static final long serialVersionUID = 6819222900970457455L;
    private JLabel label1,label2,label3,label4;
    private JTextField field1,field2,field3,field4;
    private JComboBox box;
    private JScrollPane scrollPane;
    private static JTextField field ;
    private static JButton sureButton;
    private static JButton cancelButton;
    public static TestTreeTable superModel = new TestTreeTable();
    public LittleWindow(String name){
        this.setBounds(300,300,500,400);
        this.setTitle(name);
        this.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 10;
        gc.fill = GridBagConstraints.BOTH;
        gc.gridwidth = GridBagConstraints.REMAINDER;
        JPanel centerPanel = new JPanel();
        centerPanel.setBorder(BorderFactory.createTitledBorder("增加内容"));
        field = new JTextField();
//        field.setText("输入内容");

        label1 = new JLabel("工厂名称");
        label2 = new JLabel("工厂地址");
        label3 = new JLabel("工厂类型");
        label4 = new JLabel("工厂人数");
        field1 = new JTextField();
        field2 = new JTextField();
        field3 = new JTextField();
        field4 = new JTextField();
        box = new JComboBox();
        box.addItem("1");
        box.addItem("2");
        scrollPane = new JScrollPane();
        
//        centerPanel.setLayout(new BorderLayout());

//        

        GridBagLayout centerLayout = new GridBagLayout();
        centerPanel.setLayout(centerLayout);
        GridBagConstraints centerGc = new GridBagConstraints();
        centerGc.fill = GridBagConstraints.BOTH;
        centerGc.weightx = 1;
        centerGc.weighty = 1;
        centerLayout.setConstraints(label1,centerGc);
        centerGc.weightx = 3;
        centerGc.gridwidth = GridBagConstraints.REMAINDER;
        centerLayout.setConstraints(field1,centerGc);
        centerGc.weighty = 2;
        centerGc.weightx = 1;
        centerGc.gridwidth = GridBagConstraints.BOTH;
        centerLayout.setConstraints(label2,centerGc);
        centerGc.weightx = 3;
        centerGc.gridwidth = GridBagConstraints.REMAINDER;
        scrollPane.getViewport().add(field2);
        scrollPane.setAutoscrolls(true);
        centerLayout.setConstraints(scrollPane,centerGc);
        centerGc.gridwidth = GridBagConstraints.BOTH;
        centerGc.weightx = 1;
        centerGc.weighty = 1;
        centerLayout.setConstraints(label3,centerGc);
        centerGc.gridwidth = GridBagConstraints.REMAINDER;
        centerGc.weightx = 3;
        centerLayout.setConstraints(box,centerGc);
        centerGc.gridwidth = GridBagConstraints.BOTH;
//        centerGc.gridheight = GridBagConstraints.REMAINDER;

        centerGc.weightx = 1;
        centerGc.weighty = 1;
        centerLayout.setConstraints(label4,centerGc);
        centerGc.weightx = 3;
        centerGc.gridwidth = GridBagConstraints.REMAINDER;
        centerLayout.setConstraints(field4,centerGc);
        centerPanel.add(label1);
        centerPanel.add(field1);
        centerPanel.add(label2);
        centerPanel.add(scrollPane);
        centerPanel.add(label3);
        centerPanel.add(box);
        centerPanel.add(label4);
        centerPanel.add(field4);
//        centerPanel.add(field,BorderLayout.CENTER);

        this.add(centerPanel,gc);
        gc.weighty = 1;
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        sureButton = new JButton("确定");
        sureButton.setFont(new Font("宋体",3,20));
        cancelButton = new JButton("取消");
        cancelButton.setFont(new Font("宋体",3,20));
        bottomPanel.add(sureButton,BorderLayout.CENTER);
        bottomPanel.add(cancelButton,BorderLayout.CENTER);
        addEvent(this);
        this.add(bottomPanel,gc);
        pack();
        this.setVisible(true);
    }
    public static void addEvent(final LittleWindow w){
        sureButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                DefaultTableModel model = (DefaultTableModel)w.superModel.table.getModel();
                model.addRow(new Object[]{w.field1.getText(),w.field1.getText()});
                w.superModel.table.validate();
                w.dispose();
//                w.superModel.table;

//                w.superModel

//                superModel.addData(field.getText(),field.getText());

//                superModel.table.revalidate();

//                w.dispose();

//                w.setVisible(false);

            }
        });
        cancelButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                w.dispose();
            }
        });
    }
    public void setSuper(TestTreeTable model){
        superModel = model;
    }
    public String getContent(){
        return this.field.getText();
    }
    public static void main(String args[]){
        String name = "创建工厂";
        LittleWindow win = new LittleWindow(name);
    }
}


阅读(4740) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-27 13:22:10

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com