我想点击按钮打开日期选择器。但是当我点击按钮时,它会在屏幕的左上角显示一个日历。我想在按钮下方看到它。我该怎么做?
另外,如何更改日历的大小以使其有点小?
以下是示例。
package practiceproblems;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DatePicker {
int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
JLabel l = new JLabel("", JLabel.CENTER);
String day = "";
JDialog d;
JButton[] button = new JButton[49];
public DatePicker(JFrame parent) {
d = new JDialog();
d.setModal(true);
String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
JPanel p1 = new JPanel(new GridLayout(7, 7));
p1.setPreferredSize(new Dimension(430, 120));
for (int x = 0; x < button.length; x++) {
final int selection = x;
button[x] = new JButton();
button[x].setFocusPainted(false);
button[x].setBackground(Color.white);
if (x > 6)
button[x].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
day = button[selection].getActionCommand();
d.dispose();
}
});
if (x < 7) {
button[x].setText(header[x]);
button[x].setForeground(Color.red);
}
p1.add(button[x]);
}
JPanel p2 = new JPanel(new GridLayout(1, 3));
JButton previous = new JButton("<< Previous");
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
month--;
displayDate();
}
});
p2.add(previous);
p2.add(l);
JButton next = new JButton("Next >>");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
month++;
displayDate();
}
});
p2.add(next);
d.add(p1, BorderLayout.CENTER);
d.add(p2, BorderLayout.SOUTH);
d.pack();
d.setLocationRelativeTo(parent);
displayDate();
d.setVisible(true);
}
public void displayDate() {
for (int x = 7; x < button.length; x++)
button[x].setText("");
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy");
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month, 1);
int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
button[x].setText("" + day);
l.setText(sdf.format(cal.getTime()));
d.setTitle("Date Picker");
}
public String setPickedDate() {
if (day.equals(""))
return day;
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month, Integer.parseInt(day));
return sdf.format(cal.getTime());
}
}
class Picker {
public static void main(String[] args) {
JLabel label = new JLabel("Selected Date:");
final JTextField text = new JTextField(20);
JButton b = new JButton("popup");
JPanel p = new JPanel();
p.add(label);
p.add(text);
p.add(b);
final JFrame f = new JFrame();
f.getContentPane().add(p);
f.pack();
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
text.setText(new DatePicker(f).setPickedDate());
}
});
}
}
答案 0 :(得分:3)
因此,您在按钮下方显示日历所需的代码如下所示。但是在你可以使用它之前,你的代码几乎没有变化,你必须(1),应该(2)和可能(3)。
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
DatePicker dp = new DatePicker(f);
Point bP = b.getLocationOnScreen();
dp.d.setLocation(bP.x, bP.y + b.getHeight());
dp.d.setVisible(true);
text.setText(dp.setPickedDate());
}
});
当考虑到尺寸变化时,问题的第二部分安德鲁·汤普森回答说很好,因为你已经打电话给包装了,任何尺寸修改都是不受欢迎的。
享受,博罗。
答案 1 :(得分:2)
我想在按钮下方看到它。我该怎么做? ..
Component.setLocation(int, int)或Window.setLocationRelativeTo(Component)。
..如何更改日历的大小以使其有点小?
Component.setSize(int, int),或者作为trashgod建议,致电Window.pack()。
后者更好,因为pack()
计算窗口(框架,对话框等)在不截断内容的情况下有多小的“繁重”。它不是确定最小尺寸的简单任务。