我正在尝试使用 JTextArea 在 java 中制作约会日历。左键单击任何一天都应显示其自己的可编辑 JTextArea 用于编写约会。我只需要帮助弹出一个新的文本区域,别管每天的部分。我已经阅读了 docs.oracle 的东西,到目前为止我的尝试导致了未知来源的错误。
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.BorderLayout;
import javax.swing.*;
@SuppressWarnings("serial")
public class GUICal extends JFrame{
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];
static GUICal _gui;
public GUICal() {
JFrame jpf = new JFrame();
jpf.setDefaultCloseOperation(EXIT_ON_CLOSE);
d = new JDialog();
d.setModal(true);
String[] header = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
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.green);
if (x > 6) {
button[x].addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent ae) {
day = button[selection].getActionCommand();
switch(ae.getModifiers()) {
case InputEvent.BUTTON1_MASK: {//left-clicker
break;
}
case InputEvent.BUTTON3_MASK: {//right-clicker
break;
}
}
}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
});
}
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("<< Prior");
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(jpf);
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);
d.setTitle(sdf.format(cal.getTime()));
for (int y = 7; y < button.length; y++) {
String str = button[x].getText();
int m = Integer.parseInt(str);
SimpleDateFormat fm = new SimpleDateFormat("MMMM yyyy");
String dfm = fm.format(new Date());
SimpleDateFormat format = new SimpleDateFormat("dd");
String date = format.format(new Date());
int n = Integer.parseInt(date);
if ((m < n) && (dfm.equals(l.getText())))
button[x].setEnabled(false);
}
}
}
public String setPickedDate() {
if (day.equals(""))
return day;
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
" dd/MM/yyyy hh:mm:ss");
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month, Integer.parseInt(day));
return sdf.format(cal.getTime());
}
public static void main(String[] args) {
_gui = new GUICal();
}
}