火车票购票信息
- 用户登陆界面
2.下拉菜单界面
4.显示选择日期界面
5.确定按钮-显示购票信息界面
6.重置界面
详细见源码
package ting25; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.Jframe; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.*; import java.awt.*; import java.util.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; class Windows1 extends Jframe implements ActionListener{ JTextField longingname; JTextField field; JLabel dateshow;//显示选择日期到界面窗口 //获取日期显示在文本框 private JTextField dateField = new JTextField(); //---------------------------- // JTextField date_choose; //----------- String[] stations={"南宁","桂林","柳州","北海","百色"}; JComboBox beginStation,endStation; JButton selectDateButton,okButton,cancelButton; JLabel date; //dateChoose1 date1=new dateChoose1(); //JDateChooser gg = new JDateChooser(); //String dateShow=gg.getDateFormat("yyyy-MM-dd"); //获得日期方法 Windows1() { //----------------------日期 //--------------------- super(); Container c=getContentPane(); c.setLayout(new GridLayout(5,1)); JPanel p1,p2,p3,p4,p5,p6; p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); p4=new JPanel(); p5=new JPanel(); p6=new JPanel(); JLabel longing =new JLabel("乘车人姓名:"); p2.add(longing); longingname=new JTextField(10); p2.add(longingname); beginStation=new JComboBox(stations);//输入起点站 beginStation.setSelectedIndex(1); p4.add(new JLabel("起点车站:")); p4.add(beginStation); //为组合框添加监视器 endStation=new JComboBox(stations);//输入起点站 endStation.setSelectedIndex(1); p5.add(new JLabel("终点车站:")); p5.add(endStation); JButton chooseStartDateButton; JButton okButton; JButton replaceButton; chooseStartDateButton=new JButton("选择出发日期"); //出发日期监听器 chooseStartDateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JDateChooser chooseDate =new JDateChooser(); chooseDate.setBounds(400,400,400,400); chooseDate.setVisible(true); dateshow.setText(chooseDate.dateShow()); }}); //--------------------------------------------------- okButton=new JButton("确定"); replaceButton=new JButton("重置"); //---------------------------------------------------- //确定监听器 okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String nameStr=longingname.getText().trim(); String start_station=(String)(beginStation.getSelectedItem()); String end_station=(String)(endStation.getSelectedItem()); String chooseyear,choosemonth,choosedate; JDateChooser gg = new JDateChooser(); chooseyear=String.valueOf(gg.getYear()); choosemonth=String.valueOf(gg.getMonth()); choosedate=String.valueOf(gg.getDate()); // String dateShow=gg.getDateFormat("yyyy-MM-dd HH:mm:ss"); //String dateShow=getDate();//gg.dateShow(); String display1="乘车人:"+nameStr+"n"+"起点站:"+start_station+" 终点站:"+end_station+"n日期:"+chooseyear+"日"+choosemonth+"月"+choosedate+"日";//dateShow.getYear()+"日"+dateShow.getYear()+"月"+dateShow.getDay()+"日"; JOptionPane.showMessageDialog(null, display1); } }); //重置按钮监听器实现 replaceButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub longingname.setText(""); beginStation.getSelectedItem(); endStation.getSelectedItem(); dateshow.setText(""); } }); //---------------------------------------------------- JDateChooser gg = new JDateChooser(); p3.add(chooseStartDateButton); dateshow=new JLabel(); p3.add(dateshow); p1.add(okButton); p1.add(replaceButton); c.add(p2); c.add(p4); c.add(p5); c.add(p3); c.add(p1,BorderLayout.SOUTH); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } } //------日历------------- final class JDateChooser extends JDialog { // 定义相关参数 private int year = 0; private int month = 0; private int date = 0; //日期选择背景色 private Color selectColor = Color.blue; // 日期背景色 private Color dateColor = Color.white; // 日期鼠标进入背景色 private Color dateHoverColor = Color.white; // 日期标题背景色 private Color dateTitleColor = Color.white; // 日期标题字体颜色 private Color dateTitleFontColor = Color.black; //日期字体颜色 private Color dateFontColor = Color.black; //日期是否有效标志 private boolean flag = false; / public void setDate(int year, int month, int date) { if (year >= minYear && year <= maxYear) { this.year = year; } else { return; } if (month >= 1 && month <= 12) { this.month = month; } else { return; } if (date > 0 && date <= getDaysInMonth(year, month)) { this.date = date; } else { return; } } public boolean getFlag() { return flag; } // 构造方法 public JDateChooser() { initComponent(); initComponentData(); addComponent(); addListener(); setDialogAttribute(); } // 实例化组件 private void initComponent() { jbYearPre = new JButton(); jbYearNext = new JButton(); jbMonthPre = new JButton(); jbMonthNext = new JButton(); jcbYear = new JComboBox(); jcbMonth = new JComboBox (); jlDays = new JLabel[7][7]; jbChoose = new JButton(); jbToday = new JButton(); jbCancel = new JButton(); } //初始化组件数据 private void initComponentData() { jbYearPre.setText("←"); jbYearNext.setText("→"); jbMonthPre.setText("↑"); jbMonthNext.setText("↓"); Calendar calendar = Calendar.getInstance(); if (year != 0 && month != 0 && date != 0) { calendar.set(year, month - 1, date); } else { year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH) + 1; date = calendar.get(Calendar.DAY_OF_MONTH); } initYear(); jcbYear.setSelectedItem(year + "年"); for (int i = 1; i <= 12; i++) { jcbMonth.addItem(i + "月"); } jcbMonth.setSelectedItem(month + "月"); for (int i = 0; i < 7; i++) { JLabel temp = new JLabel(); temp.setHorizontalAlignment(JLabel.CENTER); temp.setVerticalAlignment(JLabel.CENTER); temp.setOpaque(true); temp.setBackground(dateTitleColor); temp.setForeground(dateTitleFontColor); jlDays[0][i] = temp; } for (int i = 1; i < 7; i++) { for (int j = 0; j < 7; j++) { JLabel temp = new JLabel(); temp.setHorizontalAlignment(JLabel.CENTER); temp.setVerticalAlignment(JLabel.CENTER); temp.setOpaque(true); temp.setForeground(dateFontColor); jlDays[i][j] = temp; } } String[] days = { "日", "一", "二", "三", "四", "五", "六" }; for (int i = 0; i < 7; i++) { jlDays[0][i].setText(days[i]); } jbChoose.setText("选择"); jbToday.setText("今日"); jbCancel.setText("取消"); changeDate(); } // 初始化显示年份范围 private void initYear() { jcbYear.removeAllItems(); for (int i = minYear; i <= maxYear; i++) { jcbYear.addItem(i + "年"); } } private void addComponent() { // 添加背部组件 JPanel north = new JPanel(); north.add(jbYearPre); north.add(jbMonthPre); north.add(jcbYear); north.add(jcbMonth); north.add(jbMonthNext); north.add(jbYearNext); //--------------- this.add(north, "North"); //---------------- // 添加中间组件 JPanel center = new JPanel(new GridLayout(7, 7)); for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { center.add(jlDays[i][j]); } } this.add(center); // 添加南部组件 JPanel jpSouth = new JPanel(); // jpSouth.add(startTime); //jpSouth.add(endTime); jpSouth.add(jbChoose); jpSouth.add(jbToday); jpSouth.add(jbCancel); this.add(jpSouth, "South"); } private int getDaysInMonth(int year, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if (isLeapYear(year)) { return 29; } return 28; default: return 0; } } //清空日期 private void clearDate() { for (int i = 1; i < 7; i++) { for (int j = 0; j < 7; j++) { jlDays[i][j].setText(""); } } } // 更改日期 private void changeDate() { refreshLabelColor(); clearDate(); Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, 1); int day_in_week = calendar.get(Calendar.DAY_OF_WEEK); int days = getDaysInMonth(year, month); if (date > days) { date = 1; } int temp = 0; for (int i = day_in_week - 1; i < 7; i++) { temp++; jlDays[1][i].setText(temp + ""); if (temp == date) { jlDays[1][i].setBackground(selectColor); } } for (int i = 2; i < 7; i++) { for (int j = 0; j < 7; j++) { temp++; if (temp > days) { return; } jlDays[i][j].setText(temp + ""); if (temp == date) { jlDays[i][j].setBackground(selectColor); } } } } private void addListener() { LabelMouseListener labelMouseListener = new LabelMouseListener(); for (int i = 1; i < 7; i++) { for (int j = 0; j < 7; j++) { jlDays[i][j].addMouseListener(labelMouseListener); } } ButtonActionListener buttonActionListener = new ButtonActionListener(); jbYearPre.addActionListener(buttonActionListener); jbYearNext.addActionListener(buttonActionListener); jbMonthPre.addActionListener(buttonActionListener); jbMonthNext.addActionListener(buttonActionListener); jbChoose.addActionListener(buttonActionListener); jbToday.addActionListener(buttonActionListener); jbCancel.addActionListener(buttonActionListener); ComboBoxItemListener comboBoxItemListener = new ComboBoxItemListener(); jcbYear.addItemListener(comboBoxItemListener); jcbMonth.addItemListener(comboBoxItemListener); } private int parseYearOrMonth(String yearOrMonth) { return Integer.parseInt(yearOrMonth.substring(0, yearOrMonth.length() - 1)); } private boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } private void setDialogAttribute() { this.setModal(true); this.setTitle("选择出发日期时间"); } private void refreshLabelColor() { for (int i = 1; i < 7; i++) { for (int j = 0; j < 7; j++) { jlDays[i][j].setBackground(dateColor); } } } public int getMinYear() { return minYear; } public int getMaxYear() { return maxYear; } public void setMinAndMaxYear(int minYear, int maxYear) { if (minYear > maxYear || minYear < 1900 || maxYear > 9999) { return; } this.minYear = minYear; this.maxYear = maxYear; initYear(); } public Color getSelectColor() { return selectColor; } public void setSelectColor(Color selectColor) { this.selectColor = selectColor; } //获得日期背景颜色 public Color getDateColor() { return dateColor; } //设置日期背景颜色 public void setDateColor(Color dateColor) { this.dateColor = dateColor; } //获得日期鼠标进入背景颜色 public Color getDetaHoverColor() { return dateHoverColor; } // 设置日期鼠标进入背景颜色 public void setDateHoverColor(Color dateHoverColor) { this.dateHoverColor = dateHoverColor; } // 获得日期标题背景颜色 public Color getDateTitleColor() { return dateTitleColor; } // 设置日期标题背景颜色 public void setDateTitleColor(Color dateTitleColor) { this.dateTitleColor = dateTitleColor; } //获得日期标题字体颜色 public Color getDateTitleFontColor() { return dateTitleFontColor; } // 设置日期标题字体颜色 public void setDateTitleFontColor(Color dateTitleFontColor) { this.dateTitleFontColor = dateTitleFontColor; } // 获得日期字体颜色 public Color getDateFontColor() { return dateFontColor; } // 设置日期字体颜色 public void setDateFontColor(Color dateFontColor) { this.dateFontColor = dateFontColor; } //获得选择年份 public int getYear() { return year; } // 获得选中月份 public int getMonth() { return month; } // 获得选中天为当月第几天 public int getDate() { return date; } // 获得选中天为一周中第几天 public int getDayOfWeek() { return getCalendar().get(Calendar.DAY_OF_WEEK); } //获得选中天为一年中第几天 public int getDayOfYear() { return getCalendar().get(Calendar.DAY_OF_YEAR); } // 获得日期对象 public Date getDateObject() { return getCalendar().getTime(); } //获得以指定规则格式化的日期字符串 public String getDateFormat(String format) { return new SimpleDateFormat(format).format(getDateObject()); } //获得Calendar对象 private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, date); return calendar; } // 标签鼠标监听 final class LabelMouseListener extends MouseAdapter { @Override public void mouseClicked(MouseEvent e) { JLabel temp = (JLabel) e.getSource(); if (!temp.getText().equals("")) { int date = Integer.parseInt(temp.getText()); { if (date != JDateChooser.this.date) { JDateChooser.this.date = date; refreshLabelColor(); temp.setBackground(selectColor); } } } } @Override public void mouseEntered(MouseEvent e) { JLabel temp = (JLabel) e.getSource(); if (!temp.getText().equals("")) { temp.setBackground(dateHoverColor); } } @Override public void mouseExited(MouseEvent e) { JLabel temp = (JLabel) e.getSource(); if (!temp.getText().equals("")) { if (Integer.parseInt(temp.getText()) != date) { temp.setBackground(dateColor); } else { temp.setBackground(selectColor); } } } } // 按钮动作监听 final class ButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == jbYearPre) { int select = jcbYear.getSelectedIndex(); if (select > 0) { jcbYear.setSelectedIndex(select - 1); } } else if (e.getSource() == jbYearNext) { int select = jcbYear.getSelectedIndex(); if (select < jcbYear.getItemCount() - 1) { jcbYear.setSelectedIndex(select + 1); } } else if (e.getSource() == jbMonthPre) { int select = jcbMonth.getSelectedIndex(); if (select > 0) { jcbMonth.setSelectedIndex(select - 1); } } else if (e.getSource() == jbMonthNext) { int select = jcbMonth.getSelectedIndex(); if (select < jcbMonth.getItemCount() - 1) { jcbMonth.setSelectedIndex(select + 1); } } else if (e.getSource() == jbChoose) { flag = true; closeDateChooser(); } else if (e.getSource() == jbToday) { flag = true; Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH) + 1; date = calendar.get(Calendar.DATE); closeDateChooser(); } else if (e.getSource() == jbCancel) { flag = false; closeDateChooser(); } } } final class ComboBoxItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if (e.getSource() == jcbYear) { int year = parseYearOrMonth(jcbYear.getSelectedItem().toString()); if (year != JDateChooser.this.year) { JDateChooser.this.year = year; changeDate(); } } else if (e.getSource() == jcbMonth) { int month = parseYearOrMonth(jcbMonth.getSelectedItem().toString()); if (month != JDateChooser.this.month) { JDateChooser.this.month = month; changeDate(); } } } } } //---------------- public class Myapp { public static void main(String[] args) { // TODO Auto-generated method stub Windows1 myapp = new Windows1(); myapp.setTitle("购买火车票"); myapp.setBounds(200,200,400,400); myapp.setVisible(true); myapp.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); } } //2.方格游戏 界面: 源代码: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import org.ietf.jgss.GSSManager; import java.io.*; import java.text.BreakIterator; import java.util.*; import java.util.logging.Handler; public class Game extends Jframe{ JPanel mainPanel=new JPanel(); JButton button[][]=new JButton[4][4]; int data[][]=new int[4][4]; ArrayList num=new ArrayList (); public Game(){ super("16方格排序游戏"); Container c=this.getContentPane(); mainPanel.setLayout(new GridLayout(4, 4, 3, 3)); c.add(mainPanel); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ button[i][j]=new JButton(); mainPanel.add(button[i][j]); button[i][j].addActionListener(new Handler()); data[i][j]=0; } } InitNum(); } void InitNum(){ num.clear(); for(int i=0;i<16;i++){ num.add(String.valueOf(i)); } for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ Random r=new Random(); int index=r.nextInt(num.size()); String string=num.get(index); if(string.equals("0")){ button[i][j].setText(""); data[i][j]=0; }else{ button[i][j].setText(string); data[i][j]=Integer.parseInt(string); } num.remove(index); } } } class Handler implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().length()==0) return; int n=Integer.parseInt(e.getActionCommand()); int locationx = 0,locationy = 0; int location = 0; int aim=-1;//记录空白按钮位置 for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(data[i][j]==n){ locationx=i; locationy=j; location=i*4+j; } } } int cand[]={location-1,location+1,location-4,location+4}; for(int i=0;i<4;i++){ if(check(cand[i])){ aim=cand[i]; } } if(aim>=0&&aim<16){ int temp=data[locationx][locationy]; button[aim/4][aim%4].setText(String.valueOf(temp)); data[aim/4][aim%4]=temp; button[locationx][locationy].setText(""); data[locationx][locationy]=0; } if(isOver()){ int choice = JOptionPane.showConfirmDialog(null,JOptionPane.YES_NO_OPTION); if(choice == JOptionPane.YES_OPTION) InitNum(); else System.exit(0); } } boolean check(int index){ if(index>=0 && index<16 && data[(index)/4][(index)%4]==0) return true; return false; } boolean isOver(){ int i,j; for(i=0;i<16;i++){ if(data[i/4][i%4]!=i) break; } if(i==16) return true; for(i=0;i<15;i++){ if(data[i/4][i%4]!=i+1) break; } if(i==15) return true; for(i=0,j=15;i<16;i++,j--){ if(data[j/4][j%4]!=i) break; } if(i==16) return true; for(i=0,j=15;i<15;i++,j--){ if(data[j/4][j%4]!=i+1) break; } if(i==15) return true; return false; } } public static void main(String[] args){ Game game=new Game(); game.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); game.setSize(300,300); game.setVisible(true); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)