java Swing 時間選擇器控制元件
效果圖:
中的 flowlayout_v.jar 檔案
示例程式碼:
HongYeLingGuDate類
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import com.changda.fingerservice.javaframe.VFlowLayout;
import hong.yelinggu.date.absinterface.SelectHYDateAbstract;
public class HongYeLingGuDate {
private final JFrame frTime = new JFrame("請選擇日期時間");
private JPanel jPtimeWeek, jPtimeDay, year_form, ybJPanel, month_form, mbJPanel;
private JButton btn_year_close, btn_month_close, btn_year_left, btn_year_right, btn_yes, btn_closed;
private Box box;
private JComboBox<String> jtf_H = null, jtf_m = null, jtf_s = null;
private SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private List<JButton> btnList = new ArrayList<>();
private JButton yearStart, monthEnd;
private String SelectNow_day = "01";
private int yearPage = 1;// 年份的頁碼
private final int yearGAP = 5;// 首頁的年差
private final int PAGE_MAIN = 0;
private final int PAGE_YEAR = 1;
private final int PAGE_MONTH = 2;
private Calendar instance = Calendar.getInstance();
private int now_year = 0;
private int now_month = 0;
private int now_day = 0;
private int now_hous = 0;
private int now_min = 0;
private int now_ss = 0;
private SelectHYDateAbstract HdateInterface;
String returnDateFormat = null;
/**
* 例項化控制元件
*
* @param returnDateFormat
* 返回的時間格式
*/
public HongYeLingGuDate(String returnFormat) {
// TODO Auto-generated constructor stub
returnDateFormat = returnFormat;
}
/**
* 建立時間拾取器
*/
public void creatDatePicker(SelectHYDateAbstract dateInterface) {
// TODO Auto-generated method stub
//判斷如果時間控制元件是顯示可見的就不執行了,防止多次執行
if (frTime.isVisible()){
return;
}
HdateInterface = dateInterface;
Date dateTime = new java.util.Date();
String StringTime = sdFormat.format(dateTime);
instance.setTime(dateTime);
String[] splDate = StringTime.split(" ");
String dateAssemble = splDate[0];
String timeAssemble = splDate[1];
String[] splitItemDate = dateAssemble.split("-");// 日期
String[] splitItemTime = timeAssemble.split(":");// 時間
now_year = Integer.parseInt(splitItemDate[0]);
now_month = Integer.parseInt(splitItemDate[1]);
now_day = Integer.parseInt(splitItemDate[2]);
now_hous = Integer.parseInt(splitItemTime[0]);
now_min = Integer.parseInt(splitItemTime[1]);
now_ss = Integer.parseInt(splitItemTime[2]);
frTime.getContentPane().setLayout(new VFlowLayout());
// 年,月,日選擇入口區
JPanel jPtimeTiele = new JPanel(new GridLayout(1, 3));
yearStart = new JButton(now_year + "年", new ImageIcon("./src/down.png"));
monthEnd = new JButton((now_month < 10 ? "0" + now_month : now_month) + "月", new ImageIcon("./src/down.png"));
yearStart.setFocusable(false);
yearStart.setBorderPainted(false);
yearStart.setBackground(new Color(0, 161, 203));
monthEnd.setFocusable(false);
monthEnd.setBorderPainted(false);
monthEnd.setBackground(new Color(0, 161, 203));
jPtimeTiele.add(yearStart);
JButton btn_null = new JButton("");
btn_null.setBorderPainted(false);
btn_null.setBackground(new Color(245, 245, 245));
btn_null.setFocusable(false);
jPtimeTiele.add(btn_null);
jPtimeTiele.add(monthEnd);
jPtimeTiele.setBackground(new Color(0, 161, 203));
// 週期 顯示區
jPtimeWeek = new JPanel(new GridLayout(1, 7));
jPtimeWeek.setBorder(new EmptyBorder(8, 0, 8, 0));
jPtimeWeek.setBackground(new Color(245, 245, 245));
String[] weekText = { "週日", "週一", "週二", "週三", "週四", "週五", "週六" };
for (int i = 0; i < weekText.length; i++) {
jPtimeWeek.add(new JLabel(weekText[i], SwingConstants.CENTER));
}
jPtimeDay = new JPanel(new GridLayout(6, 7));
jPtimeDay.setBackground(Color.WHITE);
// 日期選擇入口
UpdataDateList(null);
JPanel jPtimeTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
String[] hous = new String[24];
for (int i = 0; i < hous.length; i++) {
hous[i] = i < 10 ? "0" + i : String.valueOf(i);
}
jtf_H = new JComboBox<>(hous);
jtf_H.setSelectedIndex(now_hous);
jtf_H.setPreferredSize(new Dimension(50, 25));
String[] mins = new String[60];
String[] ss = new String[60];
for (int i = 0; i < mins.length; i++) {
mins[i] = i < 10 ? "0" + i : String.valueOf(i);
ss[i] = i < 10 ? "0" + i : String.valueOf(i);
}
jtf_m = new JComboBox<>(mins);
jtf_m.setSelectedIndex(now_min);
jtf_m.setPreferredSize(new Dimension(50, 25));
jtf_s = new JComboBox<>(ss);
jtf_s.setSelectedIndex(now_ss);
jtf_s.setPreferredSize(new Dimension(50, 25));
jPtimeTime.add(new JLabel("時"));
jPtimeTime.add(jtf_H);
jPtimeTime.add(new JLabel("分"));
jPtimeTime.add(jtf_m);
jPtimeTime.add(new JLabel("秒"));
jPtimeTime.add(jtf_s);
box = Box.createHorizontalBox();
box.add(jPtimeTime);
JPanel panel_r = new JPanel(new FlowLayout(FlowLayout.RIGHT));
btn_closed = new JButton("關閉");
btn_closed.setFocusable(false);
btn_closed.setBorderPainted(false);
btn_closed.setBackground(new Color(0, 161, 203));
btn_yes = new JButton("確認");
btn_yes.setBackground(new Color(0, 161, 203));
btn_yes.setFocusable(false);
btn_yes.setBorderPainted(false);
panel_r.add(btn_closed);
panel_r.add(btn_yes);
box.add(panel_r);
box.add(Box.createHorizontalGlue());
box.add(panel_r);
/*
* 關閉時間選擇器
*/
btn_closed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frTime.dispose();
}
});
/*
* 確定選擇的時間
*/
btn_yes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
// TODO Auto-generated method stub
StringBuffer sbfd = new StringBuffer();
sbfd.append(yearStart.getText()).append(monthEnd.getText()).append(SelectNow_day)
.append(jtf_H.getSelectedItem()).append(jtf_m.getSelectedItem())
.append(jtf_s.getSelectedItem());
String selectT = sbfd.toString().replace("年", "").replace("月", "");
if (HdateInterface != null) {
if (returnDateFormat != null) {
SimpleDateFormat nowFormat = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat format = new SimpleDateFormat(returnDateFormat);
Date parse = nowFormat.parse(selectT);
selectT = format.format(parse);
}
HdateInterface.clickOnSwingToTime(selectT);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frTime.dispose();
}
});
frTime.add(jPtimeTiele);
frTime.add(jPtimeWeek);
frTime.add(jPtimeDay);
frTime.add(box);
/*
* 年份的浮層
*/
year_form = new JPanel(new GridLayout(5, 4));
year_form.setBackground(Color.WHITE);
final List<JButton> list_btn = new ArrayList<>();
for (int i = now_year - yearGAP; i < now_year + 15; i++) {
JButton btn_y = new JButton(i + "年");
btn_y.setPreferredSize(new Dimension(45, 50));
btn_y.setBackground(Color.WHITE);
btn_y.setBorderPainted(false);
btn_y.setFocusable(false);
year_form.add(btn_y);
list_btn.add(btn_y);
btn_y.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton ye = (JButton) e.getSource();
for (JButton jButton : list_btn) {
jButton.setBackground(Color.WHITE);
}
ye.setBackground(new Color(0, 161, 203));
selectYearClick(ye.getText().trim());
}
});
}
/*
* 月份的浮層
*/
month_form = new JPanel(new GridLayout(3, 4));
month_form.setBackground(Color.WHITE);
final List<JButton> list_month_btn = new ArrayList<>();
for (int i = 1; i <= 12; i++) {
JButton btn_y = new JButton((i < 10 ? "0" + i : i) + "月");
btn_y.setPreferredSize(new Dimension(50, 80));
btn_y.setBackground(Color.WHITE);
btn_y.setBorderPainted(false);
btn_y.setFocusable(false);
month_form.add(btn_y);
list_month_btn.add(btn_y);
btn_y.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton ye = (JButton) e.getSource();
for (JButton jButton : list_month_btn) {
jButton.setBackground(Color.WHITE);
}
ye.setBackground(new Color(0, 161, 203));
monthEnd.setText(ye.getText().trim());
showhidle(PAGE_MAIN, true);
}
});
}
year_form.setVisible(false);
month_form.setVisible(false);
ybJPanel = new JPanel();
mbJPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
btn_year_close = new JButton("關閉");
btn_year_left = new JButton("上一頁");
btn_year_right = new JButton("下一頁");
btn_year_left.setPreferredSize(new Dimension(110, 30));
btn_year_right.setPreferredSize(new Dimension(110, 30));
btn_year_close.setPreferredSize(new Dimension(80, 30));
btn_month_close = new JButton("關閉");
ybJPanel.add(btn_year_left);
ybJPanel.add(btn_year_right);
JLabel jLabel_null = new JLabel("");
jLabel_null.setPreferredSize(new Dimension(93, 22));
ybJPanel.add(jLabel_null);
ybJPanel.add(btn_year_close);
mbJPanel.add(btn_month_close);
ybJPanel.setVisible(false);
mbJPanel.setVisible(false);
frTime.add(year_form);
frTime.add(month_form);
frTime.add(ybJPanel);
frTime.add(mbJPanel);
frTime.pack();
frTime.setSize(new Dimension(430, 370));
frTime.setLocationRelativeTo(null);
frTime.setResizable(false);
frTime.setVisible(true);
frTime.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);// 只關閉子視窗
/*
* 選擇年份
*/
yearStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
showhidle(PAGE_YEAR, true);
}
});
/*
* 年份上一頁
*/
btn_year_left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (yearPage > 1) {
year_form.removeAll();
int startYest;
int nowGap = now_year - yearGAP;
startYest = nowGap + ((yearPage - 2) * 20);
final List<JButton> list_btn = new ArrayList<>();
for (int i = startYest; i < startYest + 20; i++) {
JButton btn_y = new JButton(i + "年");
btn_y.setPreferredSize(new Dimension(45, 50));
btn_y.setBackground(Color.WHITE);
btn_y.setBorderPainted(false);
btn_y.setFocusable(false);
year_form.add(btn_y);
list_btn.add(btn_y);
btn_y.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton ye = (JButton) e.getSource();
for (JButton jButton : list_btn) {
jButton.setBackground(Color.WHITE);
}
ye.setBackground(new Color(0, 161, 203));
selectYearClick(ye.getText().trim());
}
});
year_form.updateUI();
}
yearPage--;
}
}
});
/*
* 年份下一頁
*/
btn_year_right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
year_form.removeAll();
int startYest;
int nowGap = now_year - yearGAP;
startYest = nowGap + (yearPage * 20);
final List<JButton> list_btn = new ArrayList<>();
for (int i = startYest; i < startYest + 20; i++) {
JButton btn_y = new JButton(i + "年");
btn_y.setPreferredSize(new Dimension(45, 50));
btn_y.setBackground(Color.WHITE);
btn_y.setBorderPainted(false);
btn_y.setFocusable(false);
year_form.add(btn_y);
list_btn.add(btn_y);
btn_y.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton ye = (JButton) e.getSource();
for (JButton jButton : list_btn) {
jButton.setBackground(Color.WHITE);
}
ye.setBackground(new Color(0, 161, 203));
selectYearClick(ye.getText().trim());
}
});
year_form.updateUI();
}
yearPage++;
}
});
/*
* 選擇月份
*/
monthEnd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
showhidle(PAGE_MONTH, true);
}
});
/**
* 年份單個選擇完畢
*/
btn_year_close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
showhidle(PAGE_MAIN, true);
}
});
/**
* 月份單個選擇完畢
*/
btn_month_close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
showhidle(PAGE_MAIN, true);
}
});
}
/*
*更新時間的天數
* @param data
* 時間格式: 年-月
*/
private void UpdataDateList(String data) {
try {
if (data != null) {
Date parse = sdFormat.parse(data);
instance.setTime(parse);
btnList.clear();
jPtimeDay.removeAll();
}
instance.set(Calendar.DAY_OF_MONTH, 1);
int week_mo = instance.get(Calendar.DAY_OF_WEEK);
int maximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i <= 42; i++) {
String btnNum = "";
int monthDay = 0;
if (i >= week_mo && i < maximum + week_mo) {
monthDay = i - week_mo + 1;
btnNum = String.valueOf(monthDay < 10 ? "0" + monthDay : monthDay);
}
JButton btnDay = new JButton(btnNum);
btnDay.setPreferredSize(new Dimension(30, 35));
btnDay.setBorderPainted(false);
if (now_day == monthDay) {
btnDay.setBackground(new Color(0, 161, 203));
btnDay.setForeground(Color.WHITE);
SelectNow_day = btnNum;
} else {
btnDay.setBackground(Color.WHITE);
}
btnDay.setFocusable(false);
btnDay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton source = (JButton) e.getSource();
SelectNow_day = source.getText().toString();
if (!source.getText().isEmpty()) {
for (JButton itembtn : btnList) {
itembtn.setBackground(Color.WHITE);
itembtn.setForeground(Color.BLACK);
}
source.setBackground(new Color(0, 161, 203));
source.setForeground(Color.WHITE);
}
}
});
btnList.add(btnDay);
jPtimeDay.add(btnDay);
}
if (data != null) {
jPtimeDay.updateUI();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 選擇的年份的事件
*/
private void selectYearClick(String year) {
yearStart.setText(year);
showhidle(PAGE_MAIN, true);
}
/**
* 顯示隱藏切換
*
* @param page
* 頁面
* @param showpage
* 是否顯示
*/
private void showhidle(int page, boolean showpage) {
switch (page) {
case PAGE_MAIN:// 主頁
jPtimeWeek.setVisible(showpage);
jPtimeDay.setVisible(showpage);
box.setVisible(showpage);
year_form.setVisible(!showpage);
ybJPanel.setVisible(!showpage);
String yeatText = yearStart.getText().trim().replace("年", "");
String monthText = monthEnd.getText().trim().replace("月", "");
UpdataDateList(yeatText + "-" + monthText + "-01 00:00:00");
break;
case PAGE_YEAR:
year_form.setVisible(showpage);
ybJPanel.setVisible(showpage);
jPtimeWeek.setVisible(!showpage);
jPtimeDay.setVisible(!showpage);
box.setVisible(!showpage);
month_form.setVisible(!showpage);
mbJPanel.setVisible(!showpage);
break;
case PAGE_MONTH:
jPtimeWeek.setVisible(!showpage);
jPtimeDay.setVisible(!showpage);
box.setVisible(!showpage);
year_form.setVisible(!showpage);
ybJPanel.setVisible(!showpage);
month_form.setVisible(showpage);
mbJPanel.setVisible(showpage);
default:
break;
}
}
}
抽象類 : SelectHYDateAbstract
public abstract interface SelectHYDateAbstract {
/**
* 選擇的時間的回電方法
* @param selectTime
*/
void clickOnSwingToTime(String selectTime);
}
類測試方法:
//返回的資料格式
private static String s = "yyyy-MM-dd HH:mm:ss";
public static void main(String[] args) {
HongYeLingGuDate guDate = new HongYeLingGuDate(s);
guDate.creatDatePicker(new SelectHYDateAbstract() {
@Override
public void clickOnSwingToTime(String time) {
// TODO Auto-generated method stub
System.out.println("你選擇的日期是="+time);
}
});
}
選擇後的測試結果:
你選擇的日期是=2017-06-30 10:43:19
相關推薦
java Swing 時間選擇器控制元件
效果圖: 中的 flowlayout_v.jar 檔案 示例程式碼: HongYeLingGuDate類 import java.awt.Color; import java.awt.Dimension; import java.awt.Flow
iOS學習十二之選擇器控制元件UIPickerView
UIPickerView是一個簡易的列表控制元件,用於提供有限個數的選項供使用者選擇。 它是通過代理和資料來源的方法對其進行設定和資料來源填充的,這種控制元件的設計模式也是代理模式的應用之一。 新增下面的程式碼即可實現基本功能。 class ViewController: UIViewControlle
java Swing 之下拉列表控制元件
/** * Java Swing 之下拉列表控制元件 * @author gao */ package com.gao; import java.awt.FlowLayout; import javax.swing.JComboBox; import javax.
java Swing 之列表框控制元件
/** * java Swing 列表框控制元件 * @author gao */ package com.gao; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing
iOS:選擇器控制元件UIPickerView的詳解和演示
#import "ViewController.h" 2 3 @interface ViewController () 4 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; 5 6 @end
react.js antd datePicker控制元件獲取時間與從資料庫獲取時間繫結到時間選擇器上
npm install moment --save # npm yarn add moment let moment = require('moment'); //資料庫存入的時間為 let datam1=2018-11-20 17:35:00 //如果是時間戳,請自行轉換 //onCh
DateTimePicker控制元件(日期時間選擇器)《筆記5》
DateTimePicker控制元件使用筆記 如何取年限,時間的資料? 關鍵詞(Value) #region Value顯示模式 label5.Text = "年限:" + dateTimePicker4.Value.Ye
組裝原有控制元件實現橫向滾動的時間選擇器,可滑動 點選,初始狀態在指定位置
簡介 前段時間,產品設計了一個橫向的滾動的時間選擇器,由於工作很急,也沒時間來自定義view,而且目前開發的專案很老了,不支援V7的包,所以我不能用recyclerview。我就將就之前的GridView和HorizontalScrollView,組裝了一哈就可以了。本
JAVA SWING 日期選擇控制元件(目前支援JLable和JTextField)
package com.monitor.date; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; i
關於Timer計時器控制元件的java.lang.IllegalStateException: TimerTask is scheduled already問題分析
博主最近在改被人的專案,準備寫一個類似今日頭條的載入完成提示,就寫了個計時器讓它顯示幾秒,於是就用到了timer這個計時器控制元件,簡單的正常程式碼如下,因為博主的載入需要多次,當timer二次呼叫的時候就出現了java.lang.IllegalStateException: TimerTask i
基於vue開發的多功能的時間選擇器元件,開箱即用
好一段時間沒有寫過部落格了,在國慶期間心血來潮優化了一個元件,在日常開發中時常會有需求用到時間選擇器,不同的專案需求可能會不一樣。近期開發的幾個專案中就有需求用到這樣的選擇器,由於以前有用到相關的元件,只是功能沒有這麼強大,趁著國慶放假的時間我就基於原元件添加了一些功能,現在也釋出到npm包了,有需要的小夥伴
自定義java Swing日曆選擇元件JCalendarChooser
package cn.net.iem.view.mainframe; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridBagConstraints; impo
android基礎-Toast提示框、日曆檢視(CalendarView)元件、日期、時間選擇器DatePicker和TimerPicker等
1. Toast提示框 // 建立一個Toast提示資訊 Toast toast = Toast.makeText(MainActivity.this , "簡單
ElementUI DatePicker 日期選擇器控制選擇時間範圍
time() value hold disabled -o 24* true 時間 picker 選擇今天以及今天之後的日期 <el-date-picker v-model="value1" type="date"
時間選擇器jquerry特效
清空 z-index format 獲取 sub tran cond hang button double-date.js代碼 $(function() { var dateStr = ‘<div class="date-list"><div cla
日期時間選擇器插件flatpickr
link get date 告訴 alt 解決 自己 images ast 前言:在網頁上需要輸入時間的時候,我們可以用HTML5的inputl中的date類型。但是如下入所示,有些瀏覽器不支持。flatpickr這個小插件可以解決這個問題。 1.flatpickr日期時
Android零基礎入門第57節:日期選擇器DatePicker和時間選擇器TimePicker
oncreate ted show imageview bce min date 教程 運行程序 在實際開發中,經常會遇見一些時間選擇器、日期選擇器、數字選擇器等需求,那麽從本期開始來學習Android中常用選擇器,今天學習的是DatePicker和TimePicke
android日期時間選擇器
net oid androi class 我們 下載地址 很好 需要 滿足 android原生的日期時間控件,因為是原生的總有其滿足不了我們需求的時候,Android 手機版本那麽多,用戶彈出來的控件五花八門。因為項目需要,在網上找了一 些demo看了看,感覺有些寫的很好,
vue 時間選擇器組件
emp str else template route spl efault emit child vue 時間選擇器組件 組件效果: 單文件組件: <template> <div class="date-pickers"> &
element 時間選擇器——年
技術分享 有關 alt 關於 image ear class 技術 http <el-date-picker v-model="fileYear" type="year" placeholder="選擇年"> </el-date-picker>