初学者求助:如何做一个只可以输入日期的文本框

verymorose 2004-07-15 01:43:29
有现成的控件吗
是用jFormattedTextField类吗,
该如何控制输入的格式呢,
能不能在右边加上一个旋转按钮,就是可以点击按钮加一天,减一天的那种输入框
...全文
141 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
verymorose 2004-07-15
  • 打赏
  • 举报
回复
to lovelx(小狼剑剑):
哥们,你写的这个东西不是太好使阿,年月日都可以随意输,而且随便输几个字符,没有日期校验阿
verymorose 2004-07-15
  • 打赏
  • 举报
回复
我加入到界面上了,怎么输入部分不能得到焦点阿,倒是可以点按钮,但输入部分没有数字显示阿
verymorose 2004-07-15
  • 打赏
  • 举报
回复
我把它加到我的界面上还需要些什么咚咚阿
我是初学,都是用JBUILDER以图形方式添加的
lovelx 2004-07-15
  • 打赏
  • 举报
回复
java 中没有你说的那种控件,要想用,只能自己做。

我就是用的时候临时写的一个。

是有点垃圾。。。嘿嘿
lovelx 2004-07-15
  • 打赏
  • 举报
回复
package test;

import java.awt.*;
import javax.swing.*;

/**
* <p>File Name: BevelArrowIcon</p>
* <p>Company: Informedia Ltd.</p>
* <p>Date: 2003/07/31</p>
* @author gzm
*/

public class BlankIcon implements Icon {
public static final int UP = 0;// direction
public static final int DOWN = 1;

private static final int DEFAULT_SIZE =6;

private Color edge1;
private Color edge2;
private Color fill;
private int size ;
private int direction ;

public BlankIcon(int direction, boolean isRaisedView, boolean isPressedView) {
if (isRaisedView) {
if (isPressedView) {
init( UIManager.getColor("controlLtHighlight"),
UIManager.getColor("controlDkShadow"),
UIManager.getColor("controlShadow"),
DEFAULT_SIZE, direction);
} else {
init( UIManager.getColor("controlHighlight"),
UIManager.getColor("controlShadow"),
UIManager.getColor("control"),
DEFAULT_SIZE, direction);
}
} else {
if (isPressedView) {
init( UIManager.getColor("controlDkShadow"),
UIManager.getColor("controlLtHighlight"),
UIManager.getColor("controlShadow"),
DEFAULT_SIZE, direction);
} else {
init( UIManager.getColor("controlShadow"),
UIManager.getColor("controlHighlight"),
UIManager.getColor("control"),
DEFAULT_SIZE, direction);
}
}
}

public BlankIcon(Color edge1, Color edge2, Color fill,
int size, int direction) {
init(edge1, edge2, fill, size, direction);
}


public void paintIcon(Component c, Graphics g, int x, int y) {
switch (direction) {
case DOWN: drawDownArrow(g, x, y); break;
case UP: drawUpArrow(g, x, y); break;
}
}

public int getIconWidth() {
return size;
}

public int getIconHeight() {
return size;
}


private void init(Color edge1, Color edge2, Color fill,
int size, int direction) {
this.edge1 = edge1;
this.edge2 = edge2;
this.fill = fill;
this.size = size;
System.out.println(size+"\n");
this.direction = direction;
}

private void drawDownArrow(Graphics g, int xo, int yo) {
g.setColor(new Color(0,0,0)) ;
//g.setColor(edge1);
g.drawLine(xo, yo, xo+size-1, yo);
g.drawLine(xo, yo+1, xo+size-2, yo+1);
//g.setColor(edge2);
g.drawLine(xo+size-2, yo+1, xo+size-1, yo+1);
int x = xo+1;
int y = yo+2;
int dx = size-6;
while (y+1 < yo+size) {
// g.setColor(edge1);
g.drawLine(x, y, x+1, y);
g.drawLine(x, y+1, x+1, y+1);
if (0 < dx) {
// g.setColor(fill);
g.drawLine(x+2, y, x+1+dx, y);
g.drawLine(x+2, y+1, x+1+dx, y+1);
}
// g.setColor(edge2);
g.drawLine(x+dx+2, y, x+dx+3, y);
g.drawLine(x+dx+2, y+1, x+dx+3, y+1);
x += 1;
y += 2;
dx -= 2;
}
// g.setColor(edge1);
g.drawLine(xo+(size/2), yo+size-1, xo+(size/2), yo+size-1);
}

private void drawUpArrow(Graphics g, int xo, int yo) {
//g.setColor(new Color(0,0,0)) ;
g.setColor(new Color(0,0,0));
int x = xo+(size/2);
g.drawLine(x, yo, x, yo);
x--;
int y = yo+1;
int dx = 0;

while (y+3 < yo+size) {
// g.setColor(edge1);
g.drawLine(x, y, x+1, y);
g.drawLine(x, y+1, x+1, y+1);
if (0 < dx) {
// g.setColor(new Color(0,0,0));
g.drawLine(x+2, y, x+1+dx, y);
g.drawLine(x+2, y+1, x+1+dx, y+1);
}

// g.setColor(new Color(255,255,255));
g.drawLine(x+dx+2, y, x+dx+2, y);
g.drawLine(x+dx+2, y+1, x+dx+2, y+1);
x -= 1;
y += 2;
dx += 2;
}
// g.setColor(new Color(255,255,255));
g.drawLine(xo+1, yo+size-3, xo+size-1, yo+size-3);
//g.setColor(new Color(0,0,0));
g.drawLine(xo+1, yo+size-2, xo+size-1, yo+size-2);
g.drawLine(xo, yo+size-1, xo+size, yo+size-1);

}


}
lovelx 2004-07-15
  • 打赏
  • 举报
