java万年历javaswing带系统时间
时间: 2023-05-13 14:00:23 浏览: 156
Java万年历是使用Java编写的日历应用程序,可以显示任何日期和年份的日历,并具有一些其他功能,例如计算两个日期之间的天数和周数等。
JavaSwing是Java平台的用户界面(UI)工具包,Java开发人员可以使用它来创建桌面应用程序。
将JavaSwing集成到Java万年历中可以增强应用程序的用户界面,使其更加美观和易于使用。
为了带有系统时间,可以使用Java提供的时间类(java.util.Date)和格式化类(java.text.SimpleDateFormat)。我们可以使用SimpleDateFormat将系统时间格式化为我们想要的格式,并将其放在Java万年历的合适位置。
当Java万年历启动时,我们可以使用以下代码来获取当前系统时间:
```
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String sysTime = dateFormat.format(date);
```
将该字符串sysTime插入到Java万年历的适当位置即可。
综上所述,通过将JavaSwing和系统时间集成到Java万年历中,可以增强应用程序的用户界面和实用性。
相关问题
用Java swing组件和Java card写一个带备忘录的万年历
好的,以下是一个简单的Java Swing和Java Card应用程序示例,它实现了一个带备忘录的万年历。该应用程序使用Java Swing组件构建用户界面,并使用Java Card存储备忘录数据。
Java Swing部分:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class MyCalendar extends JFrame implements ActionListener {
private JTextArea memo;
private JButton saveButton;
private JButton loadButton;
private JTextField dateField;
private JButton showButton;
private JLabel messageLabel;
public MyCalendar() {
setTitle("MyCalendar");
setSize(400, 300);
JPanel topPanel = new JPanel();
dateField = new JTextField(10);
topPanel.add(dateField);
showButton = new JButton("Show");
showButton.addActionListener(this);
topPanel.add(showButton);
add(topPanel, BorderLayout.NORTH);
memo = new JTextArea();
JScrollPane memoScrollPane = new JScrollPane(memo);
add(memoScrollPane, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
saveButton = new JButton("Save");
saveButton.addActionListener(this);
bottomPanel.add(saveButton);
loadButton = new JButton("Load");
loadButton.addActionListener(this);
bottomPanel.add(loadButton);
add(bottomPanel, BorderLayout.SOUTH);
messageLabel = new JLabel();
add(messageLabel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == showButton) {
showMemo();
} else if (e.getSource() == saveButton) {
saveMemo();
} else if (e.getSource() == loadButton) {
loadMemo();
}
}
private void showMemo() {
String dateString = dateField.getText();
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateString);
String memoText = JavaCard.getMemo(date);
memo.setText(memoText);
messageLabel.setText("");
} catch (Exception e) {
messageLabel.setText("Invalid date format: " + dateString);
}
}
private void saveMemo() {
String dateString = dateField.getText();
String memoText = memo.getText();
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateString);
JavaCard.saveMemo(date, memoText);
messageLabel.setText("Memo saved for date: " + dateString);
} catch (Exception e) {
messageLabel.setText("Invalid date format: " + dateString);
}
}
private void loadMemo() {
String memoText = JavaCard.loadMemo();
memo.setText(memoText);
messageLabel.setText("Memo loaded from card.");
}
public static void main(String[] args) {
MyCalendar calendar = new MyCalendar();
calendar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calendar.setVisible(true);
}
}
```
Java Card部分:
```java
import javacard.framework.*;
import javacard.security.*;
import javacardx.crypto.*;
public class JavaCard extends Applet {
private static final byte MEMO_RECORD_SIZE = 256;
private static final byte MEMO_RECORD_COUNT = 100;
private static final byte INS_SAVE_MEMO = (byte) 0x01;
private static final byte INS_LOAD_MEMO = (byte) 0x02;
private static final short MEMO_RECORDS_OFFSET = (short) 0x0000;
private static final short MEMO_RECORD_SIZE_OFFSET = (short) 0x7F00;
private static final byte[] DEFAULT_KEY = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
private Cipher cipher;
private byte[] sessionKey;
private RandomData random;
private JavaCard() {
random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
cipher = Cipher.getInstance(Cipher.ALG_DES_CBC_ISO9797_M2, false);
sessionKey = new byte[8];
}
public static void install(byte[] bArray, short bOffset, byte bLength) {
new JavaCard().register();
}
public void process(APDU apdu) {
byte[] buffer = apdu.getBuffer();
if (selectingApplet()) {
return;
}
switch (buffer[ISO7816.OFFSET_INS]) {
case INS_SAVE_MEMO:
saveMemo(buffer);
break;
case INS_LOAD_MEMO:
loadMemo(apdu);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
private void saveMemo(byte[] buffer) {
byte[] data = new byte[MEMO_RECORD_SIZE];
short dataOffset = 0;
short recordOffset = MEMO_RECORDS_OFFSET;
while (recordOffset < MEMO_RECORDS_OFFSET + MEMO_RECORD_COUNT * MEMO_RECORD_SIZE) {
short bytesRead = readRecord(recordOffset, data, dataOffset, MEMO_RECORD_SIZE);
if (bytesRead == 0) {
break;
}
recordOffset += MEMO_RECORD_SIZE;
dataOffset += bytesRead;
}
short newRecordOffset = getNewRecordOffset();
Util.arrayCopy(buffer, ISO7816.OFFSET_CDATA, data, newRecordOffset, MEMO_RECORD_SIZE);
writeRecord(newRecordOffset, data, newRecordOffset, MEMO_RECORD_SIZE);
}
private void loadMemo(APDU apdu) {
byte[] buffer = apdu.getBuffer();
short bytesRead = readRecord(MEMO_RECORDS_OFFSET, buffer, ISO7816.OFFSET_CDATA, MEMO_RECORD_SIZE);
apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, bytesRead);
}
private short readRecord(short offset, byte[] buffer, short bufferOffset, short length) {
cipher.init(new SecretKeySpec(sessionKey, (short) 0, (short) sessionKey.length, Cipher.ALG_DES_CBC_NOPAD), Cipher.MODE_DECRYPT);
cipher.doFinal(DEFAULT_KEY, (short) 0, (short) DEFAULT_KEY.length, buffer, MEMO_RECORD_SIZE_OFFSET);
Util.arrayCopy(buffer, MEMO_RECORD_SIZE_OFFSET + offset, buffer, bufferOffset, length);
random.generateData(sessionKey, (short) 0, (short) sessionKey.length);
cipher.init(new SecretKeySpec(sessionKey, (short) 0, (short) sessionKey.length, Cipher.ALG_DES_CBC_NOPAD), Cipher.MODE_ENCRYPT);
cipher.doFinal(DEFAULT_KEY, (short) 0, (short) DEFAULT_KEY.length, buffer, MEMO_RECORD_SIZE_OFFSET);
return length;
}
private void writeRecord(short offset, byte[] buffer, short bufferOffset, short length) {
cipher.init(new SecretKeySpec(sessionKey, (short) 0, (short) sessionKey.length, Cipher.ALG_DES_CBC_NOPAD), Cipher.MODE_DECRYPT);
cipher.doFinal(DEFAULT_KEY, (short) 0, (short) DEFAULT_KEY.length, buffer, MEMO_RECORD_SIZE_OFFSET);
Util.arrayCopy(buffer, bufferOffset, buffer, MEMO_RECORD_SIZE_OFFSET + offset, length);
random.generateData(sessionKey, (short) 0, (short) sessionKey.length);
cipher.init(new SecretKeySpec(sessionKey, (short) 0, (short) sessionKey.length, Cipher.ALG_DES_CBC_NOPAD), Cipher.MODE_ENCRYPT);
cipher.doFinal(DEFAULT_KEY, (short) 0, (short) DEFAULT_KEY.length, buffer, MEMO_RECORD_SIZE_OFFSET);
}
private short getNewRecordOffset() {
short recordOffset = MEMO_RECORDS_OFFSET;
while (recordOffset < MEMO_RECORDS_OFFSET + MEMO_RECORD_COUNT * MEMO_RECORD_SIZE) {
if (readRecord(recordOffset, JCSystem.makeTransientByteArray(MEMO_RECORD_SIZE, JCSystem.CLEAR_ON_DESELECT), (short) 0, MEMO_RECORD_SIZE) == 0) {
return recordOffset;
}
recordOffset += MEMO_RECORD_SIZE;
}
ISOException.throwIt(ISO7816.SW_FILE_FULL);
return (short) 0;
}
public static String getMemo(Date date) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = dateFormat.format(date);
byte[] buffer = new byte[MEMO_RECORD_SIZE];
short bytesRead = JavaCard.getInstance().readRecord((short) (MEMO_RECORDS_OFFSET + dateString.getBytes().length), buffer, (short) 0, MEMO_RECORD_SIZE);
return new String(buffer, (short) 0, bytesRead);
}
public static void saveMemo(Date date, String memo) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = dateFormat.format(date);
byte[] buffer = new byte[MEMO_RECORD_SIZE];
Util.arrayCopy(dateString.getBytes(), (short) 0, buffer, (short) 0, (short) dateString.getBytes().length);
Util.arrayCopy(memo.getBytes(), (short) 0, buffer, (short) dateString.getBytes().length, (short) memo.getBytes().length);
JavaCard.getInstance().writeRecord((short) (MEMO_RECORDS_OFFSET + dateString.getBytes().length), buffer, (short) 0, MEMO_RECORD_SIZE);
}
public static String loadMemo() {
byte[] buffer = new byte[MEMO_RECORD_SIZE];
JavaCard.getInstance().readRecord(MEMO_RECORDS_OFFSET, buffer, (short) 0, MEMO_RECORD_SIZE);
return new String(buffer, (short) 0, MEMO_RECORD_SIZE);
}
private static JavaCard instance;
private static JavaCard getInstance() {
if (instance == null) {
instance = new JavaCard();
}
return instance;
}
}
```
这只是一个简单的示例,您可以根据您的需求进行修改和扩展。请注意,Java Card应用程序的可用内存和处理能力可能有限,因此您需要谨慎考虑您的应用程序设计和实现。
阅读全文