banner
NEWS LETTER

实验四:图形界面及应用系统的设计

Scroll down

实验四:图形界面及应用系统的设计

实验目的:

  1. 了解 java.awt 包及 Swing 的新增特性。
  2. 了解 Java 系统图形用户界面的基本工作原理和界面设计的步骤。
  3. 掌握图形用户界面各种布局策略的设计与使用。
  4. 了解图形用户界面的事件响应机制,并能进行常用的事件处理,如按钮事件、键盘事件、窗口事件等。
  5. 掌握图形用户界面各种常用的基本组件的使用方法及其事件响应。
  6. 了解面向对象数据流的概念, 了解常见的输入输出流以及分类。
  7. 掌握文件描述,顺序处理,随机访问处理的方法
  8. 掌握文件与目录的操作过程。

实验内容及要求:

制作如图一个留言板的界面,并按要求加入所需控件,并能按要求进行窗口控件的布局。并按要求为按钮、文本框、窗口添加事件,使之实现提交显示留言,清屏,留言至顶和至尾。要求文本框能自动产生滚动条,界面美观。

注意:

  1. 通过两个文本文件存储表情和留言内容。
  • Expression.dat:用于存储表情,如:微笑、大哭、流泪等;每个表情占一行。
  • Msg.dat:用于存储留言信息,留言信息格式为:

[2019-10-04 12:35] 你微笑地说:今天下午去图书馆吗?

  • 其中:时间是提交留言的时间,每条留言一行。
  1. 窗口启动的时候(或点击“查看”按钮时),从 Msg.dat 文件中读出所有留言记录,显示在文本框中;从 Expression.dat 文件中读出所有表情记录,显示在表情下拉列表中。将 Msg.dat 中所有留言记录,倒序显示在文本域中。
  2. 点击提交按钮,将表单内容存入文件,同时刷新文本框的留言内容。
  3. “清屏”代表清除留言框内容,“至顶”和“至尾”功能是当留言内容过多时,将滚动条滚动到最上面或最下面,实现过程为移动文本区域里面的光标,使其指向第一个位置和最后一个位置。(设置光标位置函数为:setCaretPosition(int 位置))
    5.(选作)增加“表情维护”按钮,在新窗口修改表情文件的内容
    6.(选作)增加“删除留言”按钮,在新窗口通过选择数字或全部删除相关的留言内容
  4. 时间安排:前3学时,做界面和基本的显示效果操作事件,后3学时将文件操作引入相关的事件中。

※※※※本实验代码转载于nice bug※※※※

原作者:https://blog.csdn.net/qq_45441117/article/details/111334825

如果你有时间可以去学习一下:https://blog.csdn.net/xietansheng/article/details/72814492

有到了面向CSDN编程了,我只是大自然搬运工!我也才开始学java(二级都没过纯小白!!!!)

加了一些注释 左上角图标我换了 表情文件 消息记录

55)
13
15

