在 Java 中使用 JCalendar 选择出生日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20730946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting Date of Birth using JCalendar in Java
提问by RAJIL KV
I am developing a java desktop app. I wanna implement Date of Birth selecting using JCalendar. I have a JLabel and this jCalendar is added to JLabel .And set position using setBounds().But the calendar is overlapping with other components and i can't select the date
so what should i do?
我正在开发一个 Java 桌面应用程序。我想使用 JCalendar 实现出生日期选择。我有一个 JLabel,这个 jCalendar 被添加到 JLabel 中。并使用 setBounds() 设置位置。但是日历与其他组件重叠,我无法选择日期,我该怎么办?
Here is my code snippet
这是我的代码片段
JCalendar dob=new JCalendar();
raillabel. add(dob);
dob.setBounds(250, 186, 320, 330);
采纳答案by dic19
First off, why are you trying to add JCalendar
to a JLabel
? You can easily add this to a JPanel
:
首先,你为什么要添加JCalendar
到 a JLabel
?您可以轻松地将其添加到JPanel
:
JPanel panel = new JPanel();
panel.add(new JCalendar());
If you still want to add this JCalendar
to a JLabel
then you need to provide a LayoutManagerto this last one in order to properly add components to it:
如果你仍然想把它添加JCalendar
到 aJLabel
那么你需要为最后一个提供一个LayoutManager以便正确地向它添加组件:
JCalendar calendar = new JCalendar();
JLabel label = new JLabel("Select date of birth:");
label.setLayout(new BorderLayout());
label.add(calendar, BorderLayout.EAST);
Take a look to A Visual Guide to Layout Managers.
看看布局管理器的视觉指南。
As @mKorbel says in his comment you shouldn't use NullLayout
and setBounds()
method to set the component location/size. This is the task layout managers are intended for.
正如@mKorbel 在他的评论中所说,您不应该使用NullLayout
和setBounds()
方法来设置组件位置/大小。这是任务布局管理器的目的。
Finally:
最后:
I wanna implement Date of Birth selecting using
JCalendar
.
我想使用
JCalendar
.
You may want to try JDateChooser
instead, which allows selecting a date or type it by hand:
您可能想尝试一下JDateChooser
,它允许选择日期或手动输入:
JDateChooser chooser = new JDateChooser();
chooser.setLocale(Locale.US);
JPanel panel = new JPanel();
panel.add(new JLabel("Date of Birth:"));
panel.add(chooser);
Picture
图片
回答by David
Using swing, just place the jDateChooser control on your form and it will be just fine
使用 Swing,只需将 jDateChooser 控件放在您的表单上就可以了