用java记事本编写以下代码,以“yyyy:mm:dd”格式输入一个日期,要求输入年份在1949-2021,月份在01-12,天数在01-31。(不考虑闰年及每月具体天数)编写程序实现日期的合法性检测,输入正确则提示“OK”, 否则根据实际情况提示“日期格式不符合要求”
时间: 2023-03-08 12:58:52 浏览: 78
import java.util.Scanner;
public class dateCheck{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入日期(yyyy:mm:dd格式):");
String date = input.nextLine();
String[] arr = date.split(":");
if(arr.length != 3) {
System.out.println("日期格式不符合要求");
}
else {
int year = Integer.parseInt(arr[0]);
int month = Integer.parseInt(arr[1]);
int day = Integer.parseInt(arr[2]);
if(year<1949 || year>2021 || month<1 || month>12 || day<1 || day>31){
System.out.println("日期格式不符合要求");
}
else{
System.out.println("OK");
}
}
}
}答案:我已经编写了一段代码来检测日期的合法性,它可以帮助你检查输入的日期是否符合要求。
相关问题
仿照Windows中的记事本,编写一个Java程序记事本。代码要求:有打开和保存文本文件,设置字体功能
好的,我可以为您提供一个简单的Java程序记事本,实现打开和保存文本文件的功能,以及设置字体的功能。
代码如下:
```java
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.*;
public class JavaNotepad extends JFrame implements ActionListener {
private JTextArea textArea;
private JFileChooser fileChooser;
private Font font;
public JavaNotepad() {
super("Java Notepad");
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
// 创建文件菜单
JMenu fileMenu = new JMenu("文件");
JMenuItem openMenuItem = new JMenuItem("打开");
openMenuItem.addActionListener(this);
JMenuItem saveMenuItem = new JMenuItem("保存");
saveMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
// 创建格式菜单
JMenu formatMenu = new JMenu("格式");
JMenuItem fontMenuItem = new JMenuItem("字体");
fontMenuItem.addActionListener(this);
formatMenu.add(fontMenuItem);
// 将菜单栏添加到窗口中
menuBar.add(fileMenu);
menuBar.add(formatMenu);
setJMenuBar(menuBar);
// 创建文本区域
textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
// 创建文件选择器
fileChooser = new JFileChooser();
// 设置窗口大小和可见性
setSize(800, 600);
setVisible(true);
}
// 处理菜单项的动作
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("打开")) {
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else if (command.equals("保存")) {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else if (command.equals("字体")) {
font = JFontChooser.showDialog(this, "选择字体", font);
if (font != null) {
textArea.setFont(font);
}
}
}
public static void main(String[] args) {
new JavaNotepad();
}
}
// 字体选择器
class JFontChooser extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private static JDialog dialog;
private static Font font;
private static JLabel previewLabel;
private static JComboBox<String> fontNameComboBox;
private static JComboBox<String> fontStyleComboBox;
private static JComboBox<Integer> fontSizeComboBox;
private static JButton okButton;
private static JButton cancelButton;
public JFontChooser() {
super(new BorderLayout());
// 字体名称选择框
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = env.getAvailableFontFamilyNames();
fontNameComboBox = new JComboBox<String>(fontNames);
fontNameComboBox.setSelectedItem(font.getName());
// 字体样式选择框
String[] fontStyles = {"常规", "粗体", "斜体", "粗斜体"};
fontStyleComboBox = new JComboBox<String>(fontStyles);
fontStyleComboBox.setSelectedIndex(font.getStyle());
// 字体大小选择框
Integer[] fontSizes = {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72};
fontSizeComboBox = new JComboBox<Integer>(fontSizes);
fontSizeComboBox.setSelectedItem(font.getSize());
// 预览标签
previewLabel = new JLabel("这是一个字体预览标签", JLabel.CENTER);
previewLabel.setFont(font);
// 确定和取消按钮
okButton = new JButton("确定");
okButton.addActionListener(this);
cancelButton = new JButton("取消");
cancelButton.addActionListener(this);
// 创建面板
JPanel panel1 = new JPanel(new GridLayout(3, 2));
panel1.add(new JLabel("字体名称:"));
panel1.add(fontNameComboBox);
panel1.add(new JLabel("字体样式:"));
panel1.add(fontStyleComboBox);
panel1.add(new JLabel("字体大小:"));
panel1.add(fontSizeComboBox);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(previewLabel, BorderLayout.CENTER);
JPanel panel3 = new JPanel(new FlowLayout());
panel3.add(okButton);
panel3.add(cancelButton);
// 将面板添加到窗口中
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
}
// 显示字体选择器对话框
public static Font showDialog(JFrame parent, String title, Font font) {
JFontChooser.font = font;
dialog = new JDialog(parent, title, true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.getContentPane().add(new JFontChooser());
dialog.pack();
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
return JFontChooser.font;
}
// 处理按钮的动作
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
String fontName = (String)fontNameComboBox.getSelectedItem();
int fontStyle = fontStyleComboBox.getSelectedIndex();
int fontSize = (Integer)fontSizeComboBox.getSelectedItem();
font = new Font(fontName, fontStyle, fontSize);
previewLabel.setFont(font);
dialog.dispose();
} else if (e.getSource() == cancelButton) {
dialog.dispose();
}
}
}
```
这个Java记事本程序使用了Swing组件,包括JFrame、JMenuBar、JMenu、JMenuItem、JTextArea、JScrollPane、JFileChooser、JLabel、JComboBox、JButton等。它提供了打开、保存和设置字体的功能。您可以根据自己的需求进行修改和扩展。
用Java写的记事本程序 要求:让它在打开的时候默认打开一个文件
好的,我的回答如下:
您可以使用以下代码来实现这个要求:
```java
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class MyNotepad extends JFrame {
JTextArea textArea;
public MyNotepad() {
setTitle("MyNotepad");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
//创建文本区域
textArea = new JTextArea();
add(new JScrollPane(textArea));
//默认打开文件
File file = new File("default.txt");
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder fileContent = new StringBuilder();
while ((line = reader.readLine()) != null) {
fileContent.append(line).append("\n");
}
reader.close();
textArea.setText(fileContent.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new MyNotepad().setVisible(true);
}
}
```
这是一个基于 Java 的记事本程序,包括一个带滚动条的文本区域,在程序启动时会默认打开一个名为“default.txt”的文本文件,如果该文件不存在则不会打开任何文件。您可以将程序编译成可执行文件,双击打开即可使用。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)