📄 orderpanel.java
字号:
package airline;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class orderPanel extends JPanel
{
public orderPanel()
{
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit()
throws Exception
{
this.setLayout(null);
jLabel1.setText("出发城市");
jLabel1.setBounds(new Rectangle(56, 42, 65, 29));
jComboBox1.setBounds(new Rectangle(121, 42, 165, 29));
jLabel2.setText("到达城市");
jLabel2.setBounds(new Rectangle(359, 42, 65, 29));
jComboBox2.setBounds(new Rectangle(425, 42, 115, 29));
jLabel3.setText("出发日期");
jLabel3.setBounds(new Rectangle(56, 119, 65, 29));
jComboBox3.setBounds(new Rectangle(122, 119, 57, 29));
jComboBox4.setBounds(new Rectangle(192, 119, 39, 29));
jComboBox5.setBounds(new Rectangle(247, 119, 39, 29));
jComboBox3.addItemListener(listener);
jComboBox4.addItemListener(listener);
jLabel4.setText("购票数量");
jLabel4.setBounds(new Rectangle(359, 119, 65, 29));
jLabel5.setText("姓名");
jLabel5.setBounds(new Rectangle(56, 182, 64, 29));
jTextField2.setBounds(new Rectangle(120, 182, 166, 29));
jLabel6.setText("性别");
jLabel6.setBounds(new Rectangle(359, 182, 65, 29));
jLabel7.setText("身份证");
jLabel7.setBounds(new Rectangle(56, 252, 64, 29));
jRadioButton1.setIcon(null);
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(425, 182, 55, 29));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(479, 182, 61, 29));
jLabel8.setText("3张以上价格更优惠(包括3张)");
jLabel8.setBounds(new Rectangle(359, 252, 181, 29));
jButton1.setBounds(new Rectangle(188, 314, 81, 31));
jButton1.setText("提交");
jButton1.addActionListener(listener1);
jButton2.setBounds(new Rectangle(366, 314, 81, 31));
jButton2.setText("重填");
jButton2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}
});
jTextField1.setBounds(new Rectangle(423, 119, 117, 29));
jTextField3.setBounds(new Rectangle(120, 252, 165, 28));
jLabel9.setForeground(Color.red);
jLabel9.setBounds(new Rectangle(423, 154, 117, 27));
jLabel10.setForeground(Color.red);
jLabel10.setBounds(new Rectangle(119, 218, 165, 27));
jLabel11.setForeground(Color.red);
jLabel11.setBounds(new Rectangle(119, 286, 165, 27));
group.add(jRadioButton1);
group.add(jRadioButton2);
this.add(jLabel3);
this.add(jComboBox3);
this.add(jComboBox4);
this.add(jComboBox5);
this.add(jComboBox2);
this.add(jLabel2);
this.add(jLabel1);
this.add(jComboBox1);
this.add(jLabel4);
this.add(jLabel5);
this.add(jTextField2);
this.add(jLabel6);
this.add(jLabel7);
this.add(jRadioButton1);
this.add(jRadioButton2);
this.add(jLabel8);
this.add(jButton1);
this.add(jButton2);
this.add(jTextField1);
this.add(jTextField3);
this.add(jLabel9);
this.add(jLabel10);
this.add(jLabel11);
}
String[] years=new String[]{"2007","2008","2009"};
String[] months=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12"};
String[] city=new String[]{"北京","重庆","长沙","上海"};
JLabel jLabel1 = new JLabel();
JComboBox jComboBox1 = new JComboBox(city);
JLabel jLabel2 = new JLabel();
JComboBox jComboBox2 = new JComboBox(city);
JLabel jLabel3 = new JLabel();
JComboBox jComboBox3 = new JComboBox(years);
JComboBox jComboBox4 = new JComboBox(months);
JComboBox jComboBox5 = new JComboBox(getDay());
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
ButtonGroup group=new ButtonGroup() ;
JLabel jLabel8 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
//当年月变化是每月天数发生变化
ItemListener listener=new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
int length=Math.abs(jComboBox5.getItemCount()-getDay().length);
if(jComboBox5.getItemCount()>getDay().length)
{
for(int i=0;i<length;i++)
jComboBox5.removeItemAt(jComboBox5.getItemCount()-1);
}
else if(jComboBox5.getItemCount()<getDay().length)
{
for(int i=0;i<length;i++)
jComboBox5.addItem(jComboBox5.getItemCount()+1);
}
}
};
ActionListener listener1=new ActionListener()
{
String start;
String destination;
String time;
int qty;
String name;
int sex;
String IdCard;
public void actionPerformed(ActionEvent e)
{
//票数不能为空,不能为非数字
if(jTextField1.getText().trim().equals(""))
{
jLabel9.setText("票数为数字(小于50)");
return;
}
else{
jLabel9.setText("");
try{
qty=Integer.parseInt(jTextField1.getText());
}
catch(Exception ex){
jLabel9.setText("票数为数字(小于50)");
return;
}
}
//姓名不能为空
if(jTextField2.getText().trim().equals(""))
{
jLabel10.setText("姓名不能为空");
return;
}
else{
jLabel10.setText("");
name=jTextField2.getText();
}
//身份证为18位
if(jTextField3.getText().length()!=18)
{
jLabel11.setText("身份证为18位");
return;
}
else{
jLabel10.setText("");
IdCard=jTextField3.getText();
}
//男:sex=1;女:sex=2;
if(jRadioButton1.isSelected())
sex=1;
else sex=2;
//得到出发城市和到达城市
start=(String)jComboBox1.getSelectedItem();
destination=(String)jComboBox2.getSelectedItem();
if(start.equals(destination))
{
JOptionPane.showMessageDialog(null,"出发城市与到达城市不能相同");
return;
}
//年月日
int year=Integer.parseInt((String)jComboBox3.getSelectedItem());
int month=Integer.parseInt((String)jComboBox4.getSelectedItem());
int day=Integer.parseInt((String)jComboBox5.getSelectedItem());
Calendar date=Calendar.getInstance();
int yearNow=date.get(Calendar.YEAR);
int monthNow=date.get(Calendar.MONTH);
int dayNow=date.get(Calendar.DAY_OF_MONTH);
int days=(year-yearNow)*365+(month-monthNow-1)*30+day-dayNow+1;
if(days<45&&days>3)
{
time=year+"-"+month+"-"+day;
}
else
{
JOptionPane.showMessageDialog(null,"只能订45天以内且3天后的机票");
return;
}
String message="2,"+start+","+destination+","+time+","+qty
+","+name+","+sex+","+IdCard;
Client client=new Client(message);
ArrayList aList=client.list;
System.out.println(aList);
if(aList.isEmpty()||aList.equals(null))
JOptionPane.showMessageDialog(null,"没有符合你要求的航班") ;
else if(aList.get(0).equals("fail"))
JOptionPane.showMessageDialog(null,"求检查你提交的数据是否合法,确定后再试") ;
else if(aList.get(0).equals("success"))
{
int price=0;
try{
price=Integer.parseInt(aList.get(2).toString());
}
catch(Exception ex){}
String success="恭喜你,订票成功!\n"+"此次订单号: "+aList.get(1)
+"\n金额: "+price*qty
+"\n航班: "+aList.get(3)
+"\n起飞时间: "+time+aList.get(4).toString().substring(10,16)
+"\n到达时间: "+time+aList.get(5).toString().substring(10,16)
+"\n请记住订单号以及你的个人信息,以便查询" ;
JOptionPane.showMessageDialog(null,success);
}
}
};
JLabel jLabel9 = new JLabel();
JLabel jLabel10 = new JLabel();
JLabel jLabel11 = new JLabel();
//计算包含相应年月天数
private String[] getDay()
{
int year = Integer.parseInt(
(String) jComboBox3.getItemAt(jComboBox3.getSelectedIndex()));
int month = Integer.parseInt(
(String) jComboBox4.getItemAt(jComboBox4.getSelectedIndex()));
if (month == 2) {
GregorianCalendar g = new GregorianCalendar();
if (g.isLeapYear(year)) {
String[] days = new String[29];
for (int i = 0; i < 29; i++)
days[i] = Integer.toString(i + 1);
return days;
}
else {
String[] days = new String[28];
for (int i = 0; i < 28; i++)
days[i] = Integer.toString(i + 1);
return days;
}
}
else if (month <= 7 && (month % 2 == 1)) {
String[] days = new String[31];
for (int i = 0; i < 31; i++)
days[i] = Integer.toString(i + 1);
return days;
}
else if (month <= 7 && (month % 2 == 0)) {
String[] days = new String[30];
for (int i = 0; i < 30; i++)
days[i] = Integer.toString(i + 1);
return days;
}
else if (month >= 8 && (month % 2 == 1)) {
String[] days = new String[30];
for (int i = 0; i < 30; i++)
days[i] = Integer.toString(i + 1);
return days;
}
else {
String[] days = new String[31];
for (int i = 0; i < 31; i++)
days[i] = Integer.toString(i + 1);
return days;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -