📄 example.java
字号:
/*****************************************/
/*程序名:DengLu.java */
/*功 能:接受用户名和密码实现登陆功能 */
/*日 期:2008-02-28 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
public class Example extends JFrame implements ListSelectionListener,ActionListener{
private ButtonGroup buttonGroup = new ButtonGroup();
//客户电话文本框
private JTextField jPhone;
//客户姓名文本框
private JTextField jName;
//出发地列表值
String[] entries={" ","北京","上海","天津","大连","西藏","郑州","广州"};
//目的地列表值
String[] entrie={" ","南京","西安","商丘","南阳","西藏","郑州","深圳"};
//目的地列表框
private JList endStation;
//出发地列表框
private JList startStation;
//列车列表值
String[] train={" ","T23","Z12","T78","56","K12","Z11","K67","D123"};
//列车列表框
private JList trainNo;
//用于存放输出结果的字符串
String startS;
String endS;
String trainN;
String ifsend;
//输出结果框
JFrame frame;
JPanel p5;
//多行文本
JTextArea text;
//保存按钮
JButton jBtn3;
//返回按钮
JButton jBtn4;
JOptionPane jOptionPane;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Example frame = new Example();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
//构造函数
public Example() {
super("订票系统");
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
//设置窗口关闭时进行退出程序操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(Color.BLUE);
setJMenuBar(menuBar);
final JMenu menu = new JMenu();
menu.setFont(new Font("", Font.BOLD, 14));
menu.setText("文件");
menuBar.add(menu);
final JMenuItem menuItem = new JMenuItem();
menuItem.setFont(new Font("", Font.PLAIN, 14));
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DengLu frame=new DengLu();
frame.setVisible(true);
}
});
menuItem.setText("登陆");
menu.add(menuItem);
final JMenuItem menuItem_2 = new JMenuItem();
//设置订票事件处理
menuItem_2.setFont(new Font("", Font.PLAIN, 14));
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(true);
text.setText("客户订票信息如下:"+"\n"+"出发地:"+startS+"\n"+"目的地:"+endS+"\n"+
"车次:"+trainN+"\n"+"客户姓名:"+jName.getText()+"\n"+"客户电话:"+
jPhone.getText()+"\n"+ifsend);
frame.pack();
}
});
menuItem_2.setText("订票");
menu.add(menuItem_2);
final JMenuItem menuItem_1 = new JMenuItem();
//设置打开事件处理
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(true);
text.setText("客户订票信息如下:"+"\n"+"出发地:"+startS+"\n"+"目的地:"+endS+"\n"+
"车次:"+trainN+"\n"+"客户姓名:"+jName.getText()+"\n"+"客户电话:"+
jPhone.getText()+"\n"+ifsend);
frame.pack();
}
});
menuItem_1.setFont(new Font("", Font.PLAIN, 14));
menuItem_1.setText("打开");
menu.add(menuItem_1);
final JMenuItem menuItem_4 = new JMenuItem();
//设置保存事件处理
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String inStr="客户订票信息如下:"+"\n"+"出发地:"+startS+"\n"+"目的地:"+endS+"\n"+
"车次:"+trainN+"\n"+"客户姓名:"+jName.getText()+"\n"+"客户电话:"+
jPhone.getText()+"\n"+ifsend;
try{
FileWriter fw=new FileWriter("order.txt");
BufferedWriter bw=new BufferedWriter(fw);
jOptionPane.showMessageDialog(frame,"文件保存成功");
bw.write(inStr);
bw.close();
}
catch(FileNotFoundException f)
{
f.printStackTrace();
}
catch(IOException el)
{
el.printStackTrace();
}
}
});
menuItem_4.setFont(new Font("", Font.PLAIN, 14));
menuItem_4.setText("保存");
menu.add(menuItem_4);
final JMenuItem menuItem_3 = new JMenuItem();
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
menuItem_3.setFont(new Font("", Font.PLAIN, 14));
menuItem_3.setText("退出");
menu.add(menuItem_3);
final JPanel p1 = new JPanel();
p1.setBackground(Color.MAGENTA);
p1.setForeground(Color.WHITE);
p1.setLayout(null);
p1.setBounds(0, 0, 492, 227);
getContentPane().add(p1);
//出发地标签
final JLabel jLabel = new JLabel();
jLabel.setFont(new Font("", Font.BOLD, 14));
jLabel.setBounds(10, 20, 144, 27);
jLabel.setText("请选择出发地:");
p1.add(jLabel);
//目的地标签
final JLabel jLabel_1 = new JLabel();
jLabel_1.setFont(new Font("", Font.BOLD, 14));
jLabel_1.setText("请选择目的地:");
jLabel_1.setBounds(10, 53, 144, 27);
p1.add(jLabel_1);
//车次标签
final JLabel jLabel_2 = new JLabel();
jLabel_2.setFont(new Font("", Font.BOLD, 14));
jLabel_2.setText("请 选择 车 次:");
jLabel_2.setBounds(10, 86, 144, 33);
p1.add(jLabel_2);
//客户姓名标签
final JLabel jLabel4 = new JLabel();
jLabel4.setFont(new Font("", Font.BOLD, 14));
jLabel4.setText("客 户 姓 名:");
jLabel4.setBounds(10, 145, 144, 27);
p1.add(jLabel4);
//客户电话标签
final JLabel jLabel5 = new JLabel();
jLabel5.setFont(new Font("", Font.BOLD, 14));
jLabel5.setText("客户联系电话:");
jLabel5.setBounds(10, 190, 144, 27);
p1.add(jLabel5);
this.jName = new JTextField();
this.jName.setBounds(274, 139, 159, 33);
p1.add(this.jName);
this.jPhone = new JTextField();
this.jPhone.setBounds(274, 184, 159, 33);
p1.add(this.jPhone);
final JScrollPane scrollPane = new JScrollPane(startStation);
scrollPane.setBounds(274, 20, 159, 21);
p1.add(scrollPane);
this.startStation = new JList();
//初始化出发地列表框
startStation=new JList(entries);
startStation.setVisibleRowCount(1);
startStation.addListSelectionListener(this);
scrollPane.setViewportView(this.startStation);
final JScrollPane scrollPane_1 = new JScrollPane(endStation);
scrollPane_1.setBounds(274, 53, 159, 21);
p1.add(scrollPane_1);
this.endStation = new JList();
//初始化目的地列表框
endStation=new JList(entrie);
endStation.setVisibleRowCount(1);
endStation.addListSelectionListener(this);
scrollPane_1.setViewportView(this.endStation);
final JScrollPane scrollPane_2 = new JScrollPane(trainNo);
scrollPane_2.setBounds(274, 92, 159, 21);
p1.add(scrollPane_2);
this.trainNo = new JList();
//初始化车次列表框
trainNo=new JList(train);
trainNo.setVisibleRowCount(1);
trainNo.addListSelectionListener(this);
scrollPane_2.setViewportView(this.trainNo);
final JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setLayout(null);
p2.setBounds(0, 229, 492, 38);
getContentPane().add(p2);
final JLabel label = new JLabel();
label.setFont(new Font("", Font.BOLD, 15));
label.setBounds(10, 10, 146, 18);
label.setText("是否需要订票:");
p2.add(label);
final JRadioButton jRbtn1 = new JRadioButton();
jRbtn1.setActionCommand("sendTo");
jRbtn1.addActionListener(this);
jRbtn1.setForeground(Color.BLUE);
jRbtn1.setFont(new Font("", Font.BOLD, 14));
buttonGroup.add(jRbtn1);
jRbtn1.setText("需要订票");
jRbtn1.setBounds(166, 8, 85, 23);
p2.add(jRbtn1);
final JRadioButton jRbtn2 = new JRadioButton();
jRbtn2.setActionCommand("noSend");
jRbtn2.addActionListener(this);
jRbtn2.setForeground(Color.BLUE);
jRbtn2.setFont(new Font("", Font.BOLD, 14));
buttonGroup.add(jRbtn2);
jRbtn2.setText("不需要订票");
jRbtn2.setBounds(307, 8, 101, 23);
p2.add(jRbtn2);
final JPanel p3 = new JPanel();
p3.setBackground(Color.YELLOW);
p3.setLayout(null);
p3.setBounds(0, 269, 492, 52);
getContentPane().add(p3);
final JButton jBtn1 = new JButton();
//使用适配器注册监听器
jBtn1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
//将结果显示在另一个窗口中
frame.setVisible(true);
text.setText("客户订票信息如下:"+"\n"+"出发地:"+startS+"\n"+"目的地:"+endS+"\n"+
"车次:"+trainN+"\n"+"客户姓名:"+jName.getText()+"\n"+"客户电话:"+
jPhone.getText()+"\n"+ifsend);
frame.pack();
}
});
jBtn1.setFont(new Font("", Font.BOLD, 14));
jBtn1.setBounds(97, 10, 93, 32);
jBtn1.setText("确 定");
p3.add(jBtn1);
final JButton jBtn2 = new JButton();
jBtn2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});
jBtn2.setFont(new Font("", Font.BOLD, 14));
jBtn2.setText("取 消");
jBtn2.setBounds(285, 10, 93, 32);
p3.add(jBtn2);
//输出结果界面
frame=new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.setVisible(false);
p5=new JPanel();
jOptionPane=new JOptionPane();
text=new JTextArea();
jBtn3=new JButton("保存");
jBtn4=new JButton("返回");
frame.getContentPane().add(text,BorderLayout.NORTH);
frame.getContentPane().add(p5,BorderLayout.SOUTH);
p5.add(jBtn3);
p5.add(jBtn4);
//保存按钮事件处理
jBtn3.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String inStr="客户订票信息如下:"+"\n"+"出发地:"+startS+"\n"+"目的地:"+endS+"\n"+
"车次:"+trainN+"\n"+"客户姓名:"+jName.getText()+"\n"+"客户电话:"+
jPhone.getText()+"\n"+ifsend;
try{
FileWriter fw=new FileWriter("order.txt");
BufferedWriter bw=new BufferedWriter(fw);
jOptionPane.showMessageDialog(frame,"文件保存成功");
bw.write(inStr);
bw.close();
}
catch(FileNotFoundException f)
{
f.printStackTrace();
}
catch(IOException el)
{
el.printStackTrace();
}
}
});
//返回按钮事件处理
jBtn4.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
frame.setVisible(false);
}
});
}
//实现ListSelectionListener接口
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
if(!startStation.isSelectionEmpty()&&!endStation.isSelectionEmpty()&&!trainNo.isSelectionEmpty())
{
startS=startStation.getSelectedValue().toString();
endS=endStation.getSelectedValue().toString();
trainN=trainNo.getSelectedValue().toString();
}
}
//实现ActionListener接口方法
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("sendTo"))
ifsend="客户需要送票";
if(e.getActionCommand().equals("noSend"))
ifsend="客户不需要送票";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -