⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculatorclient.java

📁 精通Jboss——Ejb和Web Services开发精解的随书源代码
💻 JAVA
字号:
/**
 * CalculatorClient.java Created on 2003-12-20
 *
 */
package com.liuyang.jboss.sessionbean.calculator;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.liuyang.jboss.sessionbean.calculator.ejb.Calculator;
import com.liuyang.jboss.sessionbean.calculator.ejb.CalculatorHome;

/**
 * @author liuyang
 *
 */
public class CalculatorClient extends JFrame{

	public static void main(String[] args) throws Exception {
		CalculatorClient client = new CalculatorClient();
		client.getCalculator();
		client.start();
	}
	JTextField testfield = new JTextField();
	Calculator calculator;
	public Calculator getCalculator() throws Exception{
		Hashtable env = new Hashtable();
		env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL,"localhost");
		Context initContext = new InitialContext(env);		
		try {
			Object ref  = initContext.lookup("CalculatorHomeRemote");
			CalculatorHome home = (CalculatorHome)
			PortableRemoteObject.narrow(ref, CalculatorHome.class);
			calculator = home.create();
			return 	calculator;	
		}
		finally {
		  if (initContext != null) {
			initContext.close();
		  }
		}	
	}
	public void start(){

		Container content = this.getContentPane();
		content.setLayout(new BorderLayout());
		testfield.setText("0");
		testfield.setSize(60,50);
		content.add(BorderLayout.NORTH,testfield);

		JPanel sourth = new JPanel();	
		sourth.setLayout(new GridLayout(2,2));	
		JButton addbtn = new JButton("加");
		ActionListener add = new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					calculator.add(Integer.parseInt(testfield.getText()));
					int now = calculator.getValue();
					testfield.setText(now+"");
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		};
		addbtn.addActionListener(add);
		sourth.add(addbtn);
		
		JButton subtractbtn = new JButton("减");
		ActionListener subtract = new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					calculator.subtract(Integer.parseInt(testfield.getText()));
					int now = calculator.getValue();
					testfield.setText(now+"");
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				}				
			}
		};
		subtractbtn.addActionListener(subtract);
		sourth.add(subtractbtn);	

		JButton getbtn = new JButton("==");
		ActionListener get = new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					int now = calculator.getValue();
					testfield.setText(now+"");
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				}				
			}
		};
		getbtn.addActionListener(get);
		sourth.add(getbtn);

		JButton multiplybtn = new JButton("乘");
		ActionListener multiply = new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					calculator.multiplyBy(Integer.parseInt(testfield.getText()));
					int now = calculator.getValue();
					testfield.setText(now+"");
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				}				
			}
		};
		multiplybtn.addActionListener(multiply);
		sourth.add(multiplybtn);
	
		JButton dividebtn = new JButton("除");
		ActionListener divide = new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					calculator.divideBy(Integer.parseInt(testfield.getText()));
					int now = calculator.getValue();
					testfield.setText(now+"");
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				}				
			}
		};
		dividebtn.addActionListener(divide);
		sourth.add(dividebtn);

		JButton resetbtn = new JButton("重算");
		ActionListener reset = new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					calculator.reset();
					int now = calculator.getValue();
					testfield.setText(now+"");
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (RemoteException e) {
					e.printStackTrace();
				}				
			}
		};
		resetbtn.addActionListener(reset);
		sourth.add(resetbtn);
		
		content.add(BorderLayout.CENTER,sourth);		
		this.pack();
		this.setSize(200,150);
		this.setLocation(200,200);		
		this.addWindowListener(
			new WindowAdapter(){
				public void windowClosing(WindowEvent event) {
						System.exit(0);
				}
			}
		);			
		this.setTitle("计算器");	
		this.show();		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -