📄 planeinformation.java
字号:
package planeInformation;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class PlaneInformation implements ActionListener {
Frame mainFrame;
Frame inputFrame;
Frame searchFrame;
TextArea plaInfo;
Label lb[] = new Label[4];
Label lb2[] = new Label[2];
TextField tf[] = new TextField[4];
TextField tf2[] = new TextField[2];
Button btn[] = new Button[3];
Button btn2[] = new Button[2];
Panel p1, p2, p3, p4;
IOOperation ioo;
Plane pla;
Plane plane[] = new Plane[100];
public PlaneInformation() {
/**
* 构造函数,初始话主窗口信息 set mainframe
*/
mainFrame = new Frame("PlaneInformation");
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 菜单栏File选项添加
MenuItem item1 = new MenuItem("Record");
MenuItem item2 = new MenuItem("Search");
MenuItem item3 = new MenuItem("Modify");
MenuItem item4 = new MenuItem("Exit");
MenuItem item5 = new MenuItem("About");
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
Menu menu1 = new Menu("File");
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu1.addSeparator();
menu1.add(item4);
Menu menu2 = new Menu("Help");
menu2.add(item5);
MenuBar mb = new MenuBar();
mb.add(menu1);
mb.add(menu2);
mainFrame.setMenuBar(mb);
// 文本框控制区域设置
plaInfo = new TextArea();
plaInfo.setFont(new Font("serif", Font.PLAIN, 18));// 字体格式
mainFrame.add(plaInfo);
mainFrame.setSize(400, 250);// 大小
mainFrame.setLocation(200, 100);// 位置
mainFrame.setVisible(true);// 显示Frame
/**
* 记录飞机信息的窗口设置
*/
inputFrame = new Frame();
inputFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
inputFrame.setVisible(false);
}
});
p1 = new Panel(new GridLayout(4, 2));
p2 = new Panel();
String lbname[] = { "FlightNumber:", "StartCity:", "Destination:",
"FlightTime" };
String btnname[] = { "Save", "Delete", " Exit " };
// Record窗口中各字段名称、按钮设置
for (int i = 0; i < 4; i++) {
lb[i] = new Label(lbname[i]);
tf[i] = new TextField(15);
p1.add(lb[i]);
p1.add(tf[i]);
}
for (int i = 0; i < 3; i++) {
btn[i] = new Button(btnname[i]);
btn[i].addActionListener(this);
p2.add(btn[i]);
}
btn[2].setActionCommand("input");// **用来获取对象的标签或事先为这个对象设置的命令名**//
inputFrame.add(p1, BorderLayout.CENTER);
inputFrame.add(p2, BorderLayout.SOUTH);
inputFrame.pack();
inputFrame.setLocationRelativeTo(mainFrame);
/**
* set searchFrame which is used to search plane information
*/
searchFrame = new Frame("Search plane");
searchFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
searchFrame.setVisible(false);
}
});
p3 = new Panel(new GridLayout(2, 2));
p4 = new Panel();
String lbname2[] = { "FlightNumber :", "StartCity:" };
String btnname2[] = { "Search", " Exit " };
for (int i = 0; i < 2; i++) {
lb2[i] = new Label(lbname2[i]);
tf2[i] = new TextField(15);
p3.add(lb2[i]);
p3.add(tf2[i]);
}
for (int i = 0; i < 2; i++) {
btn2[i] = new Button(btnname2[i]);
btn2[i].addActionListener(this);
p4.add(btn2[i]);
}
btn2[1].setActionCommand("search");
searchFrame.add(p3, BorderLayout.CENTER);
searchFrame.add(p4, BorderLayout.SOUTH);
searchFrame.pack();
searchFrame.setLocationRelativeTo(mainFrame);
/**
* IO operation object
*/
ioo = new IOOperation();
plane = ioo.getAllPlane();
}
public void actionPerformed(ActionEvent e) {
/**
* 按钮功能实现
*/
if (e.getSource() instanceof MenuItem) {
MenuItem mi = (MenuItem) e.getSource();
if (mi.getLabel().equals("Record")) {// 如果点击Record
inputFrame.setTitle("Record");
inputFrame.setSize(200, 200);
for (int i = 0; i < 4; i++)
tf[i].setText("");
p2.remove(btn[1]);// 如果是记录信息,删除‘删除’按钮
btn[0].setActionCommand("input");// 按钮‘保存’功能实现
inputFrame.setVisible(true);
} else if (mi.getLabel().equals("Search")) {
searchFrame.setVisible(true);
} else if (mi.getLabel().equals("Modify")) {
inputFrame.setTitle("Modify");
if (pla != null) {// 修改信息时,文本框信息显示
tf[0].setText(pla.getFlightNumber());
tf[1].setText(pla.getStartCity());
tf[2].setText(pla.getDestination());
tf[3].setText(pla.getFlightTime());
}
p2.remove(btn[2]);
p2.add(btn[1]);
p2.add(btn[2]);
btn[0].setActionCommand("modify");
inputFrame.setVisible(true);
} else if (mi.getLabel().equals("Exit"))
System.exit(0);
else if (mi.getLabel().equals("About")) {
final Dialog progInfo = new Dialog(mainFrame, "ProgInfo", true);
progInfo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
progInfo.dispose();
}
});
progInfo.setLayout(new FlowLayout());
Label l = new Label("Plane Information System");
progInfo.add(l);
progInfo.setSize(200, 80);
progInfo.setLocationRelativeTo(mainFrame);
progInfo.setVisible(true);
}
}
/**
* Button action
*/
else {
Button btn = (Button) e.getSource();
if (btn.getLabel().equals("Save")) {// 点击保存时,个文本框必须非空
if (!tf[0].getText().equals("") && !tf[1].getText().equals("")
&& !tf[2].getText().equals("")
&& !tf[3].getText().equals("")) {
Plane p = new Plane();
// Plane p = new Plane(tf[0].getText(), tf[1].getText(),
// tf[2]
// .getText(), tf[3].getText());
p.setFlightNumber(tf[0].getText());
p.setStartCity(tf[1].getText());
p.setDestination(tf[2].getText());
p.setFlightTime(tf[3].getText());
System.out.println(p.getFlightNumber());
if (btn.getActionCommand().equals("input")) {
for (int i = 0; i < plane.length; i++) {
if (plane[i] == null) {
plane[i] = p;
System.out.println(p.getFlightNumber());
break;
}
}
ioo.write(plane);
} else {
for (int i = 0; i < plane.length; i++) {
if (plane[i].equals(pla)) {
plane[i] = p;
break;
}
}
ioo.write(plane);
}
}
inputFrame.setVisible(false);
plaInfo.setText("");
} else if (btn.getLabel().equals("Delete")) {
int index = 200;
if (pla != null) {
for (int i = 0; i < plane.length; i++) {
if (plane[i] != null && plane[i].equals(pla)) {
index = i;
if (i != plane.length - 1)
plane[i] = plane[i + 1];
else
plane[i] = null;
}
if (i == index && plane[i + 1] == null)
break;
else if (i > index && i < plane.length - 1) {
plane[i] = plane[i + 1];
if (i == plane.length - 1)
plane[i] = null;
}
}
ioo.write(plane);
}
pla = null;
inputFrame.setVisible(false);
plaInfo.setText("");
} else if (btn.getLabel().equals("Search")) {
pla = null;
if (!tf2[0].getText().equals("")
|| !tf2[1].getText().equals("")) {
String condition = "";
if (!tf2[0].getText().equals("")) {
condition = tf2[0].getText();
System.out.println(tf2[0].getText());
} else {
condition = tf2[1].getText();
System.out.println(tf2[1].getText());
}
for (int i = 0; i < plane.length; i++) {
if (plane[i] != null) {
System.out.println(plane[i].getFlightNumber());
if (plane[i].getFlightNumber().equals(condition)
|| plane[i].getStartCity()
.equals(condition)) {
System.out.println(plane[i].getFlightNumber());
pla = plane[i];
break;
}
}
}
}
if (pla != null) {
plaInfo.setText("FlightNumber: " + pla.getFlightNumber()
+ "\n" + "StartCity: " + pla.getStartCity() + "\n"
+ "Destination: " + pla.getDestination() + "\n"
+ "FlightTime: " + pla.getFlightTime() + "\n");
}
searchFrame.setVisible(false);
} else if (btn.getLabel().equals(" Exit "))
if (btn.getActionCommand().equals("input"))
inputFrame.setVisible(false);
else
searchFrame.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -