📄 comprehenquery.java
字号:
//import flight.assist.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.sql.*;
public class ComprehenQuery extends JPanel implements ActionListener,ItemListener
{
static SqlBean sqlBean = new SqlBean();
private static DefaultComboBoxModel modelPlace = new DefaultComboBoxModel();
//航空公司下拉列表
private static DefaultComboBoxModel modelAirFirm = new DefaultComboBoxModel();
private static Object[] year = {" 2007"," 2008"};
private static Object[] month = {"1","2","3","4","5","6","7","8","9","10","11","12"};
private static Object[] day = {"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"};
private static JComboBox jcbStart = new JComboBox(),jcbFirstArrive = new JComboBox(),
jcbArrive = new JComboBox(),jcbAirFirm = new JComboBox(modelAirFirm),
jcbYear1 = new JComboBox(year),jcbYear2 = new JComboBox(year),
jcbMonth2 = new JComboBox(month),jcbMonth1 = new JComboBox(month),
jcbDay1 = new JComboBox(day),jcbDay2 = new JComboBox(day);
private JRadioButton jrbSingle = new JRadioButton("单程",true),
jrbDouble = new JRadioButton("往返",false),
jrbMutiple = new JRadioButton("联程",false);
private JLabel jlStart,jlFirstArrive,jlArrive,
jlTime1,jlTime2,jlAirFirm,
jlReplaceArrive,jlReplaceTime;
private JPanel jpFirstArriveBox,jpTime2Box,jpReplaceArrive,jpArrive1,jpReplaceTime,jpTime2;
private JButton jbQuery = new JButton("查询");
private String start,firstArrive,arrive,leaveWeek,leaveWeek2,backWeek,airFirm,
leaveYear,leaveMonth,leaveDay,backYear,backMonth,backDay,
leaveYear2,leaveMonth2,leaveDay2;
public ComprehenQuery()
{
jcbAirFirm.addItem("所有");
jcbAirFirm.setSelectedItem("所有");
setDisplayPresentTime(); //得到当前时间
JPanel jp1 = new JPanel();
jp1.add(jrbSingle);
JPanel jp2 = new JPanel();
jp2.add(jrbDouble);
JPanel jp3 = new JPanel();
jp3.add(jrbMutiple);
JPanel jpRadioButton = new JPanel();
jpRadioButton.setLayout(new GridLayout(5,1));
jpRadioButton.add(new JLabel(" "));
jpRadioButton.add(jp1);
jpRadioButton.add(jp2);
jpRadioButton.add(jp3);
jpRadioButton.add(new JLabel(" "));
ButtonGroup bg = new ButtonGroup();
bg.add(jrbSingle);
bg.add(jrbDouble);
bg.add(jrbMutiple);
JPanel jpStart = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jpStart.add(jlStart = new JLabel(" 出发城市:"));
JPanel jpFirstArrive = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jpFirstArrive.add(jlFirstArrive = new JLabel(" "));
JPanel jpArrive = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jpArrive.add(jlArrive = new JLabel(" 到达城市:"));
JPanel jpTime1Tip = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jpTime1Tip.add(jlTime1 = new JLabel(" 出发日期:"));
JPanel jpTime2Tip = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jpTime2Tip.add(jlTime2 = new JLabel(" "));
JPanel jpAirFirm = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jpAirFirm.add(jlAirFirm = new JLabel(" 航空公司:"));
JPanel jpLabels = new JPanel();
jpLabels.setLayout(new GridLayout(7,1));
jpLabels.add(jpStart);
jpLabels.add(jpFirstArrive);
jpLabels.add(jpArrive);
jpLabels.add(jpTime1Tip);
jpLabels.add(jpTime2Tip);
jpLabels.add(jpAirFirm);
jpLabels.add(new JLabel(" "));
JPanel jpStartBox = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpStartBox.add(jcbStart);
jpReplaceArrive = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpReplaceArrive.add(jlReplaceArrive = new JLabel(" "));
jpArrive1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpArrive1.add(jcbFirstArrive);
jpFirstArriveBox = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
jpFirstArriveBox.add(jpReplaceArrive);
JPanel jpArriveBox = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpArriveBox.add(jcbArrive);
JPanel jpTime1Box = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpTime1Box.add(jcbYear1);
jpTime1Box.add(jcbMonth1);
jpTime1Box.add(jcbDay1);
jpReplaceTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpReplaceTime.add(jlReplaceTime = new JLabel(" "));
jpTime2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpTime2.add(jcbYear2);
jpTime2.add(jcbMonth2);
jpTime2.add(jcbDay2);
jpTime2Box = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
jpTime2Box.add(jpReplaceTime);
//***********************
JPanel jpAirFirmBox = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpAirFirmBox.add(jcbAirFirm);
JPanel jpButton = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpButton.add(jbQuery);
JPanel jpComboBox = new JPanel();
jpComboBox.setLayout(new GridLayout(7,1));
jpComboBox.add(jpStartBox);
jpComboBox.add(jpFirstArriveBox);
jpComboBox.add(jpArriveBox);
jpComboBox.add(jpTime1Box);
jpComboBox.add(jpTime2Box);
jpComboBox.add(jpAirFirmBox);
jpComboBox.add(jpButton);
JPanel jpQuery = new JPanel();
jpQuery.setLayout(new BorderLayout());
jpQuery.add(jpLabels,BorderLayout.WEST);
jpQuery.add(jpComboBox,BorderLayout.CENTER);
JPanel jpDown = new JPanel();
jpDown.setLayout(new BorderLayout());
jpDown.add(jpRadioButton,BorderLayout.WEST);
jpDown.add(jpQuery,BorderLayout.CENTER);
JLabel jlTitle = new JLabel("综合查询");
jlTitle.setHorizontalAlignment(JLabel.CENTER);
jlTitle.setFont(new Font("Tims",Font.BOLD,23));
this.setLayout(new BorderLayout());
this.add(jlTitle,BorderLayout.NORTH);
this.setBorder(new TitledBorder("综合查询"));
this.add(jpDown,BorderLayout.CENTER);;
jrbSingle.addActionListener(new RadioListener()); //添加监听
jrbDouble.addActionListener(new RadioListener());
jrbMutiple.addActionListener(new RadioListener());
jcbYear1.addItemListener(this);
jcbYear2.addItemListener(this);
jcbMonth1.addItemListener(this);
jcbMonth2.addItemListener(this);
jbQuery.addActionListener(this);
}
public static void updatePlaceComboBox(String newPlace,int insertOrDelete)
{
if (insertOrDelete == 1)
{
if (modelPlace.getIndexOf(newPlace) == -1)
{
modelPlace.addElement(newPlace);
jcbStart.addItem(newPlace);
jcbFirstArrive.addItem(newPlace);
jcbArrive.addItem(newPlace);
}
}
else if (insertOrDelete == 2)
{
if (modelPlace.getIndexOf(newPlace) != -1)
{
modelPlace.removeElement(newPlace);
jcbStart.removeItem(newPlace);
jcbFirstArrive.removeItem(newPlace);
jcbArrive.removeItem(newPlace);
}
}
}
public static void updateAirFirmComboBox(String newAirFirm,int insertOrDelete)
{
if (insertOrDelete == 1)
{
if (modelAirFirm.getIndexOf(newAirFirm) == -1)
jcbAirFirm.addItem(newAirFirm);
}
else if (insertOrDelete == 2)
{
if (modelAirFirm.getIndexOf(newAirFirm) != -1)
jcbAirFirm.removeItem(newAirFirm);
}
}
public void setDisplayPresentTime()
{
Calendar cal = Calendar.getInstance();
cal.setTime(new java.util.Date());
String year = String.valueOf(cal.get(Calendar.YEAR)); //得到当前年月日
String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
jcbYear1.setSelectedItem(year);
jcbYear2.setSelectedItem(year);
jcbMonth1.setSelectedItem(month);
jcbMonth2.setSelectedItem(month);
updateDay(year,month,jcbDay1);
updateDay(year,month,jcbDay2);
jcbDay1.setSelectedItem(day);
jcbDay2.setSelectedItem(day);
}
private void updateDay(String year,String month,JComboBox jcb)
{
// 4,6,9,11月是30天
if (month.equals("4") || month.equals("6") || month.equals("9") || month.equals("11"))
{
if (jcb.getItemCount() == 31) //其他为31天
jcb.removeItem("31");
else if(jcb.getItemCount() == 29) //闰年二月
jcb.addItem("30");
else if (jcb.getItemCount() == 28) //非闰年二月
{
jcb.addItem("29");
jcb.addItem("30");
}
}
else if (month.equals("2"))
{
int years = Integer.parseInt(year);
//是闰年
if ( (years % 400 == 0) || (years %4 == 0 && years % 100 != 0))
{
if (jcb.getItemCount() == 31)
{
jcb.removeItem("30");
jcb.removeItem("31");
}
else if (jcb.getItemCount() == 30)
{
jcb.removeItem("30");
}
else if (jcb.getItemCount() == 28)
{
jcb.addItem("29");
}
}
else //不是闰年
{
if (jcb.getItemCount() == 29)
{
jcb.removeItem("29");
}
else if (jcb.getItemCount() == 30)
{
jcb.removeItem("29");
jcb.removeItem("30");
}
else if (jcb.getItemCount() == 31)
{
jcb.removeItem("29");
jcb.removeItem("30");
jcb.removeItem("31");
}
}
}
else
{
if (jcb.getItemCount() == 28)
{
jcb.addItem("29");
jcb.addItem("30");
jcb.addItem("31");
}
else if (jcb.getItemCount() == 29)
{
jcb.addItem("30");
jcb.addItem("31");
}
else if (jcb.getItemCount() == 30)
{
jcb.addItem("31");
}
}
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == jcbYear1 || e.getSource() == jcbMonth1)
{
String year = (String)jcbYear1.getSelectedItem();
String month = (String)jcbMonth1.getSelectedItem();
updateDay(year,month,jcbDay1);
}
if (e.getSource() == jcbYear2 || e.getSource() == jcbMonth2)
{
String year = (String)jcbYear2.getSelectedItem();
String month = (String)jcbMonth2.getSelectedItem();
updateDay(year,month,jcbDay2);
}
}
class RadioListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (jrbSingle.isSelected())
{
jlFirstArrive.setText(" ");
jpFirstArriveBox.removeAll();
jpFirstArriveBox.add(jpReplaceArrive);
jpFirstArriveBox.repaint();
jlArrive.setText(" 到达城市:");
jlTime1.setText(" 出发日期:");
jlTime2.setText(" ");
jpTime2Box.removeAll();
jpTime2Box.add(jpReplaceTime);
jpTime2Box.repaint();
}
else if(jrbDouble.isSelected())
{
jlFirstArrive.setText(" ");
jpFirstArriveBox.removeAll();
jpFirstArriveBox.add(jpReplaceArrive);
jpFirstArriveBox.repaint();
jlArrive.setText(" 到达城市:");
jlTime1.setText(" 出发日期:");
jlTime2.setText(" 返程日期:");
jpTime2Box.removeAll();
jpTime2Box.add(jpTime2);
jpTime2Box.repaint();
}
else if (jrbMutiple.isSelected())
{
jlFirstArrive.setText("第一到达城市:");
jpFirstArriveBox.removeAll();
jpFirstArriveBox.add(jpArrive1);
jpFirstArriveBox.repaint();
jlArrive.setText("第二到达城市:");
jlTime1.setText("第一出发日期:");
jlTime2.setText("第二出发日期:");
jpTime2Box.removeAll();
jpTime2Box.add(jpTime2);
jpTime2Box.repaint();
}
}
}
public void actionPerformed(ActionEvent e)
{
if (jrbSingle.isSelected())
{
start = (String)jcbStart.getSelectedItem();
start = start.trim();
arrive = (String)jcbArrive.getSelectedItem();
arrive = arrive.trim();
leaveYear = (String)jcbYear1.getSelectedItem();
leaveMonth = (String)jcbMonth1.getSelectedItem();
leaveDay = (String)jcbDay1.getSelectedItem();
if (!isTimeValid(leaveYear,leaveMonth,leaveDay))
{
JOptionPane.showMessageDialog(null,"已经过了出发时间,请重新设定并查询",
"错误信息",JOptionPane.ERROR_MESSAGE);
return;
}
leaveWeek = timeToWeek(leaveYear,leaveMonth,leaveDay);
airFirm = (String)jcbAirFirm.getSelectedItem(); //想要的航空公司
airFirm = airFirm.trim();
executeSingleQuery();
}
else if(jrbDouble.isSelected())
{
start = (String)jcbStart.getSelectedItem();
start = start.trim();
arrive = (String)jcbArrive.getSelectedItem();
arrive = arrive.trim();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -