utcalendar.java#1.1.1.1
来自「数据库远程同步软件NetBeans项目源文件 项目采用Jdesktop集成组件」· 1 代码 · 共 635 行 · 第 1/2 页
1
635 行
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package databasesyncdesktopapplication;
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.plaf.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
/**
*
* @author mjw917
*/
public class UTCalendar extends JComponent implements ActionListener
{
class Time extends JComponent implements ActionListener, FocusListener
{
class MinuteField extends JTextField
{
class MinuteDocument extends PlainDocument
{
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null)
return;
str = getText(0, offs) + str + getText(offs, getLength() - offs);
int i = 0;
try
{
i = Integer.parseInt(str);
}
catch (NumberFormatException numberformatexception)
{
}
if (i < 0 || i > 59)
return;
remove(0, getLength());
str = Integer.toString(i);
if (i < 10)
str = "0" + str;
super.insertString(0, str, a);
}
MinuteDocument()
{
}
}
protected Document createDefaultModel()
{
return new MinuteDocument();
}
public MinuteField(String t)
{
super(t);
}
}
class HourField extends JTextField
{
class HourDocument extends PlainDocument
{
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null)
return;
str = getText(0, offs) + str + getText(offs, getLength() - offs);
int i = 0;
try
{
i = Integer.parseInt(str);
}
catch (NumberFormatException numberformatexception)
{
}
if (i == 0)
i = 12;
else
if (i > 12 || i < 1)
return;
remove(0, getLength());
super.insertString(0, Integer.toString(i), a);
}
HourDocument()
{
}
}
protected Document createDefaultModel()
{
return new HourDocument();
}
public HourField(String t)
{
super(t);
}
}
class AMPMField extends JTextField
{
class AMPMDocument extends PlainDocument
{
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null)
return;
if (str.charAt(0) == 'p' || str.charAt(0) == 'P')
str = "PM";
else
if (str.charAt(0) == 'a' || str.charAt(0) == 'A')
str = "AM";
else
return;
remove(0, getLength());
super.insertString(0, str, a);
}
AMPMDocument()
{
}
}
protected Document createDefaultModel()
{
return new AMPMDocument();
}
public AMPMField(String t)
{
super(t);
}
}
public void setHours(int hours)
{
if (hours < 12)
{
txtAoP.setText("AM");
}
else
{
txtAoP.setText("PM");
if (hours > 12)
hours -= 12;
}
txtHrs.setText(Integer.toString(hours));
}
public void setMinutes(int minutes)
{
txtMns.setText(Integer.toString(minutes));
}
public int getHours()
{
int hours = 0;
try
{
hours = Integer.parseInt(txtHrs.getText());
}
catch (NumberFormatException numberformatexception)
{
}
if (txtAoP.getText().charAt(0) == 'P')
{
if (hours < 12)
hours += 12;
}
else
if (hours == 12)
hours = 0;
return hours;
}
public int getMinutes()
{
try
{
return Integer.parseInt(txtMns.getText());
}
catch (NumberFormatException numberformatexception)
{
return 0;
}
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == up && focus != null)
{
if (focus == txtAoP)
{
String t = txtAoP.getText();
txtAoP.setText(t.charAt(0) != 'P' ? "PM" : "AM");
}
else
if (focus == txtHrs)
{
int i = getHours() + 1;
if (i > 23)
i = 0;
setHours(i);
}
else
if (focus == txtMns)
{
int i = getMinutes() + 1;
if (i > 59)
i = 0;
setMinutes(i);
}
}
else
if (source == down)
if (focus != null && focus == txtAoP)
{
String t = txtAoP.getText();
txtAoP.setText(t.charAt(0) != 'P' ? "PM" : "AM");
}
else
if (focus == txtHrs)
{
int i = getHours() - 1;
if (i < 0)
i = 23;
setHours(i);
}
else
if (focus == txtMns)
{
int i = getMinutes() - 1;
if (i < 0)
i = 59;
setMinutes(i);
}
}
public void focusGained(FocusEvent fe)
{
if (fe.getComponent() instanceof JTextField)
focus = fe.getComponent();
}
public void focusLost(FocusEvent focusevent)
{
}
private JTextField txtHrs;
private JTextField txtMns;
private JTextField txtAoP;
private JButton up;
private JButton down;
private Component focus;
public Time()
{
Font font = new Font("SansSerif", 0, 10);
txtHrs = new HourField("12");
txtMns = new MinuteField("00");
txtAoP = new AMPMField("AM");
Dimension dim = txtHrs.getPreferredSize();
txtHrs.setBorder(BorderFactory.createEmptyBorder());
txtHrs.setFont(font);
txtHrs.addFocusListener(this);
txtHrs.setHorizontalAlignment(0);
txtMns.setBorder(BorderFactory.createEmptyBorder());
txtMns.setFont(font);
txtMns.addFocusListener(this);
txtMns.setHorizontalAlignment(0);
txtAoP.setBorder(BorderFactory.createEmptyBorder());
txtAoP.setFont(font);
txtAoP.addFocusListener(this);
txtAoP.setHorizontalAlignment(0);
int w = 18;
add(txtHrs);
txtHrs.setBounds(1, 1, w, dim.height);
JLabel colon = new JLabel(":");
colon.setFont(font);
int wc = colon.getPreferredSize().width;
add(colon);
colon.setBounds(w + 1, 1, wc, dim.height);
add(txtMns);
txtMns.setBounds(w + wc + 1, 1, w, dim.height);
add(txtAoP);
txtAoP.setBounds(2 * (w + wc) + 1, 1, w, dim.height);
up = new BasicArrowButton(1);
up.addActionListener(this);
add(up);
up.setBounds(3 * (w + wc) + 1, 1, dim.height / 2, dim.height / 2);
down = new BasicArrowButton(5);
down.addActionListener(this);
add(down);
down.setBounds(
3 * (w + wc) + 1,
1 + dim.height / 2,
dim.height / 2,
dim.height / 2);
setPreferredSize(
new Dimension(w * 3 + wc * 2 + 6 + dim.height / 2, dim.height + 2));
setBorder(BorderFactory.createLineBorder(Color.black));
focus = txtHrs;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?