/* * invoiceFrame.java * * Created on 2009年6月10日, 上午9:59 */ package invoice; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.dnd.DropTarget; import java.awt.Graphics; import javax.swing.BorderFactory; /** * * @author john */ public class invoiceFrame extends javax.swing.JFrame { /** Creates new form invoiceFrame */ public invoiceFrame() { initComponents(); } public Calender2 frame2; private int jt=0; /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Invoice Reports"); setBackground(new java.awt.Color(153, 102, 255)); setBounds(new java.awt.Rectangle(200, 200, 400, 200)); setResizable(false); addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { formMouseClicked(evt); } }); addComponentListener(new java.awt.event.ComponentAdapter() { public void componentMoved(java.awt.event.ComponentEvent evt) { formComponentMoved(evt); } }); jLabel1.setText("FromDate:"); jTextField1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTextField1MouseClicked(evt); } }); jLabel2.setText("ToDate:"); jTextField2.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTextField2MouseClicked(evt); } }); jButton3.setText("Submit"); jButton3.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton3MouseClicked(evt); } }); jButton4.setText("Close"); jButton4.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton4MouseClicked(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jLabel2)) .addGap(28, 28, 28) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField2) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addGap(70, 70, 70) .addComponent(jButton3) .addGap(45, 45, 45) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(87, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(49, 49, 49) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3) .addComponent(jButton4)) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked // TODO 将在此处添加您的处理代码: //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.dispose(); }//GEN-LAST:event_jButton4MouseClicked private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked // TODO 将在此处添加您的处理代码: if(sta==1||sta==2){ popup.hide(); sta=0; } }//GEN-LAST:event_formMouseClicked private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved // TODO 将在此处添加您的处理代码: if(sta==1||sta==2){ popup.hide(); sta=0; } }//GEN-LAST:event_formComponentMoved private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MouseClicked // TODO 将在此处添加您的处理代码: try { if(sta==0){ frame2=new Calender2 (jTextField1); frame2.setVisible(true); frame2.setSize(250,250); // frame2.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.GRAY) ); frame2.setBorder(BorderFactory.createTitledBorder("")); Point p=jTextField1.getLocationOnScreen(); PopupFactory factory = PopupFactory.getSharedInstance(); popup= factory.getPopup(jTextField1, frame2, p.x+10,p.y+18); popup.show(); frame2.setPop(popup); sta=1; jt=1; }else if(sta==2) {popup.hide(); frame2=new Calender2 (jTextField1); frame2.setVisible(true); frame2.setSize(250,250); frame2.setBorder(BorderFactory.createTitledBorder("")); Point p=jTextField1.getLocationOnScreen(); PopupFactory factory = PopupFactory.getSharedInstance(); popup= factory.getPopup(jTextField1, frame2, p.x+10,p.y+18); popup.show(); frame2.setPop(popup); sta=1; } } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_jTextField1MouseClicked private void jTextField2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField2MouseClicked // TODO 将在此处添加您的处理代码: try { if(sta==0){ frame2=new Calender2 (jTextField2); frame2.setVisible(true); frame2.setSize(250,250); frame2.setBorder(BorderFactory.createTitledBorder("")); Point p=jTextField2.getLocationOnScreen(); PopupFactory factory = PopupFactory.getSharedInstance(); popup= factory.getPopup(jTextField2, frame2,p.x+10,p.y+18); popup.show(); frame2.setPop(popup); sta=2; jt=2; }else if(sta==1) {popup.hide(); frame2=new Calender2 (jTextField2); frame2.setVisible(true); frame2.setSize(250,250); frame2.setBorder(BorderFactory.createTitledBorder("")); Point p=jTextField2.getLocationOnScreen(); PopupFactory factory = PopupFactory.getSharedInstance(); popup= factory.getPopup(jTextField2, frame2,p.x+10,p.y+18); popup.show(); frame2.setPop(popup); sta=2; } } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_jTextField2MouseClicked private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked // TODO 将在此处添加您的处理代码: try{ if(sta==1||sta==2){ popup.hide(); sta=0; } String frm=jTextField1.getText().toString(); String to=jTextField2.getText().toString(); if(frm==null||frm.equals("")||to==null||to.equals("")){ JOptionPane.showMessageDialog(null, "时间不能为空!", "wrong", JOptionPane.INFORMATION_MESSAGE ); }else{ //Main.fromDate=frm; //Main.toDate=to; // Main m = new Main(); // SeparationTime st=new SeparationTime(); // st.SeqTime(frm,to); System.out.println("suc!"); //SeqTime st=new SeqTime(); // st.SeqT(frm,to); JOptionPane.showMessageDialog(null, "Done.", "Message", JOptionPane.INFORMATION_MESSAGE ); } }catch(Exception e){ e.printStackTrace(); } }//GEN-LAST:event_jButton3MouseClicked /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new invoiceFrame().setVisible(true); } }); } // 变量声明 - 不进行修改//GEN-BEGIN:variables private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; // 变量声明结束//GEN-END:variables private Popup popup; public static int sta=0; }
package invoice; import java.awt.Color; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Date; import java.util.GregorianCalendar; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLayeredPane; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; public class Calender2 extends JPanel implements ActionListener,ItemListener { /** * 实现简单的日历功能 * * @author Jadie version 1.0 2007/12/29 * @author2 john update 20090617 */ private Popup popup; private JTextField jTextField2; public static String Returndate=""; private static final long serialVersionUID = 1L; private Date date = new Date(); private GregorianCalendar gregorianCalendar = new GregorianCalendar(); private String[] stringWeek = new String[] { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; private String[] stringWeekCn = new String[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; private String[] stringMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" }; private String[] strSysTime = new String[6]; // 存储当前日期信息 private String[] strSysNowTime = new String[6]; // 存储运行时日期信息 private JButton[] buttonDay = new JButton[42]; private JButton[] buttonWeek = new JButton[7]; private JLabel labelMonth = new JLabel(); private JButton buttonToday = new JButton(); private JButton buttonLastMonth = new JButton(); private JButton buttonNextMonth = new JButton(); private JComboBox comboYear = new JComboBox(); private JComboBox comboMonth = new JComboBox(); public Calender2(JTextField jTextField2) { //(frame,modal); //super("万年历"); super();//new FlowLayout(FlowLayout.LEFT,2,2) this.jTextField2=jTextField2; // setLayout(new GridLayout(8, 7, 3, 8)); setLayout(new GridLayout(8, 7, 3, 5)); setBounds(0, 0, 430, 260); //this.setBackground(Color.); //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); comboYear.setForeground(new Color(0, 0, 255)); comboYear.setFont(new Font("", Font.PLAIN, 8)); //comboYear.setMargin(new Insets(0,0,0,0)); for (int y = 1900; y < 2101; y++) { comboYear.addItem("" + new Integer(y).toString()); } //getContentPane(). add(comboYear); comboYear.addItemListener(this); final JLabel labelYear = new JLabel(); labelYear.setForeground(Color.BLUE); labelYear.setFont(new Font("新宋体", Font.PLAIN, 8)); //getContentPane(). add(labelYear); labelYear.setText(" 年"); //labelYear.setToolTipText("xxxxx"); comboMonth.setForeground(new Color(0, 0, 255)); comboMonth.setFont(new Font("", Font.PLAIN, 8)); for (int m = 1; m < 13; m++) { comboMonth.addItem("" + new Integer(m).toString()); } //getContentPane(). add(comboMonth); comboMonth.addItemListener(this); //getContentPane(). add(labelMonth); labelMonth.setForeground(Color.BLUE); labelMonth.setFont(new Font("新宋体", Font.PLAIN, 8)); labelMonth.setText(" 月"); //getContentPane(). add(buttonLastMonth); buttonLastMonth.setForeground(Color.BLUE); buttonLastMonth.setFont(new Font("新宋体", Font.PLAIN, 8)); buttonLastMonth.setText("上月"); buttonLastMonth.setMargin(new Insets(0,0,0,0)); buttonLastMonth.addActionListener(this); //getContentPane(). add(buttonToday); buttonToday.setForeground(Color.BLUE); buttonToday.setFont(new Font("新宋体", Font.PLAIN, 8)); buttonToday.setText("今天"); buttonToday.setMargin(new Insets(0,0,0,0)); buttonToday.addActionListener(this); //getContentPane(). add(buttonNextMonth); buttonNextMonth.setForeground(Color.BLUE); buttonNextMonth.setFont(new Font("新宋体", Font.PLAIN, 8)); buttonNextMonth.setText("下月"); buttonNextMonth.setMargin(new Insets(0,0,0,0)); buttonNextMonth.addActionListener(this); for (int i = 0; i < 7; i++) { buttonWeek[i] = new JButton(); //JButton xx=buttonWeek[i]; //xx.setSize(10,10); //buttonWeek[i].setMargin(new Insets(0,0,0,0)); if (i == 0 || i == 6) { buttonWeek[i].setForeground(Color.RED); } else { buttonWeek[i].setForeground(Color.BLUE); } buttonWeek[i].setFont(new Font("新宋体", Font.PLAIN, 8)); buttonWeek[i].setText(stringWeekCn[i]); buttonWeek[i].setMargin(new Insets(0,0,0,0)); //getContentPane(). add(buttonWeek[i]); } for (int i = 0; i < 42; i++) { buttonDay[i] = new JButton(); buttonDay[i].setText(""); buttonDay[i].setMargin(new Insets(0,0,0,0)); buttonDay[i].setFont(new Font("新宋体", Font.PLAIN, 9)); buttonDay[i].addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { jButtonMousePressed(evt); } }); // getContentPane(). add(buttonDay[i]); } //this.setResizable(false); getSysNowTimeInfo(); setNowDate(); setNowDate(); } public void setPop(Popup popup){ this.popup=popup; } private void jButtonMousePressed(java.awt.event.MouseEvent evt){ //System.out.println(evt.paramString()); JButton o = (JButton)evt.getSource(); String str=o.getText(); String y= comboYear.getSelectedItem().toString(); String m=comboMonth.getSelectedItem().toString(); Returndate=y.trim()+"-"+m.trim()+"-"+str.trim(); this.jTextField2.setText(Returndate); this.popup.hide(); invoiceFrame.sta=0; } public void setSysDate(int year, int month) { // 将日期设置为year年month月1日 gregorianCalendar.set(year, month, 1); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == buttonToday) { setNowDate(); setNowDate(); } else if (ae.getSource() == buttonLastMonth) { setDate(-1); } else { setDate(1); } } public void itemStateChanged(ItemEvent arg0) { setDate(0); } public void getSysNowTimeInfo() { // 得到程序运行时的时间信息并存储在字符串数组strSysNowTime中 date = gregorianCalendar.getTime(); strSysNowTime = (date + "").split(" "); } public void getSysTimeInfo() { // 得到系统当前的时间信息并存储在字符串数组strSysTime中 date = gregorianCalendar.getTime(); strSysTime = (date + "").split(" "); } public int getNowMonth() { int month = 0; for (int i = 0; i < 12; i++) { if (strSysNowTime[1].equalsIgnoreCase(stringMonth[i])) { month = i; break; } } return month; } public int weekStrat(String strWeek) { // 返回字符串strWeek与星期中的第几天匹配,SUN为第一天 int strat = 0; for (int i = 0; i < 7; i++) { if (strWeek.equalsIgnoreCase(stringWeek[i])) { strat = i; break; } } return strat; } public void setNowDate() { // 将时间设置为程序运行时的时间 setSysTime(getNowYear(), getNowMonth()); getSysTimeInfo(); setDayNull(); getDay(getMonthDays(getNowYear(), getNowMonth() - 1), getMonthDays( getNowYear(), getNowMonth()), weekStrat(strSysTime[0]), getNowDay()); comboYear.setSelectedIndex(getNowYear() - 1900); comboMonth.setSelectedIndex(getNowMonth()); } public void setDate(int move) { // 将时间设置为选中的年月增加move个月之后的时间 setSysTime(getYear(), getMonth() + move); getSysTimeInfo(); setDayNull(); getDay(getMonthDays(getYear(), getMonth() + move - 1), getMonthDays( getYear(), getMonth() + move), weekStrat(strSysTime[0]), -1); if (move != 0) { if (getMonth() == 0 && move < 0) { move = 11; comboYear.setSelectedIndex(getYear() - 1901); } else if (getMonth() == 11 && move > 0) { move = -11; comboYear.setSelectedIndex(getYear() - 1899); } else { comboYear.setSelectedIndex(getYear() - 1900); } comboMonth.setSelectedIndex(getMonth() + move); } } public void setSysTime(int year, int month) { gregorianCalendar.set(year, month, 1); } public int getNowYear() { return Integer.parseInt(strSysNowTime[5]); } public int getNowDay() { return Integer.parseInt(strSysNowTime[2]); } public int getYear() { return comboYear.getSelectedIndex() + 1900; } public int getMonth() { return comboMonth.getSelectedIndex(); } public void setDayNull() { for (int d = 0; d < 42; d++) { buttonDay[d].setText(""); } } public void getDay(int lastMonDays, int monthDays, int startWeek, int day) { // 设置日期颜色并打印 for (int d = 0; d < startWeek + 1; d++) { buttonDay[d].setForeground(Color.GRAY); buttonDay[d].setVisible(false); //buttonDay[d].setEnabled(false); //buttonDay[d].setBackground(Color.GRAY); buttonDay[d].setText((lastMonDays - startWeek) + d + 1 + ""); } for (int d = startWeek; d < startWeek + monthDays; d++) { if ((d - startWeek + 1) == day) { buttonDay[d].setForeground(Color.blue); buttonDay[d].setVisible(true); //buttonDay[d].setEnabled(true); } else if (d % 7 == 0 || d % 7 == 6) { buttonDay[d].setForeground(Color.RED); buttonDay[d].setVisible(true); } else { buttonDay[d].setForeground(Color.BLACK); //buttonDay[d].setBackground(Color.BLACK); buttonDay[d].setVisible(true); //buttonDay[d].setEnabled(true); } buttonDay[d].setText(d - startWeek + 1 + ""); } for (int d = monthDays + startWeek; d < 42; d++) { buttonDay[d].setForeground(Color.GRAY); buttonDay[d].setVisible(false); //buttonDay[d].setEnabled(false); buttonDay[d].setText(d - (monthDays + startWeek) + 1 + ""); } } public int getMonthDays(int year, int month) { // 返回year年month月的天数 switch (month) { case 3: case 5: case 8: case 10: return 30; case 1: if (gregorianCalendar.isLeapYear(year)) { return 29; } else { return 28; } default: return 31; } } }
日历控件时 down的别人的 但是 这个经过了我的加工 使得在点击text的情况下跳出日历