📄 dingpiao.java
字号:
//订票系统的开发与应用
//作者:田舒
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class dingpiao
extends JFrame implements ListSelectionListener, ActionListener
{
//声明JPanel对象,用来布局三个列表框和标签
JPanel p1;
//出发地标签
JLabel jLabe1;
//目的地标签
JLabel jLabe2;
//车次标签
JLabel jLabe3;
//出发地列表框
JList start;
//目的地列表框
JList end;
//车次列表框
JList checi;
//出发地列表值
String[] a = {
"成都", "大理", "丽江", "楚雄", "文山", "贵阳"
};
//目的地列表值
String[] b = {
"成都", "大理", "丽江", "楚雄", "文山", "贵阳", "武汉", "北京","郑州"
};
//车次列表值
String[] c = {
"A186", "T86", "Z11", "Z13", "K23", "T261", "T262", "Z108", "T82","T61"
};
//声明JPanel对象,用来布局客户姓名,联系电话两个文本框
JPanel p2;
//客户姓名文本框
JTextField jName;
//客户联系电话文本框
JTextField jPhone;
//姓名标签
JLabel jLabe4;
//电话标签
JLabel jLabe5;
//声明JPanel对象,用来布局是否送票单选按钮
JPanel p3;
//送票标签
JLabel jLabe6;
//是否送票单选按钮
JRadioButton jRbtn1;
JRadioButton jRbtn2;
//声明JPanel对象,用来布局座位类型单选按钮
JPanel p4;
//座位类型标签
JLabel jLabe7;
//座位类型单选按钮
JRadioButton jRbtn3;
JRadioButton jRbtn4;
JRadioButton jRbtn5;
ButtonGroup group;
//声明JPanel对象,用来布局按钮
JPanel p5;
//确定和取消按钮
JButton jBtn1;
JButton jBtn2;
//用于存放输出结果的字符串
String startS;
String endS;
String checiS;
String ifsend;
String style;
//输出结果框架
JFrame frame;
//多行文本
JTextArea text;
//构造函数
dingpiao()
{
super("昆明市定票系统");
//设置窗口关闭时进行退出程序的操作
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
getContentPane().setLayout(new GridLayout(4, 1));
// 创建一个JPanel对象,用于布局三个列表框和标签
p1 = new JPanel();
p1.setLayout(new GridLayout(3, 1));
//出发地标签
jLabe1 = new JLabel("请选择出发地:");
p1.add(jLabe1);
//初始化出发地列表框
start = new JList(a);
start.setVisibleRowCount(1);
start.addListSelectionListener(this);
//声明JScrollPane,用于容纳JList
JScrollPane listPane1 = new JScrollPane(start);
p1.add(listPane1);
//目的地标签
jLabe2 = new JLabel("请选择目的地:");
p1.add(jLabe2);
//初始目的地列表框
end = new JList(b);
end.setVisibleRowCount(1);
start.addListSelectionListener(this);
//声明JScrollPane,用于容纳JList
JScrollPane listPane2 = new JScrollPane(end);
p1.add(listPane2);
//初始化车次列表框
checi = new JList(c);
checi.setVisibleRowCount(1);
checi.addListSelectionListener(this);
jLabe3 = new JLabel("请选择车次:");
p1.add(jLabe3);
JScrollPane listPane3 = new JScrollPane(checi);
p1.add(listPane3);
getContentPane().add(p1);
//创建Panel p2,用于放置客户姓名,联系电话文本框
p2 = new JPanel();
p2.setLayout(new GridLayout(2, 1));
jLabe4 = new JLabel("客户姓名:");
p2.add(jLabe4);
//创建宽为5的单行文本框
jName = new JTextField(5);
p2.add(jName);
jLabe5 = new JLabel("客户联系电话:");
p2.add(jLabe5);
//创建宽为5的单行文本框
jPhone = new JTextField(5);
p2.add(jPhone);
getContentPane().add(p2);
//创建Panel p3,用于放置单选按钮
p3 = new JPanel();
//设置p3的布局方式为
p3.setLayout(new FlowLayout());
jLabe5 = new JLabel("请选择是否需要送票:");
p3.add(jLabe5);
jRbtn1 = new JRadioButton("需要送票");
jRbtn1.setActionCommand("sendTo");
jRbtn1.addActionListener(this);
jRbtn2 = new JRadioButton("不需要送票");
jRbtn2.setActionCommand("nosend");
jRbtn2.addActionListener(this);
group = new ButtonGroup();
group.add(jRbtn1);
group.add(jRbtn2);
p3.add(jRbtn2);
p3.add(jRbtn1);
getContentPane().add(p3);
//创建Panel p4,用于放置单选按钮
p4=new JPanel();
//设置p4的布局方式为
p4.setLayout(new FlowLayout());
jLabe6=new JLabel("请选择座位类型:");
p4.add(jLabe6);
jRbtn3=new JRadioButton("硬座");
jRbtn3.setActionCommand("yingzuo");
jRbtn3.addActionListener(this);
jRbtn4=new JRadioButton("硬卧");
jRbtn4.setActionCommand("yingwo");
jRbtn4.addActionListener(this);
jRbtn5=new JRadioButton("软卧");
jRbtn5.setActionCommand("ruanwo");
jRbtn5.addActionListener(this);
group = new ButtonGroup();
group.add(jRbtn3);
group.add(jRbtn4);
group.add(jRbtn5);
p4.add(jRbtn3);
p4.add(jRbtn4);
p4.add(jRbtn5);
getContentPane().add(p4);
//创建 Panel p5,用于放置按钮
p5 = new JPanel();
p5.setLayout(new FlowLayout(FlowLayout.LEFT, 100, 50));
jBtn1 = new JButton("确定");
jBtn2 = new JButton("取消");
//输出结果界面
frame = new JFrame();
frame.setVisible(false);
text = new JTextArea();
frame.getContentPane().add(text);
//使用适配器注册监听器
jBtn1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
//将结果显示在另一个空口中
frame.setVisible(true);
text.setText("客户订票信息如下:" + "\n" + "出发地:" + startS + "\n" +
"目的地:" + endS + "\n" + "车次:" + checiS + "\n" +
"客户姓名:" + jName.getText() + "\n" + "客户电话:" +
jPhone.getText() + "\n" + ifsend +"\n" +"座位类型"+ style);
frame.pack(); }
});
jBtn2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.exit(0);
}
});
p5.add(jBtn1);
p5.add(jBtn2);
getContentPane().add(p5);
this.pack();
}
//实现ListSelectionListener接口的方法
public void valueChanged(ListSelectionEvent e)
{
if(!e.getValueIsAdjusting())
{
if(!start.isSelectionEmpty()&&!end.isSelectionEmpty()&&!checi.isSelectionEmpty())
{
startS=start.getSelectedValue().toString();
endS=end.getSelectedValue().toString();
checiS=checi.getSelectedValue().toString();
}
}
}
//实现ActionListener接口方法
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("sendTo"))
ifsend="客户需要送票";
if(e.getActionCommand().equals("nosend"))
ifsend="客户不需要送票";
if(e.getActionCommand().equals("yingzuo"))
style="硬座";
if(e.getActionCommand().equals("yingwo"))
style="硬卧";
if(e.getActionCommand().equals("ruanwo"))
style="软卧";
}
public static void main(String[]args)
{
new dingpiao();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -