`
plkong
  • 浏览: 173373 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
java swing之JTable和JList jtable jlist java swing之JTable和JList
import java.awt.Dimension;
import java.util.Date;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableJFrame extends JFrame {
	public TableJFrame() {
		init();

		this.setTitle("表格的例子");
		this.setSize(new Dimension(400, 450));
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	private void init() {
//		// 1.二维数组初始化
//		String[] columnHeader = { "编号", "姓名", "年龄", "地址" };
//		String[][] data = new String[][] { { "user1", "小明", "26", "福州鼓楼" },
//				{ "user2", "小明2", "28", "福州鼓楼2" },
//				{ "user3", "小明3", "33", "福州鼓楼" },
//				{ "user4", "小明4", "26", "福州鼓楼" } };

//		
//		//2、实现了tableModel接口
//		DefaultTableModel defaultTableModel = new DefaultTableModel(data,columnHeader);
		//设置模型方式
//		table.setModel(defaultTableModel);
	
		
		
		//3、vector
		Vector<String> colHeader = new Vector<String>();
		colHeader.add("编号");
		colHeader.add("名字");
		colHeader.add("性别");
		colHeader.add("日期");
		
		Vector<Vector<String>> dataVec = new Vector<Vector<String>>();
		Vector<String> row1 = new Vector<String>();
		row1.add("0001");
		row1.add("旺财");
		row1.add("男");
		row1.add(new Date().toString());
		Vector<String> row2 = new Vector<String>();
		row2.add("0002");
		row2.add("小强");
		row2.add("男");
		row2.add(new Date().toString());
		Vector<String> row3 = new Vector<String>();
		row3.add("0003");
		row3.add("韦小宝");
		row3.add("女");
		row3.add(new Date().toString());
		Vector<String> row4 = new Vector<String>();
		row4.add("0004");
		row4.add("零零七");
		row4.add("男");
		row4.add(new Date().toString());
		
		dataVec.add(row1);
		dataVec.add(row2);
		dataVec.add(row3);
		dataVec.add(row4);
		
		JTable table = new JTable(dataVec,colHeader);
		//要显示表头必须要加入到滚动面板 滚动面板加入到窗体
//		JScrollPane scrollPane = new JScrollPane(table);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(table);
		this.add(scrollPane);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new TableJFrame();

	}

}

java Swing 进度条 Java Swing 进度条
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;




public class Test7 implements ActionListener
{
   
    private static final String DEFAULT_STATUS = "Please Waiting";

   
    private JDialog dialog;

   
    private JProgressBar progressBar;

   
    private JLabel lbStatus;

   
    private JButton btnCancel;

   
    private Window parent;

   
    private Thread thread;  //处理业务的线程

   
    private String statusInfo;

   
    private String resultInfo;

   
    private String cancelInfo;

   
    public static void show(Window parent, Thread thread)
    {
        new Test7(parent, thread, DEFAULT_STATUS, null, null);
    }

   
    public static void show(Window parent, Thread thread, String statusInfo)
    {
        new Test7(parent, thread, statusInfo, null, null);
    }

   
    public static void show(Window parent, Thread thread, String statusInfo,

                                  String resultInfo, String cancelInfo)
    {
        new Test7(parent, thread, statusInfo, resultInfo, cancelInfo);
    }

   
    private Test7(Window parent, Thread thread, String statusInfo,

                                        String resultInfo, String cancelInfo)
    {
        this.parent = parent;
        this.thread = thread;
        this.statusInfo = statusInfo;
        this.resultInfo = resultInfo;
        this.cancelInfo = cancelInfo;
        initUI();
        startThread();
        dialog.setVisible(true);
    }

    private void initUI()
    {
        if(parent instanceof Dialog)
        {
            dialog = new JDialog((Dialog)parent,  true);
        }
        else if(parent instanceof Frame)
        {
            dialog = new JDialog((Frame)parent,  true);
        }
        else
        {
            dialog = new JDialog((Frame)null,  true);
        }

        final JPanel mainPane = new JPanel(null);
        progressBar = new JProgressBar();
        lbStatus = new JLabel("" + statusInfo);
        btnCancel = new JButton("Cancel");
        progressBar.setIndeterminate(true);
        btnCancel.addActionListener(this);

        mainPane.add(progressBar);
        mainPane.add(lbStatus);
        //mainPane.add(btnCancel);

        dialog.getContentPane().add(mainPane);
        dialog.setUndecorated(true); // 除去title
        dialog.setResizable(true);
        dialog.setSize(390, 100);
        dialog.setLocationRelativeTo(parent); //设置此窗口相对于指定组件的位置
       
       
        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); // 不允许关闭
       
       

        mainPane.addComponentListener(new ComponentAdapter()
        {
            public void componentResized(ComponentEvent e)
            {
                layout(mainPane.getWidth(), mainPane.getHeight());
            }
        });
    }

    private void startThread()
    {
        new Thread()
        {
            public void run()
            {
                try
                {
                    thread.start(); // 处理耗时任务
                    // 等待事务处理线程结束
                    thread.join();
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    // 关闭进度提示框
                    dialog.dispose();

                    if(resultInfo != null && !resultInfo.trim().equals(""))
                    {
                        String title = "Info";
                        JOptionPane.showMessageDialog(parent, resultInfo, title,

                                                  JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        }.start();
    }

    private void layout(int width, int height)
    {
        progressBar.setBounds(20, 20, 350, 15);
        lbStatus.setBounds(20, 50, 350, 25);
        btnCancel.setBounds(width - 85, height - 31, 75, 21);
    }

    @SuppressWarnings("deprecation")
    public void actionPerformed(ActionEvent e)
    {
        resultInfo = cancelInfo;
        thread.stop();
    }
   
   
    public static void main(String[] args) throws Exception
    {
       

        Thread thread = new Thread()
        {
            public void run()
            {
                int index = 0;

                while(index < 5)
                {
                    try
                    {
                        sleep(1000);                       
                        System.out.println(++index);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        };

        Test7.show((Frame)null, thread, "Status", "Result", "Cancel");  
       
    }
}
Global site tag (gtag.js) - Google Analytics