?? 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 + -