Framers类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//implements 是实现多个接口, 接口的方法一般为空的, 必须重写才能使用 ,ActionListener, KeyListener键盘监听、动作监听
public class Framers extends JFrame implements ActionListener, KeyListener {
JTextArea t1;//留言框区域
JButton but1, but2, but3, but4, but5, but6;//按钮
JComboBox looks;//下拉表情列表组件
JTextField lo;//文本框
JLabel lbl1, lbl2, lbl3;//标签,构成留言框的每个部分

//有参构造方法
public Framers(String title) {
super(title);//父类调用子类标题
this.setSize(1000, 600);//设置留言框大小
this.setFont(new Font("System", Font.PLAIN, 14));
//下面都只是为了把标题居中不够好
Font f = this.getFont();
FontMetrics fm = this.getFontMetrics(f);
int x = fm.stringWidth(title);
int y = fm.stringWidth(" ");
int z = this.getWidth() / 2 - (x / 2);
int w = z / y;
String pad = " ";
pad = String.format("%" + w + "s", pad);
this.setTitle(pad + title);

this.setIconImage(new ImageIcon("src./favicon.jpg").getImage());//设置左上角图标
this.winInit();//留言框初始化
this.setVisible(true);

}

public void winInit() {
lbl1 = new JLabel("留言板", 0);// horizontalAlignment: 标签内容(在标签内)的水平对其方式(竖直方向默认居中, 可通过方法设置)
lbl2 = new JLabel("你");
lbl3 = new JLabel("地说:");
t1 = new JTextArea("留言内容:\n");
t1.setLineWrap(true);//自动换行
t1.setEditable(false);//设置留言板内容不可更改
t1.setBackground(new Color(0xFF, 0xFF, 0xEE));//设置留言版的背景色
t1.setForeground(new Color(0, 0, 255));//设置留言板字体颜色
lo = new JTextField(35);//设置输入框长度
looks = new JComboBox();
// looks.addItem("哭着");
// looks.addItem("笑着");
// looks.addItem("哈哈哈哈");
// looks.addItem("乐呵呵");
// looks.addItem("笑嘻嘻");
this.loadExp(); //文件加载表情
//创建按钮
but1 = new JButton("提交");
but2 = new JButton("清屏");
but3 = new JButton("至顶");
but4 = new JButton("至尾");
but5 = new JButton("查看");
but6 = new JButton("删除");
//给标签、文本区域、按钮、表情下拉列表设置字体,宋体 加粗,大小:20磅
lbl1.setFont(new Font("宋体", Font.BOLD, 20));
lbl2.setFont(new Font("宋体", Font.BOLD, 20));
lbl3.setFont(new Font("宋体", Font.BOLD, 20));
t1.setFont(new Font("宋体", Font.BOLD, 20));
lo.setFont(new Font("宋体", Font.BOLD, 20));
but1.setFont(new Font("宋体", Font.BOLD, 20));
but2.setFont(new Font("宋体", Font.BOLD, 20));
but3.setFont(new Font("宋体", Font.BOLD, 20));
but4.setFont(new Font("宋体", Font.BOLD, 20));
but5.setFont(new Font("宋体", Font.BOLD, 20));
but6.setFont(new Font("宋体", Font.BOLD, 20));
looks.setFont(new Font("宋体", Font.BOLD, 20));
// 添加 和 移除 按钮的点击事件
but1.addActionListener(this); //提交
but2.addActionListener(this); //清屏
but3.addActionListener(this); //至顶
but4.addActionListener(this); //至尾
but5.addActionListener(this); //查看
but6.addActionListener(this); //删除
//键盘输入监听器
lo.addKeyListener(this);
//创建不同面板
JPanel pEast = new JPanel();
JPanel pSouth = new JPanel();
JScrollPane scroll = new JScrollPane(t1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);//加滚轮 垂直滚动条一直显示, 水平滚动条从不显示
pEast.setLayout(new GridLayout(8, 1, 0, 30));//东方为网格布局 指定 行数 和 列数 的网格布局, 并指定 水平 和 竖直 网格间隙
//面板
pSouth.add(lbl2);
pSouth.add(looks);
pSouth.add(lbl3);
pSouth.add(lo);
pSouth.add(but1);
pEast.add(but2);
pEast.add(but3);
pEast.add(but4);
pEast.add(but5);
pEast.add(but6);
//将面板加入窗口 位置
add(scroll, "Center");
add(lbl1, "North");
add(pEast, "East");
add(pSouth, "South");
this.loadMsg(); //加载文件中的内容
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

//加载文件中的表情
public void loadExp() {
looks.removeAllItems();
try {
FileReader Re = new FileReader("Expression.dat");
BufferedReader br = new BufferedReader(Re);
String exp;
while ((exp = br.readLine()) != null) {
looks.addItem(exp);
}
br.close();
} catch (IOException e1) {
System.out.println("读取文件失败" + e1);
}
}

//加载文件中的留言内容
public void loadMsg() {
t1.setText("留言内容:\n");
try {
BufferedReader br = new BufferedReader(new FileReader("msg.dat"));
String msg;
while ((msg = br.readLine()) != null) {
t1.insert(msg + "\n", 6);
}
br.close();
} catch (IOException e1) {
System.out.println("读文件失败" + e1);
}
}

//提交留言过程
public void msgSubmit() {
if (lo.getText().trim().length() == 0) return;
//获取当前时间
Date crr = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");//初始化时间格式
String curr = sdf.format(crr);
String loms = "[" + curr + "]" + "你" + looks.getSelectedItem() + "地说:" + lo.getText();
t1.insert(loms + "\n", 6);
//写入msg.dat文件
File f = new File("Msg.dat");
try {
FileWriter fw = new FileWriter(f, true);//追加
fw.write(loms + "\r\n");
fw.close();
} catch (IOException e1) {
System.out.println("写入文件失败" + e1);
}
lo.setText("");
}

public void delete() {
String lom = " ";
File f = new File("Msg.dat");
try {
FileWriter fw = new FileWriter(f);//追加
fw.write(lom + "\r\n");
fw.close();
} catch (IOException e1) {
System.out.println("写入文件失败" + e1);
}
}

public void actionPerformed(ActionEvent e) {
String com = e.getActionCommand();
switch (com) {
case "提交": {
msgSubmit();
break;
}
case "清屏": {
t1.setText("留言内容:\n");
break;
}
case "至顶": {
t1.setCaretPosition(0);
break;
}
case "至尾": {
t1.setCaretPosition(t1.getText().length() - 1);
break;
}
case "查看": {
this.loadMsg();
break;
}
case "删除": {
this.delete();
break;
}
}
}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == 10) {
msgSubmit();
}
}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
}

FramelessDemo测试类

1
2
3
4
5
6
public class FramelessDemo {
public static void main(String[] args) {
Framers fr = new Framers("小小留言板");
fr.setVisible(true);
}
}

运行结果:

4

其他文章
目录导航 置顶
  1. 1. 实验四:图形界面及应用系统的设计
    1. 1.1. 实验目的:
    2. 1.2. 实验内容及要求:
      1. 1.2.1. 制作如图一个留言板的界面,并按要求加入所需控件,并能按要求进行窗口控件的布局。并按要求为按钮、文本框、窗口添加事件,使之实现提交显示留言,清屏,留言至顶和至尾。要求文本框能自动产生滚动条,界面美观。
      2. 1.2.2. 注意:
      3. 1.2.3. [2019-10-04 12:35] 你微笑地说:今天下午去图书馆吗?
    3. 1.3. ※※※※本实验代码转载于nice bug※※※※
    4. 1.4. 原作者:https://blog.csdn.net/qq_45441117/article/details/111334825
    5. 1.5. 如果你有时间可以去学习一下:https://blog.csdn.net/xietansheng/article/details/72814492
      1. 1.5.1. 有到了面向CSDN编程了,我只是大自然搬运工!我也才开始学java(二级都没过纯小白!!!!)
    6. 1.6. 加了一些注释 左上角图标我换了 表情文件 消息记录
      1. 1.6.1. Framers类
      2. 1.6.2. FramelessDemo测试类
      3. 1.6.3. 运行结果:
  2. 2. 运行结果: