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

📄 window.java

📁 基于JMS的消息,用包括界面代码,主要用初学都学习
💻 JAVA
字号:
package com.sjx.jms;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class window extends Frame{

	QueueSend qs = null;
	QueueReceive qr = null;
	TopicPublish tp = null;
	TopicSubscribeAsynchronous tsa = null;
	//TopicSubscribeSynchronous  tss = null;
	protected TextArea  displayta = null;
	protected TextArea  writeta = null;
	protected TextArea topoicTextArea = null;
	protected JButton  sendButton = null; 
	protected Panel panel = null; 
	protected Panel panel2 = null;
	protected Panel panel3 = null;
	protected TextField tf = null;
	protected JLabel label = null;
	protected JLabel label1 = null;
	protected JComboBox  combobox  = null;
	
	private int model = 0;
	
	String [] string = {"PTP模式","PUB/SUB模式"};
	
	public window(){
		qs = new QueueSend();
		qr = new QueueReceive();
		tp = new TopicPublish();
		tsa = new TopicSubscribeAsynchronous(this);
		//tss = new TopicSubscribeSynchronous(this);
		
		this.setTitle("Hehe");
		this.setLocation(300, 150);
		this.setSize(400, 500);
		this.setVisible(true);
		
		displayta = new TextArea("收到的消息:\n");
		displayta.setEditable(false);
		topoicTextArea = new TextArea("定阅的主题:\n");
		topoicTextArea.setEditable(false);
		writeta = new TextArea("");
		sendButton = new JButton("发送");
		panel = new Panel();
		panel2 = new Panel();
		panel3 = new Panel();
		tf = new TextField(10);
	    label = new JLabel("呢称");	
	    label1 = new JLabel("请选择模式:");	
	    combobox = new JComboBox(string);
	    
	    new Thread(new ReceiveThread(this)).start();
	    
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				qs.close();
				qr.close();
				tp.close();
				//tss.close();
				tsa.close();
				System.exit(0);
			}			
		});
	}
	
	public void lauchFrame(){
		this.setLayout(new BorderLayout());
		
		panel.setLayout(new BorderLayout());
		panel.add(writeta, BorderLayout.NORTH);		
		panel2.setLayout(new FlowLayout());
		panel2.add(label1);
		panel2.add(combobox);
		panel2.add(label);
		panel2.add(this.tf);
		panel2.add(sendButton);
		panel.add(panel2, BorderLayout.SOUTH );
		
		panel3.setLayout(new GridLayout(1,2));
		panel3.add(displayta);
		panel3.add(topoicTextArea);
		
		this.add(panel, BorderLayout.SOUTH );
		this.add(panel3,BorderLayout.CENTER);
		
		sendButton.addActionListener(new sendButtonActionListener(this));
		combobox.addActionListener(new comboboxActionListener());
		
		this.validate();
		//tss.Receive();
		//tsa.ReceiveMsg();
		
	}
	
	private class sendButtonActionListener implements ActionListener{
		window w = null;
		
		public sendButtonActionListener(window w){
			this.w = w;
		}
		
		public void actionPerformed(ActionEvent e) {
			String name = tf.getText();
			String text = writeta.getText();
			String string = "";
			
			if(text.equals("")){
				new JOptionPane().showMessageDialog(w,"输入不能为空!", "错误!!", JOptionPane.PLAIN_MESSAGE);
			} else{
				if(name != null && !name.trim().equals("")){
					string = name +":\n"+"  "+ text+ "\n";
				} else {
					string = name + text + "\n";
				}
				
				
				if(model == 0){
					qs.send(string);
				} else if (model == 1 ){
					tp.publicMsg(string);
				}				
			}
			writeta.setText("");		
		}
	}
	
	private class ReceiveThread implements Runnable{
		window w = null;
		public ReceiveThread(window w){
			this.w = w;
		}
		public void run() {
			while(true){
				String string = qr.receive();
				if(string != null)
					w.displayta.append(string);
				try {
					Thread.sleep(250);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}			
		}		
	}	
	
	private class comboboxActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			String s = combobox.getSelectedItem().toString();
			if(s.equals("PTP模式")){
				model = 0;
			}else if(s.equals("PUB/SUB模式")){
				model = 1;
			}
		}
	}
		
	public static void main(String args[]){
		new window().lauchFrame();
	}
}

⌨️ 快捷键说明

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