当前位置: 代码迷 >> Java相关 >> 简单的订票系统,有些不知道怎么完善,帮忙看下
  详细解决方案

简单的订票系统,有些不知道怎么完善,帮忙看下

热度:462   发布时间:2010-04-29 14:31:41.0
简单的订票系统,有些不知道怎么完善,帮忙看下
要有出发地目的地,顶票的张数,
添加单选按钮,让用户可选择单程或者往返
添加单行文本框,让用户可以输入购买张数
添加多行文本框,让用户可以输入意见
添加两个按钮, 分别为确认和退出

单选按钮:JRadioButton
复选框:JCheckBox
单行文本框:JTextField
多行文本框:JTextArea

下面是我写的一小部分,下面不知道该怎么完善,各位帮忙修改下..

import javax.swing.*;

class testGUI extends JFrame{
    JPanel jp1;
    JLabel start;
    JList startList;
    JButton jb1;
    JButton jb2;
   
    public testGUI(){
        super("订票演示");
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);   
        
        jp1 = new JPanel();
        
        //组件放入容器jp1
        start = new JLabel("出发地:");
        jp1.add(start);
        
        String[] str = {"深圳","北京","江西","湖南","重庆"};
        startList = new JList(str);
        jp1.add(startList);
        
        //容器加入到框架中
        getContentPane().add(jp1);
        pack();
    }
   
    public static void main(String[] args){
        testGUI tg = new testGUI();   
    }
}
搜索更多相关的解决方案: 订票  系统  

----------------解决方案--------------------------------------------------------
花了20分钟写了下,现在swing程序写的都很乱,希望我写的可以帮你一下,感觉写的还算规范,公司基本都是这样要求的。


package test;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;

public class TestGui extends JFrame {

    private JLabel msgLbl;
    private JLabel startLbl;
    private JLabel endLbl;
    private JLabel countLbl;
    //始发站
    private JList startList;
    private JList endList;
    private DefaultListModel stationListModel;
    private JRadioButton singeRdo;
    private JRadioButton doubleRdo;
    private JButton okBtn;;
    private JButton exitBtn;
    private JSpinner countSpinner;
    private JTextArea suggestionTxt;

    public TestGui() {
        super("订票演示");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        initStationListModel();
        initGUI();
        addListeners();
        setResizable(false);
        centerWindow(this);
    }