回复
这是我做的,用的时候直接创建个队象就好

ps: 按钮的icon 是我自己做的,你要是有更好的可以不用。

下面是我自己定义的icon
verymorose 2004-07-15
  • 打赏
  • 举报
回复
2楼的太长了,我还不太懂啊,有没有简单一点的方法阿
hl_longman 2004-07-15
  • 打赏
  • 举报
回复
UP
lovelx 2004-07-15
  • 打赏
  • 举报
回复
package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JSpinner;
import javax.swing.border.*;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.util.GregorianCalendar;


/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class Panel1 extends JPanel implements ActionListener{
Focus_Listener focus = new Focus_Listener();
BorderLayout borderLayout1 = new BorderLayout();
JPanel contentPane;
Border border1;
JTextField jTextField1 = new JTextField();
JPanel jPanel1 = new JPanel();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
Border border2;
Border border3;
Border border4;
Border border5;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
FlowLayout flowLayout1 = new FlowLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
int focusFlag = 0;

public Panel1() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
setPanel();
}

private void setPanel(){
border1 = BorderFactory.createEmptyBorder();
border2 = BorderFactory.createEmptyBorder();
border3 = BorderFactory.createEmptyBorder();
border4 = BorderFactory.createEmptyBorder();
border5 = BorderFactory.createLineBorder(SystemColor.controlText,1);
this.setLayout(null);
this.setBackground(SystemColor.menu);
this.setSize(new Dimension(530, 386));
jTextField1.setBackground(Color.white);
jTextField1.setBorder(border1);
jTextField1.addActionListener(new Panel1_jTextField1_actionAdapter(this));
jTextField1.addFocusListener(focus);
jTextField2.addFocusListener(focus);
jTextField3.addFocusListener(focus);
jPanel1.setBackground(Color.white);
jPanel1.setBorder(border5);
jPanel1.setBounds(new Rectangle(2, 6, 98, 24));
jPanel1.setLayout(flowLayout1);
jTextField2.setBorder(border1);
jTextField3.setBorder(border1);
jLabel1.setText("/");
jLabel2.setText("/");
jButton1.setBounds(new Rectangle(101, 18, 15, 15));
jButton1.setIcon(new BlankIcon(BlankIcon.DOWN ,true,true));
jButton1.addActionListener(new Panel1_jButton1_actionAdapter(this));
jButton1.addActionListener(this);
jButton2.setBounds(new Rectangle(101, 3, 15, 15));
jButton2.setIcon(new BlankIcon(BlankIcon.UP ,true,true));
jButton2.addActionListener(new Panel1_jButton2_actionAdapter(this));
jButton2.addActionListener(this);
jPanel1.add(jTextField1, null);
jPanel1.add(jLabel2, null);
jPanel1.add(jTextField3, null);
jPanel1.add(jLabel1, null);
jPanel1.add(jTextField2, null);
this.add(jButton2, null);
this.add(jButton1, null);
this.add(jPanel1, null);
jTextField1.requestFocus();
jTextField1.selectAll();
this.setSize(new Dimension(122, 39)) ;
setDate();

}
void setDate()
{
GregorianCalendar cal = new GregorianCalendar();
String sys_year = Integer.toString( cal.get(cal.YEAR) );
String sys_month = Integer.toString( (cal.get(cal.MONTH) + 1) );
String sys_day = Integer.toString( cal.get(cal.DATE) );

jTextField1.setText(sys_year);
if ( Integer.parseInt(sys_day) < 10 )
{
jTextField2.setText("0" + sys_day);
}
else
{
jTextField2.setText(sys_day);
}
if ( Integer.parseInt(sys_month) < 10 )
{
jTextField3.setText("0" + sys_month);
}
else
{
jTextField3.setText(sys_month);
}
}

