//package Subway;
//������ѯϵͳ�������ʵ��
public class Main {
public static void main(String[] args) {
MessageQuery query = new MessageQuery();
query.setTitle("������Ϣ��ѯϵͳ");
query.setSize(600,450);
}
}
//package Subway;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Manager extends JDialog {
JLabel UserName; //用户名标签
JLabel UserPwd; //密码标签
JLabel Title; //标题
JTextField txName; //用户名输入文本框
JPasswordField txPwd; //密码框
JButton btnLogin; //登录按钮
JButton btnCancel; //取消登录按钮
JPanel jp[]; //元素为JPanel面板的数组
// 添加组件功能
JButton btnAddComponent;
JButton btnDeleteComponent;
JButton btnModifyComponent;
Manager(JFrame owner){
init();
}
void init(){
jp = new JPanel[4];
for(int i=0;i<=3;i++) {
jp[i] = new JPanel(); //利用循环创建面板
}
//在各个面板中添加相应的组件,并将面板添加至容器里
Title = new JLabel("管理员登陆系统");
jp[0].add(Title);
UserName = new JLabel("用户名");
txName = new JTextField(20);
jp[1].add(UserName);
jp[1].add(txName);
UserPwd = new JLabel("密 码");
txPwd = new JPasswordField(20);
jp[2].add(UserPwd);
jp[2].add(txPwd);
btnLogin = new JButton("登录");
btnCancel = new JButton("退出");
jp[3].add(btnLogin);
jp[3].add(btnCancel);
// 创建添加、删除和修改组件的按钮
btnAddComponent = new JButton("添加组件");
btnDeleteComponent = new JButton("删除组件");
btnModifyComponent = new JButton("修改组件");
jp[4] = new JPanel();
jp[4].add(btnAddComponent);
jp[4].add(btnDeleteComponent);
jp[4].add(btnModifyComponent);
this.add(jp[4]);
btnAddComponent.setEnabled(false); // 初始时禁用管理功能按钮
btnDeleteComponent.setEnabled(false);
btnModifyComponent.setEnabled(false);
for(int j=0;j<=4;j++) {
this.add(jp[j]);
}
this.setLayout(new GridLayout(4,1)); //设置布局
this.setSize(300,200); //设置对话框大小
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
btnLogin.addActionListener(new ActionListener() { //给登录按钮注册监听器
@Override
public void actionPerformed(ActionEvent e) {
if(txName.getText().equals("manager") && new String(txPwd.getPassword()).equals("123456")) {
JOptionPane.showMessageDialog(null, "登录成功!"); //根据登录结果弹出消息框
enableManagementFeatures(true); // 启用管理功能
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
}
});
btnCancel.addActionListener(new ActionListener() { //给退出按钮注册监听器
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0); //点击按钮则退出系统
}
});
}
private void enableManagementFeatures(boolean enable) {
btnAddComponent.setEnabled(enable);
btnDeleteComponent.setEnabled(enable);
btnModifyComponent.setEnabled(enable);
}
}
//package Subway;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
//MessageQuery������Ϊ������ѯϵͳ�ṩ��ͼ��GUI���棩
public class MessageQuery extends JFrame {
JPanel jp0,jp1,jp2; //���������������
JMenuBar menuBar; //�˵���
JMenu menu; //�˵�
JTextField textInput; //�����ı���
JButton btnQuery,btnClear; //��ѯ��ť����հ�ť
JLabel reminder; //��������Ϣ��ʾ��ǩ
JTextPane textShow; //��Ϣ��ʾ�ı����
FlowLayout layout; //���沼��
Manager manager; //����Ա��¼�Ի������
SubwayLine subway; //������·�����
MessageQuery(){
init();
setVisible(true); //���ô��ڿɼ�
setLocationRelativeTo(null); //���ô���λ��
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init() {
menuBar = new JMenuBar(); //�����˵���
menu = new JMenu("����Ա"); //�����˵�
manager = new Manager(this); //��������Ա��¼�Ի���this��ָ���ô�������Ϊ�Ի��������Ĵ���
jp0 = new JPanel();
jp1 = new JPanel();
jp2 = new JPanel();
textInput = new JTextField(10); //�û������ı���
btnQuery = new JButton("��ѯ"); //��ѯ��ť
btnClear = new JButton("���"); //��������ı���ť
reminder = new JLabel("��������ʣ��硰1���ߡ�������ȹ㳡����."); //��ʾ��Ϣ
reminder.setForeground(Color.red); //������ʾ��Ϣ������ɫ
StyledDocument s = new DefaultStyledDocument(); //�ĵ�ģ��
textShow = new JTextPane(s); //��������ָ���ĵ�ģ�͵���Ϣ��ʾ�ı����
textShow.setEditable(false); //���ø��ı����ڲ��ɱ༭
textShow.setPreferredSize(new Dimension(550,250)); //�����ı�����С
layout = new FlowLayout();
layout.setVgap(15); //���������ֱ��϶
subway = new SubwayLine();
menuBar.add(Box.createHorizontalGlue()); //ʹ�˵�������ӵIJ˵�����ʾ���ұ�
menuBar.add(menu); //��Ӳ˵�
jp0.setLayout(new GridLayout(2,1));
jp0.add(jp1);
jp0.add(jp2);
jp1.add(new JLabel("�û��� ������Ϣ��ѯ"));
jp1.add(textInput);
jp1.add(btnQuery); //����������Ӧ���
jp1.add(btnClear);
jp2.add(reminder);
//����������������������
this.setLayout(layout);
this.setJMenuBar(menuBar);
this.add(jp0);
this.add(new JScrollPane(textShow)); //���Ը��������textShow��ӹ���
menu.addMenuListener(new MenuListener() { //Ϊ������Ա���˵�����Ӽ�����
public void menuSelected(MenuEvent e) {
setVisible(false);
manager.setVisible(true); //��������Ա��¼�Ի���ͬʱ�ر�ԭ����
}
@Override
public void menuDeselected(MenuEvent e) {
}
@Override
public void menuCanceled(MenuEvent e) {
}
});
btnQuery.addActionListener(new ActionListener(){ //Ϊ��Ϣ��ѯ��ť��Ӽ�����
public void actionPerformed(ActionEvent e) {
String input = textInput.getText(); //��ȡ�û��������Ϣ
SimpleAttributeSet set1,set2,set3;
set1 = new SimpleAttributeSet();
set2 = new SimpleAttributeSet();
set3 = new SimpleAttributeSet(); //�½��������Լ�
StyleConstants.setFontSize(set1,22); //Ϊ���Լ�set1���������С
StyleConstants.setBold(set1,true); // ����
StyleConstants.setForeground(set1,Color.BLUE); // Ϊ���Լ�set1����������ɫ
StyleConstants.setFontSize(set2,15);
StyleConstants.setForeground(set2,Color.BLACK);
StyleConstants.setFontSize(set3,25);
StyleConstants.setForeground(set3,Color.RED); //ͬ������Լ�set2��set3������Ӧ����
if(subway.LineQuery(input)) { //�ж�������Ƿ�Ϊ��·����
int lineNumber = subway.getLineIndex(input); //������·���Ƶõ�������·�������ж�Ӧ������ֵ
//�ĵ�ģ�������·��Ϣ
try {
textShow.setText(null);//�����壬��ÿ����һ��������ǰ������Ľ���������
s.insertString(s.getLength(),"�人����"+input+" "+subway.getHead(lineNumber)+"����"+subway.getEnd(lineNumber)+" "+"ȫ��"+subway.lineStation[lineNumber].length+"վ\n\n",set1);
//������⣬������·���ơ���վ��ĩվ������վ��������set1���Լ�
s.insertString(s.getLength(),"����ʱ��"+"����"+"������6:00����Ϣ��6:30-23:00\n",set2);
s.insertString(s.getLength(),"����վ��"+"����"+subway.getStation(lineNumber)+"\n",set2);
s.insertString(s.getLength(),"��·����"+"����"+subway.getLength(lineNumber)+"ǧ��\n",set2);
s.insertString(s.getLength(),"��·Ʊ��"+"����"+subway.getFare(lineNumber)+"Ԫ\n",set2);
//���������Ϣ����������ʱ�䡢��·���ȡ�����վ�����·Ʊ�ۣ�����set2���Լ�
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
else if(subway.stationQuery(input)){ //�ж�������Ƿ�Ϊ����վ��
ArrayList<Integer> lineNumberArray = subway.getLineIndexArray(input); //����վ�����Ƶõ����Ӧ��������·����ֵ��������������
try {
textShow.setText(null); //������
for(int n:lineNumberArray) { //����forѭ�������洢����·����ֵ�����飬�����վ������Ӧ��������·����Ϣ
s.insertString(s.getLength(),"�人����"+subway.getlineName(n)+" "+subway.getHead(n)+"����"+subway.getEnd(n)+" "+"ȫ��"+subway.lineStation[n].length+"վ\n\n",set1);
s.insertString(s.getLength(),"����ʱ��"+"����"+"������6:00����Ϣ��6:30-23:00\n",set2);
s.insertString(s.getLength(),"����վ��"+"����"+subway.getStation(n)+"\n",set2);
s.insertString(s.getLength(),"��·����"+"����"+subway.getLength(n)+"ǧ��\n",set2);
s.insertString(s.getLength(),"��·Ʊ��"+"����"+subway.getFare(n)+"Ԫ\n",set2);
//���������Ϣ
}
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
else {
try {
textShow.setText(null); //�����ش������������ʾ������set3���Լ�
s.insertString(s.getLength(),"û���ҵ������Ϣ�����������Ƿ�����",set3);
}catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
});
btnClear.addActionListener(new ActionListener(){ //Ϊ��������ı���ť��Ӽ�����
public void actionPerformed(ActionEvent e) {
textInput.setText(null);
}
});
}
}
//package Subway;
import java.util.ArrayList;
import java.util.Arrays;
public class SubwayLine {
public String lineName[]; //线路名称
public String lineStation[][]; //线路上的站点
int fare[]; //线路票价
double Length[]; //线路长度
SubwayLine(){ //构造方法
lineName = new String[] {"1号线","2号线","3号线","4号线"};
fare = new int[] {4,6,4,3};
Length = new double[] {38.54,60.8,30,50};
lineStation = new String[4][];
lineStation[0] = new String[] {"汉口北","徐州新村","黄浦路","循礼门","太平洋","汉西一路","竹叶海","东吴大道","三店","泾河"};
lineStation[1] = new String[] {"天河机场","金银潭","汉口火车站","循礼门","江汉路","街道口","光谷广场","华中科技大学","光谷大道","武汉东站","佛祖岭"};
lineStation[2] = new String[] {"宏图大道","罗家庄","云飞路","王家湾","三角湖","体育中心","东风公司","沌阳大道"};
lineStation[3] = new String[] {"新城十一路","金银湖","循礼门","轻工大学","香港路","江汉路","汉正街","东风公司"};
}
public boolean LineQuery(String message1) { //线路查询方法,判断输入的线路是否存在
boolean isfind = false;
for(int i=0;i<lineName.length;i++) {
if(lineName[i].equals(message1)) {
isfind = true;
break;
}
}
return isfind;
}
public boolean stationQuery(String message2) { //站点查询方法,判断输入的站点是否存在
boolean isfind = false;
for(int i=0;i<lineStation.length;i++) {
for(int j=0;j<lineStation[i].length;j++) {
if(lineStation[i][j].equals(message2)) {
isfind = true;
break;
}
}
}
return isfind;
}
public int getLineIndex(String line) { //根据线路名称得到其对应的索引值
int number = 0;
for(int i=0;i<lineName.length;i++) {
if(lineName[i].equals(line)) {
number = i;
}
}
return number;
}
public ArrayList<Integer> getLineIndexArray(String station) { //根据站点名称得到对应所有线路的索引值,并放入数组中
ArrayList<Integer> numberArray = new ArrayList<>(); //ArrayList只能用于函数中,不能用于类里,创建一个空数组
for(int i=0;i<lineStation.length;i++) {
for(int j=0;j<lineStation[i].length;j++) {
if(lineStation[i][j].equals(station)) {
numberArray.add(i); //将站点所对应的所有线路索引存储到数组numberArray中
}
}
}
return numberArray;
}
public String getlineName(int number) { //返回线路名称
return lineName[number];
}
public String getStation(int number) { //得到线路上的站点
return Arrays.toString(lineStation[number]);
}
public String getHead(int number) { //返回首站名称
return lineStation[number][0];
}
public String getEnd(int number) { //返回末站名称
return lineStation[number][lineStation[number].length-1];
}
public double getLength(int number) { //返回线路长度
return Length[number];
}
public int getFare(int number) { //返回线路票价
return fare[number];
}
}
【无标题】
最新推荐文章于 2024-09-15 09:47:04 发布