    private void initGUI() {
        
        msgLbl = new JLabel("填写订票信息:");
        //中间的主体内容
        JPanel mainContentPnl = new JPanel();
        mainContentPnl.setLayout(new GridBagLayout());
        mainContentPnl.setBorder(BorderFactory.createEtchedBorder());
        
        JPanel stationPnl = new JPanel();
        stationPnl.setLayout(new FlowLayout(5));
        startLbl = new JLabel("出发地:");
        startList = new JList(stationListModel);
        startList.setSelectedIndex(0);
        startList.setPreferredSize(new Dimension(60, 80));
        endLbl = new JLabel("目的地:");
        endList = new JList(stationListModel);
        endList.setSelectedIndex(0);
        endList.setPreferredSize(new Dimension(60, 80));
        stationPnl.add(startLbl);
        stationPnl.add(startList);
        stationPnl.add(endLbl);
        stationPnl.add(endList);
        
        
        JPanel typePnl = new JPanel();
        typePnl.setLayout(new GridBagLayout());
        ButtonGroup type = new ButtonGroup();
        singeRdo = new JRadioButton("单程");
        singeRdo.setSelected(true);
        doubleRdo = new JRadioButton("往返");
        countSpinner = new JSpinner(new SpinnerNumberModel());
        countLbl = new JLabel("张数:");
        type.add(singeRdo);
        type.add(doubleRdo);
        typePnl.add(singeRdo, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
        typePnl.add(doubleRdo, new GridBagConstraints(1,0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
        typePnl.add(countLbl, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        typePnl.add(countSpinner, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        
        suggestionTxt = new JTextArea(3, 64);
        JScrollPane suggestionPane = new JScrollPane(suggestionTxt);
        
        mainContentPnl.add(stationPnl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
        mainContentPnl.add(typePnl, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
        mainContentPnl.add(suggestionPane, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        
        //按钮放置面板
        JPanel buttonPnl = new JPanel();
        buttonPnl.setLayout(new FlowLayout());
        okBtn = new JButton("确定");
        okBtn.setActionCommand("ok");
        exitBtn = new JButton("退出");
        exitBtn.setActionCommand("exit");
        buttonPnl.add(okBtn);
        buttonPnl.add(exitBtn);

        // 容器加入到框架中
        Container contentPane = getContentPane();
        contentPane.setLayout(new GridBagLayout());
        contentPane.add(msgLbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(10, 10, 0, 10), 0, 0));
        contentPane.add(mainContentPnl, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.BOTH, new Insets(5, 10, 0, 10), 0, 0));
        contentPane.add(buttonPnl, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints.EAST,
                GridBagConstraints.NONE, new Insets(5, 10, 10, 10), 0, 0));
        
        pack();

    }
   
    private void addListeners()
    {
        ActionListener btnListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                String cmd = event.getActionCommand();
                if ("ok".equals(cmd))
                {
                    String typeInfo = singeRdo.isSelected() ? "单程" : "往返";
                    StringBuffer msg = new StringBuffer();
                    msg.append("订了从");
                    msg.append(startList.getSelectedValue().toString()).append("到");
                    msg.append(endList.getSelectedValue().toString());
                    msg.append(typeInfo).append("票").append(countSpinner.getValue().toString()).append("张。");
                    msg.append("".equals(suggestionTxt.getText().trim()) ? "我没有意见。" :
                        "我的意见是:" + suggestionTxt.getText().trim() + "。");
                    JOptionPane.showMessageDialog(TestGui.this, msg.toString());
                }
                else if ("exit".equals(cmd))
                {
                    System.exit(0);
                }
            }
        };
        okBtn.addActionListener(btnListener);
        exitBtn.addActionListener(btnListener);
    }
   
    /**
     * 初始化站点信息
     */
    private void initStationListModel()
    {
        String[] str = { "深圳", "北京", "江西", "湖南", "重庆" };
        stationListModel = new DefaultListModel();
        for (int i = 0; i < str.length; i++) {
            stationListModel.addElement(str[i]);
        }
    }
   
    private void centerWindow(Window w){
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = w.getSize();
        int x = (screenSize.width - windowSize.width) / 2;
        int y = (screenSize.height - windowSize.height) / 2;
        
        w.setLocation(new Point(x, y));
    }

    public static void main(String[] args) {
        TestGui tg = new TestGui();
        tg.setVisible(true);
    }
}

----------------解决方案--------------------------------------------------------
回复 2楼 baifenghan
写得很复杂,很多知识点我都没有学到
有点看不清楚
不过你写得很规范!
值得我学习
谢谢你!
----------------解决方案--------------------------------------------------------
以下是引用baifenghan在2010-4-29 20:18:33的发言:

花了20分钟写了下,现在swing程序写的都很乱,希望我写的可以帮你一下,感觉写的还算规范,公司基本都是这样要求的。


package test;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;

public class TestGui extends JFrame {

    private JLabel msgLbl;
    private JLabel startLbl;
    private JLabel endLbl;
    private JLabel countLbl;
    //始发站
    private JList startList;
    private JList endList;
    private DefaultListModel stationListModel;
    private JRadioButton singeRdo;
    private JRadioButton doubleRdo;
    private JButton okBtn;;
    private JButton exitBtn;
    private JSpinner countSpinner;
    private JTextArea suggestionTxt;

    public TestGui() {
        super("订票演示");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        initStationListModel();
        initGUI();
        addListeners();
        setResizable(false);
        centerWindow(this);
    }

    private void initGUI() {
        
        msgLbl = new JLabel("填写订票信息:");
        //中间的主体内容
        JPanel mainContentPnl = new JPanel();
        mainContentPnl.setLayout(new GridBagLayout());
        mainContentPnl.setBorder(BorderFactory.createEtchedBorder());
        
        JPanel stationPnl = new JPanel();
        stationPnl.setLayout(new FlowLayout(5));
        startLbl = new JLabel("出发地:");
        startList = new JList(stationListModel);
        startList.setSelectedIndex(0);
        startList.setPreferredSize(new Dimension(60, 80));
        endLbl = new JLabel("目的地:");
        endList = new JList(stationListModel);
        endList.setSelectedIndex(0);
        endList.setPreferredSize(new Dimension(60, 80));
        stationPnl.add(startLbl);
        stationPnl.add(startList);
        stationPnl.add(endLbl);
        stationPnl.add(endList);
        
        
        JPanel typePnl = new JPanel();
        typePnl.setLayout(new GridBagLayout());
        ButtonGroup type = new ButtonGroup();
        singeRdo = new JRadioButton("单程");
        singeRdo.setSelected(true);
        doubleRdo = new JRadioButton("往返");
        countSpinner = new JSpinner(new SpinnerNumberModel());
        countLbl = new JLabel("张数:");
        type.add(singeRdo);
        type.add(doubleRdo);
        typePnl.add(singeRdo, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
        typePnl.add(doubleRdo, new GridBagConstraints(1,0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
        typePnl.add(countLbl, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        typePnl.add(countSpinner, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        
        suggestionTxt = new JTextArea(3, 64);
        JScrollPane suggestionPane = new JScrollPane(suggestionTxt);
        
        mainContentPnl.add(stationPnl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
        mainContentPnl.add(typePnl, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
        mainContentPnl.add(suggestionPane, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        
        //按钮放置面板
        JPanel buttonPnl = new JPanel();
        buttonPnl.setLayout(new FlowLayout());
        okBtn = new JButton("确定");
        okBtn.setActionCommand("ok");
        exitBtn = new JButton("退出");
        exitBtn.setActionCommand("exit");
        buttonPnl.add(okBtn);
        buttonPnl.add(exitBtn);

        // 容器加入到框架中
        Container contentPane = getContentPane();
        contentPane.setLayout(new GridBagLayout());
        contentPane.add(msgLbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.NONE, new Insets(10, 10, 0, 10), 0, 0));
        contentPane.add(mainContentPnl, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                GridBagConstraints.BOTH, new Insets(5, 10, 0, 10), 0, 0));
        contentPane.add(buttonPnl, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints.EAST,
                GridBagConstraints.NONE, new Insets(5, 10, 10, 10), 0, 0));
        
        pack();

    }
   
    private void addListeners()
    {
        ActionListener btnListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                String cmd = event.getActionCommand();
                if ("ok".equals(cmd))
                {
                    String typeInfo = singeRdo.isSelected() ? "单程" : "往返";
                    StringBuffer msg = new StringBuffer();
                    msg.append("订了从");
                    msg.append(startList.getSelectedValue().toString()).append("到");
                    msg.append(endList.getSelectedValue().toString());
                    msg.append(typeInfo).append("票").append(countSpinner.getValue().toString()).append("张。");
                    msg.append("".equals(suggestionTxt.getText().trim()) ? "我没有意见。" :
                        "我的意见是:" + suggestionTxt.getText().trim() + "。");
                    JOptionPane.showMessageDialog(TestGui.this, msg.toString());
                }
                else if ("exit".equals(cmd))
                {
                    System.exit(0);
                }
            }
        };
        okBtn.addActionListener(btnListener);
        exitBtn.addActionListener(btnListener);
    }
   
    /**
     * 初始化站点信息
     */
    private void initStationListModel()
    {
        String[] str = { "深圳", "北京", "江西", "湖南", "重庆" };
        stationListModel = new DefaultListModel();
        for (int i = 0; i < str.length; i++) {
            stationListModel.addElement(str);
        }
    }
   
    private void centerWindow(Window w){
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = w.getSize();
        int x = (screenSize.width - windowSize.width) / 2;
        int y = (screenSize.height - windowSize.height) / 2;
        
        w.setLocation(new Point(x, y));
    }

    public static void main(String[] args) {
        TestGui tg = new TestGui();
        tg.setVisible(true);
    }
}
经鉴定 可以订负数张票 - -!
----------------解决方案--------------------------------------------------------
      
       SpinnerNumberModel spinnerNumberModel = new SpinnerNumberModel();
        spinnerNumberModel.setValue(1);
        spinnerNumberModel.setMinimum(1);
        
        JPanel typePnl = new JPanel();
        typePnl.setLayout(new GridBagLayout());
        ButtonGroup type = new ButtonGroup();
        singeRdo = new JRadioButton("单程");
        singeRdo.setSelected(true);
        doubleRdo = new JRadioButton("往返");
        countSpinner = new JSpinner(spinnerNumberModel);
        countLbl = new JLabel("张数:");

简单修改下就OK乐
----------------解决方案--------------------------------------------------------
回复 5楼 dadongzicool
不错,确实有这个问题。
----------------解决方案--------------------------------------------------------
  相关解决方案