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

📄 framebean.java

📁 java的入门学习,javabean的实现.
💻 JAVA
字号:

import java.beans.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JTabbedPane;



/**
 * FrameBean is a user interface to display chat messages. User can add and
 * remove a chat user, display a message in a ChatDisplay or send a message to
 * the ChatDisplay. If there is an error, nothing happens.
 */
public class FrameBean extends WindowAdapter implements ActionListener {
	//允许在FrameBean的实例上触发属性改变事件
	public PropertyChangeSupport changes = new PropertyChangeSupport(this);

	//默认服务器为初始活动用户
	static String[] legalUser = { "Public" };

	//选择目标用户的下拉列表框
	public JComboBox desPerson = new JComboBox(legalUser);

	//增、删用户的输入框
	JTextField addRemMemberField = new JTextField();

	//发送消息输入框
	JTextField messageField = new JTextField(23);

	//显示公有消息和私人消息的选项卡
	public JTabbedPane jtp = new JTabbedPane(JTabbedPane.BOTTOM);

	//公有消息显示区
	public JTextArea publicArea = new JTextArea(10, 30);

	//私有消息显示区
	public JTextArea privateArea = new JTextArea(10, 30);

	//新建一Frame的实例,默认的标题为chat Display !
	JFrame frameshow = new JFrame("chat display!");

	//新建一顶部面板,并内建活页夹
	JPanel TopPanel = new JPanel();

	//新建带滚动条的Public面板
	JScrollPane jsp1 = new JScrollPane(publicArea);

	//新建带滚动条的Private面板
	JScrollPane jsp2 = new JScrollPane(privateArea);

	//新建一中部面板
	JPanel MidPanel = new JPanel();

	//新建一底部面板
	JPanel BotPanel = new JPanel();
	
	//新建一菜单条
	JMenuBar jmb = new JMenuBar();

	//新建一菜单
	JMenu jmi1 = new JMenu("File");

	//新建一菜单
	JMenu jmi2 = new JMenu("Action");


	//新建一文本标签
	JLabel jl1 = new JLabel("Send To:  ");

	//新建一文本标签
	JLabel jl2 = new JLabel("Message");

	//新建一按钮
	JButton jb = new JButton("Send");


    

	/**
	 * 用来生产窗体的函数
	 */
	public void test() {
		frameshow.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		Container contentPane = frameshow.getContentPane();
		//设置容器布局
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		frameshow.setSize(400, 360);
		TopPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		//设置快捷键
		jmi1.setMnemonic('F');
		jmi2.setMnemonic('A');
		jmb.add(jmi1);
		jmb.add(jmi2);
		TopPanel.add(jmb);
		contentPane.add(TopPanel);
		//设置不可编辑
		publicArea.setEditable(false);
		privateArea.setEditable(false);
		jsp1.getViewport().add(publicArea);
		jsp2.getViewport().add(privateArea);
		//设置切换的面板
		jtp.addTab("Public", jsp1);
		jtp.addTab("Private", jsp2);
		contentPane.add(jtp);
		MidPanel.setLayout(new BoxLayout(MidPanel, BoxLayout.X_AXIS));
		MidPanel.add(jl1);
		MidPanel.add(desPerson);
		contentPane.add(MidPanel);
		BotPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		//增加事件监听器
		jb.addActionListener(this);
		messageField.addActionListener(this);
		BotPanel.add(jl2);
		BotPanel.add(messageField);
		BotPanel.add(jb);
		contentPane.add(BotPanel);
		frameshow.show();
	}
	
	
	 String name;



	/**
	 * 
	 * 
	 * @param s
	 */
	public FrameBean(String s) {
		frameshow.setTitle(s);
		name = s;
		test();
	}

	/**
	 * 增加聊天对象
	 * 
	 * @param u
	 */
	public void addUser(String u) {
		desPerson.addItem(u);
	}


	/**
	 * 点击事件
	 */
	public void actionPerformed(ActionEvent e) {
		String temp;
		temp = ( messageField.getText() + '\n');
		setObject("Sendto" + desPerson.getSelectedItem().toString(), temp);
		messageField.setText("");
	}

	/**
	 * 在公聊区域新增文字
	 * 
	 * @param s
	 */
	public void publicAppend(String s) {
		publicArea.append(s);
	}

	/**
	 * 在私聊区域新增文字
	 * 
	 * @param s
	 */
	public void privateAppend(String s) {
		privateArea.append(s);
	}

	/**
	 *addPropertyChangeListener
	 * 
	 * @param l
	 */
	public void addPropertyChangeListener(PropertyChangeListener l) {
		changes.addPropertyChangeListener(l);
	}

	/**
	 * 设置对象的新值
	 * 
	 * @param send
	 * @param content
	 */
	public void setObject(String send, String content) {
		changes.firePropertyChange(send, null, content);
	}
}

⌨️ 快捷键说明

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