用JAVA语言写出1.生成一个简单文本编辑器,将生成的文本存入文件,如果有打开、存盘、关闭等菜单更佳。GUI界面可以自己灵活设计,同时实现窗体关闭退出功能。 2.编程实现一个加减法计算器。可以使用鼠标来完成加减法
时间: 2024-01-21 16:19:55 浏览: 63
1. 生成一个简单文本编辑器
代码如下:
```java
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class TextEditor extends JFrame implements ActionListener {
private JTextArea textArea;
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem openMenuItem, saveMenuItem, closeMenuItem;
public TextEditor() {
super("Text Editor");
initUI();
}
private void initUI() {
// 创建文本区域
textArea = new JTextArea();
// 创建菜单栏
menuBar = new JMenuBar();
fileMenu = new JMenu("文件");
// 创建菜单项
openMenuItem = new JMenuItem("打开");
saveMenuItem = new JMenuItem("保存");
closeMenuItem = new JMenuItem("关闭");
// 注册事件监听器
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
closeMenuItem.addActionListener(this);
// 将菜单项添加到菜单上
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(closeMenuItem);
// 将菜单添加到菜单栏上
menuBar.add(fileMenu);
// 设置窗口的菜单栏
setJMenuBar(menuBar);
// 将文本区域添加到窗口上
add(new JScrollPane(textArea));
// 设置窗口大小和关闭方式
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == openMenuItem) {
// 打开文件
openFile();
} else if (source == saveMenuItem) {
// 保存文件
saveFile();
} else if (source == closeMenuItem) {
// 关闭窗口
dispose();
}
}
private void openFile() {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件", "txt");
fileChooser.setFileFilter(filter);
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
reader.close();
textArea.setText(sb.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void saveFile() {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件", "txt");
fileChooser.setFileFilter(filter);
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 (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
TextEditor editor = new TextEditor();
editor.setVisible(true);
}
}
```
2. 编程实现一个加减法计算器
代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField inputField;
private JButton addButton, subButton;
private int result = 0;
public Calculator() {
super("Calculator");
initUI();
}
private void initUI() {
// 创建文本框和按钮
inputField = new JTextField(10);
addButton = new JButton("+");
subButton = new JButton("-");
// 注册事件监听器
addButton.addActionListener(this);
subButton.addActionListener(this);
// 创建面板,并将组件添加到面板上
JPanel panel = new JPanel();
panel.add(inputField);
panel.add(addButton);
panel.add(subButton);
// 将面板添加到窗口上
add(panel);
// 设置窗口大小和关闭方式
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String inputStr = inputField.getText();
if (inputStr.isEmpty()) {
return;
}
int inputNum = Integer.parseInt(inputStr);
if (source == addButton) {
result += inputNum;
} else if (source == subButton) {
result -= inputNum;
}
inputField.setText("");
JOptionPane.showMessageDialog(this, "结果为:" + result);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
阅读全文