实验四 图形用户界面设计
一、实验目的
1. 掌握Java GUI中的java.awt包和javax.swing包 2. 掌握图形用户界面程序设计的基本方法 3. 掌握事件处理模型
4. 使用Swing组件实现图形用户界面
二、准备工作
1. JDK的安装设置:JDK/JRE/JVM; 2. Eclipse集成开发环境的绿色安装;
三、实验描述
1. 实验类型:设计 2. 实验学时:2学时
3. 实验内容:创建简易聊天室(ChatRoom)的图形用户界面,并创建GUI 事件处理器
4. 具体要求: ① 创建项目及类: 类名称:ChatClient 项目:ChatRoomPrj 包:默认包
② 组件构成:该JFrame中有六个组件:中间输出文本域JTextArea组件,底部输入文本域JTextField组件,右边有两个JButton组件(Send和Quit),一个ComboBox组件用于选择发送消息的人名(昵称),以及一个菜单组件。
③ 使用嵌套面板和布局管理器来帮助构造上图所示的布局。具体如下所示: ※ 提示:JFrame默认采用BorderLayout,JMenuBar放置在North区,JTextArea放置在WEST区,JTextField放置在SOUTH区,CENTER区使用嵌套面板,该面板采用GridLayout(3行1列),分别添加两个JButton以及一个JComboBox组件。 ④ 创建ActionListener,当按下Send按钮时,将JComboBox组件选择的人名,与JTextField输入文本域的文本一起复制到JTextArea输出文本域。
1
⑤ 创建ActionListener,在输入文本域按下 Enter键时,将JComboBox组件选择的人名(昵称),与JTextField输入文本域的文本复制到JTextArea输出文本域。 ※ 提示:可以和上述 ④ 使用同一个监听器,以减少代码冗余。 ⑥ 创建ActionListener,当按下Quit按钮时,程序退出。
※ 提示:监听器可以使用内部类实现,退出使用System.exit(0)。 ⑦ 创建WindowListener,按下框架上的Close部件时,程序退出。
※ 提示:和上述 ⑧ 使用同一个监听器,以减少代码冗余。但是,该内部类要同时extends WindowAdapter 还要implements ActionListener ,以满足两个不同监听器的要求。 ⑧ 添加File菜单,该菜单必须包含Quit菜单项,选择该菜单项即终止程序。 ※ 提示:和上述 ⑥ 和 ⑦ 使用同一个监听器,以减少代码冗余。
⑨ 添加Help菜单,该菜单必须包含About菜单项,可从该菜单项弹出一个消息对话框,显示有关程序和开发人员的备注信息。
四、实验要求及总结
1. 结合上课内容,对上述程序先阅读,然后上机并调试程序,并对实验结果写出你自己的分析结论。
2. 整理上机步骤,总结经验和体会。 3. 完成实验报告和上交程序。 实验四: 实验结果:
import java.awt.*;
2
import java.io.*; import java.net.*;
import java.awt.color.*; import javax.swing.event.*; import java.util.*;
import java.net.Socket; import java.text.*;
import java.awt.event.*; import javax.swing.*;
public class CHatromm extends JFrame implements ActionListener, ItemListener { JMenuBar menubar;// 增加组件 JMenu File, Help, edit; JMenuItem send, quit, about, copy, cut, paste; JTextField text; JButton Send, Quit; JTextArea area; JComboBox choice, list; JPanel panel, panel2;// panel1放send,quit等按钮,panel2放菜单 Color red = new Color(255, 0, 0); // 使用color类创建color对象 Color green = new Color(0, 255, 0); Color blue = new Color(0, 0, 255); Color yellow = new Color(255, 255, 0); public CHatromm() { // 初始化JFrame setBounds(100, 100, 150, 150); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); init(); } public void init() { // init()方法实现组件的创建和添加 setTitle(\ FlowLayout flow = new FlowLayout(); flow.setAlignment(FlowLayout.LEFT);// 流式布局采用左对齐 panel2 = new JPanel(flow); menubar = new JMenuBar(); File = new JMenu(\
3
Help = new JMenu(\edit = new JMenu(\
send = new JMenuItem(\quit = new JMenuItem(\about = new JMenuItem(\copy = new JMenuItem(\cut = new JMenuItem(\paste = new JMenuItem(\list = new JComboBox();
GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment();// 获取本地计算机所有可用的字体 String fontmat[] = ge.getAvailableFontFamilyNames(); for (String str : fontmat) { list.addItem(str); }
send.setAccelerator(KeyStroke.getKeyStroke('S'));// 为菜单项建立快捷键 quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
about.setAccelerator(KeyStroke.getKeyStroke('A'));
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); panel2.setBackground(yellow); edit.add(cut); edit.add(copy); edit.add(paste); File.add(send); File.add(quit); File.add(edit); Help.add(about); menubar.add(File); menubar.add(Help); menubar.add(list); panel2.add(menubar); panel2.add(list);
add(panel2, BorderLayout.NORTH);// 把panel2放在JFrame北面 area = new JTextArea(90, 90); area.setLineWrap(true);
area.setForeground(Color.black);
add(new JScrollPane(area), BorderLayout.WEST);// JTextArea放在JFrame西面text = new JTextField(\
4
text.setHorizontalAlignment(JTextField.CENTER);// 设置JTextField文本居中显示 add(text, BorderLayout.SOUTH);// JTextField放在JFrame南面 panel = new JPanel();
panel.setLayout(new GridLayout(3, 1));// panel1采用3行1列的布局 Send = new JButton(\ \为按钮增加图片 Quit = new JButton(\ \
Quit.setHorizontalTextPosition(AbstractButton.CENTER); Quit.setBackground(red);// 设置按钮背景色 Send.setBackground(green);
Send.setHorizontalTextPosition(AbstractButton.CENTER); Quit.setMnemonic('q');// 为button设置键盘激活方式 Send.setMnemonic('s');
choice = new JComboBox(); choice.setBackground(blue); choice.setForeground(yellow); choice.addItem(\ choice.addItem(\ choice.addItem(\ choice.addItem(\ panel.add(Send); panel.add(Quit); panel.add(choice);
add(panel, BorderLayout.CENTER);// 注册侦听器 Send.addActionListener(this); Quit.addActionListener(this); send.addActionListener(this); quit.addActionListener(this); text.addActionListener(this); about.addActionListener(this); copy.addActionListener(this); cut.addActionListener(this); paste.addActionListener(this); list.addItemListener(this);
}
public void actionPerformed(ActionEvent e) { Date now = new Date();// 定义时间对象,显示聊天时间 DateFormat d = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); String str = d.format(now); if (e.getSource() == Send || e.getSource() == send || e.getSource() == text) {// 菜单项、按钮send响应和text按entry键5
area.append(choice.getSelectedItem() + \ + \ \ text.setText(null);
}
if (e.getSource() == Quit || e.getSource() == quit) {// quit键 System.exit(0); }
if (e.getSource() == about) { JOptionPane.showMessageDialog(File, \ \TION_MESSAGE); }
if (e.getSource() == copy) { area.copy(); text.copy(); }
if (e.getSource() == cut) { area.cut(); text.cut(); }
if (e.getSource() == paste) { area.paste(); text.paste(); }
}
public static void main(String[] args) { CHatromm chatroom = new CHatromm(); }
@Override
public void itemStateChanged(ItemEvent e) {// 设置显示字体 // TODO Auto-generated method stub String name = (String) list.getSelectedItem(); Font f = new Font(name, Font.BOLD, 20); area.setFont(f); }
}
6