我最近在做一个项目——公交车售票机。这是一个帮助用户打印门票的程序。
我有一个屏幕 ( destPanel
),其中显示要选择的城市列表 ( JList departures
)。一旦用户选择了一个城市addListSelectionListener
,第一个JList
将保存并返回一个时间表(JList timetable
)供用户再次选择,这次保存选择的出发地以便稍后在车票上打印。
addListSelectionListener
显示时间表时,我无法停止。这是为了能够选择要保存的出发地以打印车票。
我添加了我的三个课程,展示了我一直在做什么。
第一类:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class TicketGUI extends JFrame
{
public static void main(String[] args)
{
new TicketGUI();
}
private JPanel destPanel = new JPanel();
private JPanel departPanel = new JPanel();
private JPanel payPanel = new JPanel();
private JButton[] buttons;
// Display size
private final int WIDTH = 310;
private final int HEIGHT = 150;
JList departJList;
JList defltList;
Vector<Enum> listContent = new Vector<Enum>();
//Following line used for adding info to display JList.
//Vector<Object> listContent = new Vector<Object>();
JList timeTable; //Not being used.
private DefaultListModel listModel; //Not being used.
Vector<String> newListContent = new Vector<String>(); //Not being used.
JTextArea textArea = new JTextArea();
JTextField payField = new JTextField(15);
public TicketGUI()
{
this.setTitle("Bus Ticket Machine");
setLayout(new BorderLayout());
JPanel agePanel = new JPanel();
JRadioButton adultButton = new JRadioButton("Adult");
adultButton.setSelected(true);
JRadioButton studentButton = new JRadioButton("Student");
JRadioButton childButton = new JRadioButton("Children");
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(adultButton);
group.add(studentButton);
group.add(childButton);
JPanel bPanel = new JPanel();
bPanel.setMaximumSize(new Dimension(100, 100));
bPanel.setLayout(new GridLayout(4, 3));
buttons= new JButton[12];
for (int i=0; i<12; i++)
{
buttons[i]=new JButton();
//buttons[i].addActionListener(this);
}
for (int i=1; i<10; i++)
{
buttons[i-1].setText(""+i);
bPanel.add(buttons[i-1]);
}
buttons[9].setText("X");
bPanel.add(buttons[9]);
buttons[10].setText("0");
bPanel.add(buttons[10]);
buttons[11].setText("OK");
bPanel.add(buttons[11]);
payField.setText("Card Number");
payField.setEditable(false);
payPanel.setLayout(new BorderLayout());
payPanel.add(payField, BorderLayout.NORTH);
// Display
// listContent.add("London");
// listContent.add("Bristol");
// listContent.add("Sheffield");
// listContent.add("Birmingham");
// listContent.add("...");
// listContent.add("Here under list to be displayed after choosing a city");
// listContent.add("1. London -> Bristol Depart: 11:45 Arrives: 13:15");
// listContent.add("2. London -> Bristol Depart: 12:30 Arrives: 15:00");
// listContent.add("3. London -> Bristol Depart: 13:45 Arrives: 16:15");
// listContent.add("4. London -> Bristol Depart: 14:30 Arrives: 17:00");
// listContent.add("...");
// listContent.add("Your Journey");
// listContent.add("Ticket: Adult");
// listContent.add("From: London ");
// listContent.add("Going to: Bristol");
// listContent.add("Leaving: 11:45");
// listContent.add("Fare: £ 18.00");
listContent = new Vector<Enum>(Arrays.asList(EnumCity.values()));
System.out.print(listContent + "one");
// defltList = new JList(listContent);
//
// System.out.print(defltList);
departJList = new JList(listContent);
// departJList.add(listContent);
System.out.print("\n\n");
System.out.print(departJList + "two");
timeTable = new JList();
departJList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting())
{
TimeTable selected = new TimeTable();
selected.setTimeTable();
newListContent.addAll(selected.getTimeTable());
// timeTable.setListData(newListContent.toArray());
// departJList.setListData(newListContent);
}
System.out.println(newListContent);
// timeTable.setListData(newListContent.toArray());
departJList.setListData(newListContent);
// departJList.setListData(newListContent);
}
});
JScrollPane m_clScrollpane = new JScrollPane(departJList);
m_clScrollpane.setPreferredSize(new Dimension(WIDTH, HEIGHT));
JPanel farePanel = new JPanel(new GridLayout(4, 1));
farePanel.add(adultButton);
farePanel.add(studentButton);
farePanel.add(childButton);
JTextField tf = new JTextField("£ 0.00");
tf.setEditable(false);
farePanel.add(tf);
destPanel.add(farePanel);
destPanel.add(m_clScrollpane);
payPanel.add(bPanel);
add(destPanel);
add(payPanel, BorderLayout.EAST);
setVisible(true);
pack();
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent evt) {
dispose();
System.exit(0);
}
});
}
}
第二类:
public enum EnumCity {
London,
Bristol,
Sheffield,
Birmingham
}
第三类:
import java.util.Vector;
public class TimeTable {
private String timeTable;
public TimeTable()
{
}
public String setTimeTable()
{
return this.timeTable;
}
public Vector<String> getTimeTable()
{
Vector<String> timeList = new Vector<String>();
timeList.addElement("1. London -> Bristol Depart: 11:45 Arrives: 13:15");
timeList.addElement("2. London -> Bristol Depart: 12:30 Arrives: 15:00");
timeList.addElement("3. London -> Bristol Depart: 13:45 Arrives: 16:15");
timeList.addElement("4. London -> Bristol Depart: 14:30 Arrives: 17:00");
return timeList;
}
}