📄 textchatroom.java
字号:
/******************************************************************************
* [开发目标] 聊天室的制作实现
******************************************************************************/
/******************************************************************************
* Copyright (C) 2007 xxx Corporation.
* [系 统] yyy系统标准版
* [包 名] 无
* [源文件名] TextChatroom.java
* [类 名] TextChatroom。Mydialog
* -----------------------------------------------------------------------------
* REVISION 修改日期 修改者 修改内容
* -----------------------------------------------------------------------------
* 1.0.00 2007-05-02 孙瑛 修改
******************************************************************************/
/**
*聊天室的制作实现
*
* @version 1.0.00
* @author 孙瑛
*
*/
import java.awt.*;
import java.awt.event.*;
//创建Dialog类。
class Mydialog extends Dialog implements ActionListener
{
//Dialog类的构造函数。
public Mydialog()
{
super(new Frame(), "About", false);
Button but = new Button("确定");
Label labe = new Label(" Produced By SunYing");
//给Button添加监听器
but.addActionListener(this);
//dialog的布局
setLayout(new FlowLayout());
add(labe);
add(but);
setLocation(90, 80);
setSize(170, 100);
setVisible(true);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
}
);
}
//实现Mydialog的ActionListener接口
public void actionPerformed(ActionEvent evt)
{
String arg = evt.getActionCommand();
if(arg.equals("确定"))
{
//释放资源。
dispose();
}
}
};
//聊天室的class,实现ActionListener接口和接受事件的ItemListener接口
public class TextChatroom extends Frame implements ActionListener, ItemListener
{
//创建Send和Exit按钮
Button button1 = new Button("Send");
Button button2 = new Button("Exit");
//创建单选框
CheckboxGroup chk = new CheckboxGroup();
Checkbox cx1 = new Checkbox("Lucy", chk, false);
Checkbox cx2 = new Checkbox("Peter", chk, false);
Checkbox cx3 = new Checkbox("Mary", chk, false);
//创建菜单
MenuBar mb = new MenuBar();
Menu m1 = new Menu("File");
Menu m2 = new Menu("Help");
MenuItem mm1 = new MenuItem("Quit");
MenuItem mm2 = new MenuItem("About");
//创建Text文本区
TextArea area = new TextArea("", 10, 50);
TextField field = new TextField("", 50);
//构造函数。
public TextChatroom()
{
super(" Chat Room");
//addWindowListener
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
//给button添加事件监听器
button1.addActionListener(this);
button2.addActionListener(this);
//设置大小。
button1.setSize(80,30);
button2.setSize(80,30);
//给单选框注册事件监听器。
cx1.addItemListener(this);
cx2.addItemListener(this);
cx3.addItemListener(this);
//把menu放到menubar中
mb.add(m1);
mb.add(m2);
//把menuitem放到menu中
m1.add(mm1);
m2.add(mm2);
//给menuitem注册事件监听器
mm1.addActionListener(this);
mm2.addActionListener(this);
}
//实现ActionListener 接口
public void actionPerformed(ActionEvent evt)
{
String arg = evt.getActionCommand();
if(arg.equals("Send"))
{
if(field.getText() == "")
{
System.out.println("聊天内容不能为空");
}
if(field.getText() != "")
{
String s = field.getText()+'\n';
area.append(s);
field.setText("");
}
}
if(arg.equals("Exit"))
{
System.exit(0);
}
if(arg.equals("Quit"))
{
System.exit(0);
}
if(arg.equals("About"))
{
//创建对话框。
Mydialog dia = new Mydialog();
}
}
//重载单选框的事件监听器
public void itemStateChanged(ItemEvent evt)
{
if(evt.getItemSelectable()== cx1)
{
area.append("To Lucy:");
}
if(evt.getItemSelectable()== cx2)
{
area.append("To Peter:");
}
if(evt.getItemSelectable()== cx3)
{
area.append("To Mary:");
}
}
//实现聊天室的布局管理
public void launchFrame()
{
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();
//使得menubar可见
setMenuBar(mb);
//把button1,button2放到Panel p2中
p2.setLayout(new BorderLayout());
p2.add(button1, BorderLayout.NORTH);
p2.add(button2, BorderLayout.SOUTH);
//把单选框选项放到p3中
p3.setLayout(new BorderLayout());
p3.add(cx1, BorderLayout.NORTH);
p3.add(cx2, BorderLayout.CENTER);
p3.add(cx3, BorderLayout.SOUTH);
//把p2,p3放到p4中
p4.setLayout(new BorderLayout());
p4.add(p2, BorderLayout.NORTH);
p4.add(p3,BorderLayout.SOUTH);
//绘制颜色
field.setBackground(Color.cyan);
area.setBackground(Color.lightGray);
button1.setBackground(Color.pink);
button2.setBackground(Color.pink);
p4.setBackground(Color.yellow);
//把area,p4放到p1中
p1.setLayout(new BorderLayout());
p1.add(area, BorderLayout.WEST);
p1.add(p4, BorderLayout.EAST);
//把p1,field放到frame中
setLayout(new BorderLayout());
this.add(p1, BorderLayout.NORTH);
//this.add(p2);
this.add(field, BorderLayout.SOUTH);
}
//main函数
public static void main(String [] args)
{
TextChatroom textchatroom = new TextChatroom();
textchatroom.launchFrame();
textchatroom.setSize(430,250);
textchatroom.setVisible(true);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -