void close() { try { int option = JOptionPane.showConfirmDialog(null,"是否保存", "记事本提示",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,null); switch (option) { case JOptionPane.YES_OPTION:saveFile();break; case JOptionPane.NO_OPTION:System.exit(0);break; case JOptionPane .CANCEL_OPTION:return;} } catch (Exception e) { e.printStackTrace(); } }
时间: 2024-02-14 15:20:52 浏览: 18
这段代码定义了一个方法close(),主要功能是在用户关闭程序时弹出一个对话框,询问用户是否需要保存文件,然后根据用户的选择进行相应的操作。
具体来说,这个方法会调用JOptionPane.showConfirmDialog()方法,弹出一个对话框,其中包含“是/否/取消”三个选项。如果用户选择“是”,则会调用saveFile()方法保存文件;如果用户选择“否”,则会直接退出程序;如果用户选择“取消”,则不做任何操作,返回到原来的界面。
如果在执行这个过程中出现异常,比如用户操作不当,会打印出异常信息。
相关问题
void close(){ try { int option = JOptionPane.showConfirmDialog(null,"是否保存", "记事本提示",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,null); switch (option) { case JOptionPane.YES_OPTION:saveFile();System.exit(0);break; case JOptionPane.NO_OPTION:System.exit(0);break; case JOptionPane .CANCEL_OPTION:return;} } catch (Exception e) { e.printStackTrace(); } }
这是一个 Java 语言的方法,其目的是关闭程序并询问用户是否需要保存文件。具体解释如下:
1. `void` 表示该方法不返回任何值。
2. `close()` 是该方法的名称。
3. `try` 表示该方法中包含可能会抛出异常的代码块,需要进行异常处理。
4. `int option = JOptionPane.showConfirmDialog(null,"是否保存", "记事本提示",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,null);` 是一个弹出对话框,询问用户是否需要保存文件。其中参数含义如下:
- `null` 表示对话框的父窗口为 null,即居中显示。
- `"是否保存"` 是对话框的标题。
- `"记事本提示"` 是对话框的消息内容。
- `JOptionPane.YES_NO_CANCEL_OPTION` 表示对话框中有“是”、“否”、“取消”三个按钮。
- `JOptionPane.WARNING_MESSAGE` 表示对话框的图标为警告图标。
- `null` 表示对话框的选项不需要自定义。
5. `switch (option) { case JOptionPane.YES_OPTION:saveFile();System.exit(0);break; case JOptionPane.NO_OPTION:System.exit(0);break; case JOptionPane.CANCEL_OPTION:return;}` 是根据用户选择的不同,执行不同的操作。其中:
- `JOptionPane.YES_OPTION` 表示用户选择了“是”。
- `saveFile()` 是保存文件的方法。
- `System.exit(0)` 表示正常退出程序。
- `JOptionPane.NO_OPTION` 表示用户选择了“否”。
- `JOptionPane.CANCEL_OPTION` 表示用户选择了“取消”。
- `return` 表示退出该方法,但不退出程序。
6. `catch (Exception e) { e.printStackTrace(); }` 表示如果有异常发生,则打印异常信息。
记事本java代码实现新建打开保存另存为退出
好的,以下是一个简单的记事本 Java 代码,实现了新建、打开、保存、另存为和退出功能:
```java
import java.awt.BorderLayout;
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.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Notepad extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private JFileChooser fileChooser;
private String fileName;
public Notepad() {
super("记事本");
// 设置菜单栏
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem newMenuItem = new JMenuItem("新建");
JMenuItem openMenuItem = new JMenuItem("打开");
JMenuItem saveMenuItem = new JMenuItem("保存");
JMenuItem saveAsMenuItem = new JMenuItem("另存为");
JMenuItem exitMenuItem = new JMenuItem("退出");
newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
saveAsMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// 设置文本区域
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(scrollPane, BorderLayout.CENTER);
// 设置文件选择器
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("文本文件(*.txt)", "txt"));
// 设置窗口
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new Notepad();
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "新建":
newFile();
break;
case "打开":
openFile();
break;
case "保存":
saveFile();
break;
case "另存为":
saveAsFile();
break;
case "退出":
exit();
break;
}
}
// 新建文件
private void newFile() {
if (textArea.getText().length() > 0) {
int option = JOptionPane.showConfirmDialog(this, "是否保存更改?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
if (option == JOptionPane.YES_OPTION) {
saveFile();
} else if (option == JOptionPane.CANCEL_OPTION) {
return;
}
}
textArea.setText("");
fileName = null;
}
// 打开文件
private void openFile() {
if (textArea.getText().length() > 0) {
int option = JOptionPane.showConfirmDialog(this, "是否保存更改?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
if (option == JOptionPane.YES_OPTION) {
saveFile();
} else if (option == JOptionPane.CANCEL_OPTION) {
return;
}
}
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File file = fileChooser.getSelectedFile();
fileName = file.getAbsolutePath();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
reader.close();
textArea.setText(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 保存文件
private void saveFile() {
if (fileName == null) {
saveAsFile();
} else {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(textArea.getText());
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 另存为文件
private void saveAsFile() {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File file = fileChooser.getSelectedFile();
if (!file.getName().endsWith(".txt")) {
file = new File(file.getAbsolutePath() + ".txt");
}
fileName = file.getAbsolutePath();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 退出程序
private void exit() {
if (textArea.getText().length() > 0) {
int option = JOptionPane.showConfirmDialog(this, "是否保存更改?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
if (option == JOptionPane.YES_OPTION) {
saveFile();
} else if (option == JOptionPane.CANCEL_OPTION) {
return;
}
}
System.exit(0);
}
}
```
该记事本实现了新建、打开、保存、另存为和退出功能,可以选择文本文件进行编辑。