一个swing客户端程序(自己练手写的)

package com.tandaly.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.codehaus.xfire.client.Client;

import com.tandaly.util.ClientAuthenticationHandler;

/**
 * 主窗口
 * @author tandaly
 * @date Mar 14, 2013 4:38:16 PM
 */
public class WebServiceFrame extends javax.swing.JFrame implements ActionListener{

    /**
	 * 序列号
	 */
	private static final long serialVersionUID = 6291810775531232122L;

    private javax.swing.JButton btn_query;//执行按钮
    private javax.swing.JButton btn_reset;//清空按钮
    private javax.swing.JMenuItem item_about;//关于
    private javax.swing.JMenuItem item_exit;//退出
    private javax.swing.JMenuItem item_reset;//清空
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lab_icon;
    private javax.swing.JLabel lab_text;//提示信息面板
    private javax.swing.JTextArea txt_param;//参数文本域
    private javax.swing.JTextArea txt_info;//结果文本域
    private javax.swing.JTextField txt_url;//url文本域
    
    private javax.swing.JLabel lab_result;//结果面板
    private javax.swing.JLabel lab_method;//method面板
    private javax.swing.JLabel lab_url;//地址面板
    private javax.swing.JLabel lab_user;//用户面板
    private javax.swing.JLabel lab_pass;//密码面板
    private javax.swing.JTextField txt_user;//用户名文本框
    private javax.swing.JPasswordField txt_pass;//密码文本框
    private javax.swing.JTextField txt_method;//方法文本框
    
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private String initInfo = "\n *********************************************************\n" +
    						  "          WebService模拟客户端 @tandaly"+
    						  "\n *********************************************************\n";
    
	
    public WebServiceFrame() {
        initComponents();
        this.setTitle("webservice客户端测试器@tandaly");
        this.setIconImage(this.getToolkit().createImage(this.getClass().getClassLoader().getResource("com/tandaly/ui/images/logo.gif")));
        setPositionOnCenter();
    }
    
    public void setPositionOnCenter(){
    	Dimension dem = this.getToolkit().getScreenSize();
    	int appWidth = this.getWidth();
    	int appHeight = this.getHeight();
    	//System.out.println("(X,Y) = ("+dem.width+","+dem.height+")");
    	//System.out.println("(appWidth,appHeight) = ("+appWidth+","+appHeight+")");
    	int x = (dem.width-appWidth)/2;
    	int y = (dem.height-appHeight)/2;
    	//System.out.println("(x,y) = ("+x+","+y+")");
    	this.setLocation(x, y);
    }

 
    private void initComponents() {
		txt_user = new javax.swing.JTextField("admin");
		txt_pass = new javax.swing.JPasswordField("123456");
        txt_url = new javax.swing.JTextField("http://192.168.28.94/ysboa/services/WSService?wsdl");
        txt_method = new javax.swing.JTextField("invoke");
        btn_query = new javax.swing.JButton();
        btn_reset = new javax.swing.JButton();
        lab_icon = new javax.swing.JLabel();
        lab_text = new javax.swing.JLabel();
        lab_user = new javax.swing.JLabel();
        lab_pass = new javax.swing.JLabel();
        lab_url = new javax.swing.JLabel();
        lab_method = new javax.swing.JLabel();
        lab_result = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        txt_param = new javax.swing.JTextArea();
        txt_info = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        item_reset = new javax.swing.JMenuItem();
        item_exit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        item_about = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btn_query.setText("执行");

        btn_reset.setText("重置");

        txt_param.setColumns(20);
        txt_param.setRows(3);
        jScrollPane1.setViewportView(txt_param);
        
        txt_info.setColumns(20);
        txt_info.setRows(5);
        jScrollPane2.setViewportView(txt_info);

        jMenu1.setText("菜单");

        item_reset.setText("重置");
        jMenu1.add(item_reset);

        item_exit.setText("退出");
        jMenu1.add(item_exit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("帮助");

        item_about.setText("关于");
        jMenu2.add(item_about);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);
        
        //add icons for elements
        item_reset.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("com/tandaly/ui/images/reset.gif")));
        item_exit.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("com/tandaly/ui/images/exit.gif")));
        item_about.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("com/tandaly/ui/images/help.gif")));
        
        btn_query.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("com/tandaly/ui/images/query.gif")));
        btn_reset.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("com/tandaly/ui/images/reset.gif")));

        lab_icon.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("com/tandaly/ui/images/spinner.gif")));
        
        lab_icon.setVisible(false);
        
        lab_text.setText("【提示】请输入用户名、密码、请求地址以及参数信息并按执行!");
        lab_text.setVisible(true);
        
        lab_user.setText("帐号:");
        lab_user.setVisible(true);
        
        lab_pass.setText("密码:");
        lab_pass.setVisible(true);
        
        lab_url.setText("地址:");
        lab_url.setVisible(true);
        
        lab_method.setText("方法:");
        lab_method.setVisible(true);
        
        lab_result.setText("结果显示:");
        lab_result.setVisible(true);
        
        txt_param.setEditable(true);
        String paramJson = "{\"serviceCode\":1001, \"params\":\"{'userName':'admin'}\"}";
        txt_param.setText(paramJson);
        
        txt_info.setEditable(false);
        txt_info.setText(initInfo);
        
        //add listener for elements
        item_reset.addActionListener(this);
        item_exit.addActionListener(this);        
        item_about.addActionListener(this);
        
        btn_query.addActionListener(this);
        btn_reset.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
                    
                    .addGroup(layout.createSequentialGroup()
                    	.addComponent(lab_user)
                    	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_user, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lab_pass)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                     )   
                    .addGroup(layout.createSequentialGroup()
                    	.addComponent(lab_url)
                        .addComponent(txt_url, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lab_method)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_method, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_query)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_reset))
                        
                     .addGroup(layout.createSequentialGroup()
                    		 .addComponent(lab_result)
                    		 )
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lab_icon)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lab_text, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                	.addComponent(lab_user)
                    .addComponent(txt_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lab_pass)
                    .addComponent(txt_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    )
                    
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                 
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                	.addComponent(lab_url)
                    .addComponent(txt_url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lab_method)
                    .addComponent(txt_method, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_query)
                    .addComponent(btn_reset))
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                
                
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                
                .addGroup(
                		layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                		.addComponent(lab_result)
                		)   
               
                
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                
                .addGroup(
                		layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                		.addComponent(lab_icon)
                		.addComponent(lab_text)
                		)
                .addContainerGap(10, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btn_reset||e.getSource()==item_reset){
			reset();
		}
		if(e.getSource()==item_exit){
			exit();
		}
		if(e.getSource()==item_about){
			new AboutAuthorFrame().setVisible(true);
		}
		if(e.getSource()==btn_query){
			
			if(txt_user.getText().trim().length()==0){
				JOptionPane.showMessageDialog(this, "请输入帐号","提示",JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			
			if(new String(txt_pass.getPassword()).trim().length()==0){
				JOptionPane.showMessageDialog(this, "请输入密码","提示",JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			
			if(txt_url.getText().trim().length()==0){
				JOptionPane.showMessageDialog(this, "请输入地址","提示",JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			
			if(txt_method.getText().trim().length()==0){
				JOptionPane.showMessageDialog(this, "请输入方法","提示",JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			new QueryThread();
		}
	}
    
    private void exit(){
    	this.dispose();
    	System.exit(0);
    }
    
    private void reset(){
    	txt_method.setText("");
    	txt_user.setText("");
    	txt_pass.setText("");
    	txt_url.setText("");
    	txt_param.setText("");
    	txt_info.setText(initInfo);
    	lab_icon.setVisible(false);
    	lab_text.setText("【提示】请输入用户名、密码、请求地址以及参数信息并按执行!");
    }
  
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            java.awt.EventQueue.invokeLater(new Runnable() {
	            public void run() {
	            	 new WebServiceFrame().setVisible(true);
	            }
	        });  
        }
        catch (Exception exception) {
            exception.printStackTrace();
        }       
    }


    protected class QueryThread implements Runnable{

    	public QueryThread(){
    		new Thread(this).start();
    	}
    	
		public void run() {
			lab_icon.setVisible(true);
			lab_text.setText("正在执行,请稍后...");
			//lab_text.setVisible(true);
			
			String result = null;
	    	//String url = "http://192.168.28.94/ysboa/services/TestService?wsdl";
	    	long countTime = 0;
			try
			{
				String urls = txt_url.getText();
				Client c = new Client(new URL(urls));
				
//				User user = new User();
//				user.setUserName("");
				//String param = JSONObject.toJSONString(user);
				//txt_param.setText(param);
				//发送授权信息
				c.addOutHandler(new ClientAuthenticationHandler(txt_user.getText(), new String(txt_pass.getPassword())));
				
				//调用webservice返回信息
				long start = System.currentTimeMillis();
				
				
				Object[] results = c.invoke(txt_method.getText(), new Object[]{txt_param.getText().trim()});
				long end = System.currentTimeMillis();
				countTime = end - start;
				result = "" + results[0];
				//System.out.println(results[0]);
				
				//Document d = (Document)results[0];
				//System.out.println(d.getFirstChild().getFirstChild());
				//System.out.println(d.getLastChild());
				
				lab_text.setText("执行成功!");
			}catch(Exception e)
			{
				//e.printStackTrace();
				result = "" + e.toString();//e.getMessage();
				lab_text.setText("执行失败!");
			}
			
			txt_info.setText(result);
			lab_icon.setVisible(false);
			//lab_text.setVisible(true);
			
			lab_text.setText(lab_text.getText() + "\n[耗时:"+countTime+"毫秒]@" + df.format(new Date()));
		}
    }
    
    
  
}



package com.tandaly.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.ImageIcon;

/**
 * 
 * @author tandaly 
 * @date Mar 14, 2013 4:50:51 PM
 */
public class AboutAuthorFrame extends javax.swing.JFrame{

    /**
	 * 
	 */
	private static final long serialVersionUID = 2849635436032422602L;
	
    public AboutAuthorFrame() {
    	setTitle("关于作者(@tandaly[2013-03-14])");
    	setResizable(false);
        initComponents();
        this.addWindowListener(new WindowAdapter(){
        	public void windowClosing(WindowEvent e){
        		dispose();
        	}
        });
        setPositionOnCenter();
    }
    
    public void setPositionOnCenter(){
    	Dimension dem = this.getToolkit().getScreenSize();
    	int appWidth = this.getWidth();
    	int appHeight = this.getHeight();
    	//System.out.println("(X,Y) = ("+dem.width+","+dem.height+")");
    	//System.out.println("(appWidth,appHeight) = ("+appWidth+","+appHeight+")");
    	int x = (dem.width-appWidth)/2;
    	int y = (dem.height-appHeight)/2;
    	//System.out.println("(x,y) = ("+x+","+y+")");
    	this.setLocation(x, y);
    }


    private void initComponents() {

        l_image = new javax.swing.JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("com/tandaly/ui/images/tdcq.gif")));
        l_page = new javax.swing.JLabel();
        bt_ok = new javax.swing.JButton();        
        
        bt_ok.setVisible(false);

        l_page.addMouseListener(new MouseAdapter(){
        	
        	public void mouseEntered(MouseEvent e){
        		l_page.setForeground(Color.red); 
        		l_page.setFont(herfFont);
        		l_page.setCursor(new Cursor(Cursor.HAND_CURSOR));
        		
        	}
        	
        	public void mousePressed(MouseEvent e){
        		l_page.setForeground(Color.red); 
        		l_page.setFont(herfFont);
        		l_page.setCursor(herfCursor);
        		
        		Runtime rt = Runtime.getRuntime();
        		try {
					rt.exec("explorer http://tandaly.42t.com");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
        	}
        	
        	public void mouseExited(MouseEvent e){
        		l_page.setForeground(Color.black); 
        		l_page.setFont(normalFont);
        		l_page.setCursor(defaultCursor);
        	}
        });
        
        
//        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        l_page.setText("tandaly:http://tandaly.42t.com");

        bt_ok.setText("OK");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(l_image, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bt_ok, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(l_page, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(l_image, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(44, Short.MAX_VALUE)
                .addComponent(l_page)
                .addGap(42, 42, 42)
                .addComponent(bt_ok))
        );

        pack();
    }// </editor-fold>

    
 
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AboutAuthorFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton bt_ok;
    private javax.swing.JLabel l_image;
    private javax.swing.JLabel l_page;
    
    private Font herfFont = new Font("Courier New",0,12);
    private Font normalFont = new Font("宋体",0,12);
    
    private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
    private Cursor herfCursor = new Cursor(Cursor.HAND_CURSOR);
	
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值