用Java.Swing写一个万年历
时间: 2024-02-06 15:11:58 浏览: 109
好的,我可以为你提供一个简单的Java.Swing万年历示例代码,如下所示:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class CalendarApp extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JLabel lblMonth, lblYear;
JButton btnPrev, btnNext, btnExit;
JTable tblCalendar;
JComboBox<String> cmbYear;
DefaultTableModel modelCalendar;
public CalendarApp() {
setTitle("Java Calendar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(420, 320);
lblMonth = new JLabel();
lblYear = new JLabel();
cmbYear = new JComboBox<String>();
btnPrev = new JButton("<<");
btnNext = new JButton(">>");
btnExit = new JButton("Exit");
modelCalendar = new DefaultTableModel() {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int rowIndex, int mColIndex) {
return false;
}
};
tblCalendar = new JTable(modelCalendar);
tblCalendar.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
int row = tblCalendar.getSelectedRow();
int col = tblCalendar.getSelectedColumn();
int year = Integer.parseInt(cmbYear.getSelectedItem().toString());
int month = lblMonth.getText().equalsIgnoreCase("January") ? 0
: lblMonth.getText().equalsIgnoreCase("February") ? 1
: lblMonth.getText().equalsIgnoreCase("March") ? 2
: lblMonth.getText().equalsIgnoreCase("April") ? 3
: lblMonth.getText().equalsIgnoreCase("May") ? 4
: lblMonth.getText().equalsIgnoreCase("June") ? 5
: lblMonth.getText().equalsIgnoreCase("July") ? 6
: lblMonth.getText()
.equalsIgnoreCase("August") ? 7
: lblMonth.getText()
.equalsIgnoreCase("September")
? 8
: lblMonth.getText()
.equalsIgnoreCase("October")
? 9
: lblMonth
.getText()
.equalsIgnoreCase(
"November")
? 10
: 11;
if (evt.getClickCount() == 2) {
int day = (int) modelCalendar.getValueAt(row, col);
new DayApp(day, month, year);
}
}
});
JScrollPane paneCalendar = new JScrollPane(tblCalendar);
paneCalendar.setPreferredSize(new Dimension(300, 200));
JPanel pnlCalendar = new JPanel(null);
pnlCalendar.setBorder(BorderFactory.createTitledBorder("Calendar"));
btnPrev.addActionListener(this);
btnNext.addActionListener(this);
cmbYear.addActionListener(this);
btnExit.addActionListener(this);
pnlCalendar.add(lblMonth);
pnlCalendar.add(lblYear);
pnlCalendar.add(cmbYear);
pnlCalendar.add(btnPrev);
pnlCalendar.add(btnNext);
pnlCalendar.add(paneCalendar);
pnlCalendar.add(btnExit);
lblMonth.setBounds(160 - lblMonth.getPreferredSize().width / 2, 25, 100, 25);
lblYear.setBounds(10, 225, 80, 20);
cmbYear.setBounds(230, 225, 80, 20);
btnPrev.setBounds(10, 25, 50, 25);
btnNext.setBounds(300, 25, 50, 25);
paneCalendar.setBounds(10, 50, 380, 160);
btnExit.setBounds(160 - btnExit.getPreferredSize().width / 2, 250, 100, 25);
GregorianCalendar cal = new GregorianCalendar();
int year = cal.get(GregorianCalendar.YEAR);
int month = cal.get(GregorianCalendar.MONTH);
int day = cal.get(GregorianCalendar.DAY_OF_MONTH);
lblMonth.setText(String.format("%tB", cal));
lblYear.setText(String.format("%s", year));
for (int i = year - 100; i <= year + 100; i++) {
cmbYear.addItem(String.format("%s", i));
}
cmbYear.setSelectedItem(String.format("%s", year));
tblCalendar.getParent().setBackground(tblCalendar.getBackground());
tblCalendar.getTableHeader().setResizingAllowed(false);
tblCalendar.getTableHeader().setReorderingAllowed(false);
tblCalendar.setColumnSelectionAllowed(true);
tblCalendar.setRowSelectionAllowed(true);
tblCalendar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
for (int i = 0; i < 7; i++) {
modelCalendar.addColumn(new String[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }[i]);
}
tblCalendar.setRowHeight(38);
modelCalendar.setColumnCount(7);
modelCalendar.setRowCount(6);
for (int i = year - 100; i <= year + 100; i++) {
cmbYear.addItem(String.format("%s", i));
}
refreshCalendar(month, year);
add(pnlCalendar, BorderLayout.CENTER);
setVisible(true);
}
public void refreshCalendar(int month, int year) {
String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
int nod, som;
btnPrev.setEnabled(true);
btnNext.setEnabled(true);
if (month == 0 && year <= Calendar.getInstance().get(Calendar.YEAR) - 100) {
btnPrev.setEnabled(false);
}
if (month == 11 && year >= Calendar.getInstance().get(Calendar.YEAR) + 100) {
btnNext.setEnabled(false);
}
lblMonth.setText(months[month]);
lblMonth.setBounds(160 - lblMonth.getPreferredSize().width / 2, 25, 100, 25);
cmbYear.setSelectedItem(String.format("%s", year));
GregorianCalendar cal = new GregorianCalendar(year, month, 1);
nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
som = cal.get(GregorianCalendar.DAY_OF_WEEK);
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
modelCalendar.setValueAt(null, i, j);
}
}
for (int i = 1; i <= nod; i++) {
int row = new Integer((i + som - 2) / 7);
int column = (i + som - 2) % 7;
modelCalendar.setValueAt(i, row, column);
}
}
public static void main(String[] args) {
new CalendarApp();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnPrev)) {
int year = Integer.parseInt(cmbYear.getSelectedItem().toString());
int month = lblMonth.getText().equalsIgnoreCase("January") ? 11
: lblMonth.getText().equalsIgnoreCase("February") ? 0
: lblMonth.getText().equalsIgnoreCase("March") ? 1
: lblMonth.getText().equalsIgnoreCase("April") ? 2
: lblMonth.getText().equalsIgnoreCase("May") ? 3
: lblMonth.getText().equalsIgnoreCase("June") ? 4
: lblMonth.getText().equalsIgnoreCase("July") ? 5
: lblMonth.getText()
.equalsIgnoreCase("August") ? 6
: lblMonth.getText()
.equalsIgnoreCase("September")
? 7
: lblMonth.getText()
.equalsIgnoreCase("October")
? 8
: lblMonth.getText()
.equalsIgnoreCase(
"November")
? 9
: 10;
if (month == 0) {
month = 11;
year--;
} else {
month--;
}
refreshCalendar(month, year);
} else if (e.getSource().equals(btnNext)) {
int year = Integer.parseInt(cmbYear.getSelectedItem().toString());
int month = lblMonth.getText().equalsIgnoreCase("January") ? 11
: lblMonth.getText().equalsIgnoreCase("February") ? 0
: lblMonth.getText().equalsIgnoreCase("March") ? 1
: lblMonth.getText().equalsIgnoreCase("April") ? 2
: lblMonth.getText().equalsIgnoreCase("May") ? 3
: lblMonth.getText().equalsIgnoreCase("June") ? 4
: lblMonth.getText().equalsIgnoreCase("July") ? 5
: lblMonth.getText()
.equalsIgnoreCase("August") ? 6
: lblMonth.getText()
.equalsIgnoreCase("September")
? 7
: lblMonth.getText()
.equalsIgnoreCase("October")
? 8
: lblMonth.getText()
.equalsIgnoreCase(
"November")
? 9
: 10;
if (month == 11) {
month = 0;
year++;
} else {
month++;
}
refreshCalendar(month, year);
} else if (e.getSource().equals(cmbYear)) {
int year = Integer.parseInt(cmbYear.getSelectedItem().toString());
int month = lblMonth.getText().equalsIgnoreCase("January") ? 11
: lblMonth.getText().equalsIgnoreCase("February") ? 0
: lblMonth.getText().equalsIgnoreCase("March") ? 1
: lblMonth.getText().equalsIgnoreCase("April") ? 2
: lblMonth.getText().equalsIgnoreCase("May") ? 3
: lblMonth.getText().equalsIgnoreCase("June") ? 4
: lblMonth.getText().equalsIgnoreCase("July") ? 5
: lblMonth.getText()
.equalsIgnoreCase("August") ? 6
: lblMonth.getText()
.equalsIgnoreCase("September")
? 7
: lblMonth.getText()
.equalsIgnoreCase("October")
? 8
: lblMonth.getText()
.equalsIgnoreCase(
"November")
? 9
: 10;
refreshCalendar(month, year);
} else if (e.getSource().equals(btnExit)) {
System.exit(0);
}
}
}
class DayApp extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JLabel lblDay, lblMonth, lblYear;
JButton btnSave, btnCancel;
JTextArea txtEvent;
JPanel pnlDay;
public DayApp(int day, int month, int year) {
setTitle("Day Event");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
lblDay = new JLabel(String.format("%s", day));
lblMonth = new JLabel(String.format("%tB", new GregorianCalendar(year, month, 1)));
lblYear = new JLabel(String.format("%s", year));
txtEvent = new JTextArea();
pnlDay = new JPanel(null);
pnlDay.setBorder(BorderFactory.createTitledBorder("Event"));
btnSave = new JButton("Save");
btnCancel = new JButton("Cancel");
btnSave.addActionListener(this);
btnCancel.addActionListener(this);
lblDay.setBounds(140 - lblDay.getPreferredSize().width / 2, 25, 100, 25);
lblMonth.setBounds(10, 60, 100, 25);
lblYear.setBounds(230, 60, 50, 25);
txtEvent.setBounds(20, 90, 250, 50);
btnSave.setBounds(50, 150, 80, 25);
btnCancel.setBounds(160, 150, 80, 25);
pnlDay.add(lblDay);
pnlDay.add(lblMonth);
pnlDay.add(lblYear);
pnlDay.add(txtEvent);
pnlDay.add(btnSave);
pnlDay.add(btnCancel);
add(pnlDay);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnSave)) {
JOptionPane.showMessageDialog(this, "Event saved successfully!");
dispose();
} else if (e.getSource().equals(btnCancel)) {
dispose();
}
}
}
```
运行这个代码,你会看到一个简单的Java.Swing万年历。你可以选择年份和月份,然后浏览日历中的日期。你还可以双击一个日期来创建一个事件。
阅读全文