class Focus_Listener extends FocusAdapter{

public void focusGained(FocusEvent e) {
if ((JTextField)e.getSource() == jTextField1 ){
focusFlag = 1;
jTextField1.selectAll();
} else if ((JTextField)e.getSource() == jTextField2 ){
focusFlag = 2;
jTextField2.selectAll();
}else if ((JTextField)e.getSource() == jTextField3 ){
focusFlag = 3;
jTextField3.selectAll();
}

}
}


public void actionPerformed(ActionEvent e) {

}

void jButton2_actionPerformed(ActionEvent e) {
try{
int text = 0;
if (focusFlag == 1) {
text = Integer.parseInt(jTextField1.getText());
jTextField1.setText(String.valueOf(text + 1));
}
if (focusFlag == 2) {
text = Integer.parseInt(jTextField2.getText());
if (text < 10) {
jTextField2.setText("0" + String.valueOf(text + 1));
}
else {
if ( !(text<31) ) {
jTextField2.setText("01");
}
else {
jTextField2.setText(String.valueOf(text + 1));
}
}
}
if (focusFlag == 3) {
text = Integer.parseInt(jTextField3.getText());
if (text < 10) {
jTextField3.setText("0" + String.valueOf(text + 1));
}
else {
if ( !(text<12) ) {
jTextField3.setText("01");
}
else {
jTextField3.setText(String.valueOf(text + 1));
}
}

}
}catch(Exception ex){}

}

void jButton1_actionPerformed(ActionEvent e) {
try{
int text = 0;
if (focusFlag == 1) {
text = Integer.parseInt(jTextField1.getText());
jTextField1.setText(String.valueOf(text - 1));
}
if (focusFlag == 2) {
text = Integer.parseInt(jTextField2.getText());
if (text < 11) {
if ( !(text>1) ) {
jTextField2.setText("31");
}
else {
jTextField2.setText("0" + String.valueOf(text - 1));
}
}
else {
jTextField2.setText(String.valueOf(text - 1));
}
}
if (focusFlag == 3) {
text = Integer.parseInt(jTextField3.getText());
if (text < 11) {
if ( !(text>1) ) {
jTextField3.setText("12");
}
else {
jTextField3.setText("0" + String.valueOf(text - 1));
}

}
else {
jTextField3.setText(String.valueOf(text - 1));
}

}
}catch(Exception ex){}

}

void jTextField1_actionPerformed(ActionEvent e) {

}

public String getDate()
{
String date = "";
date += jTextField1.getText() + "/";
date += jTextField3.getText() + "/";
date += jTextField2.getText();
// System.out.println("date "+date);
return date;
}

}

class Panel1_jButton2_actionAdapter implements java.awt.event.ActionListener {
Panel1 adaptee;

Panel1_jButton2_actionAdapter(Panel1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}

class Panel1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Panel1 adaptee;

Panel1_jButton1_actionAdapter(Panel1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}



class Panel1_jTextField1_actionAdapter implements java.awt.event.ActionListener {
Panel1 adaptee;

Panel1_jTextField1_actionAdapter(Panel1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jTextField1_actionPerformed(e);
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