用JAVA语言写出1.生成一个简单文本编辑器,将生成的文本存入文件,如果有打开、存盘、关闭等菜单更佳。GUI界面可以自己灵活设计,同时实现窗体关闭退出功能。 2.编程实现一个加减法计算器。可以使用鼠标来完成加减法运算。
时间: 2023-12-03 21:47:35 浏览: 141
1. 简单文本编辑器
可以使用JAVA Swing库来实现一个简单的文本编辑器。以下是一个基本的代码示例:
```
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileSystemView;
public class TextEditor extends JFrame implements ActionListener {
private JTextArea textArea;
private JButton saveButton;
private JButton openButton;
private JButton closeButton;
public TextEditor() {
initUI();
}
private void initUI() {
setTitle("Text Editor");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
saveButton = new JButton("Save");
saveButton.addActionListener(this);
panel.add(saveButton);
openButton = new JButton("Open");
openButton.addActionListener(this);
panel.add(openButton);
closeButton = new JButton("Close");
closeButton.addActionListener(this);
panel.add(closeButton);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(panel, BorderLayout.NORTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveButton) {
saveFile();
} else if (e.getSource() == openButton) {
openFile();
} else if (e.getSource() == closeButton) {
dispose();
}
}
private void saveFile() {
JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = fileChooser.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(selectedFile));
writer.write(textArea.getText());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void openFile() {
JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
textArea.setText("");
textArea.read(new java.io.FileReader(selectedFile), null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
TextEditor editor = new TextEditor();
editor.setVisible(true);
});
}
}
```
该代码创建了一个包含文本区域和三个按钮的窗口。用户可以在文本区域中输入文本,并使用“保存”按钮将其保存到文件中,使用“打开”按钮打开已经保存的文件,使用“关闭”按钮关闭窗口。
2. 简单加减法计算器
以下是一个基本的代码示例:
```
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private JTextField inputField;
private JButton addButton;
private JButton subtractButton;
private JButton equalsButton;
private JButton clearButton;
private JButton[] digitButtons;
private Stack<Integer> stack;
private int currentNumber;
private boolean isAdding;
public Calculator() {
initUI();
}
private void initUI() {
setTitle("Calculator");
setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
inputField = new JTextField();
inputField.setEditable(false);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 3));
digitButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
digitButtons[i] = new JButton(i + "");
digitButtons[i].addActionListener(this);
buttonPanel.add(digitButtons[i]);
}
addButton = new JButton("+");
addButton.addActionListener(this);
buttonPanel.add(addButton);
subtractButton = new JButton("-");
subtractButton.addActionListener(this);
buttonPanel.add(subtractButton);
equalsButton = new JButton("=");
equalsButton.addActionListener(this);
buttonPanel.add(equalsButton);
clearButton = new JButton("C");
clearButton.addActionListener(this);
buttonPanel.add(clearButton);
getContentPane().add(inputField, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
stack = new Stack<Integer>();
currentNumber = 0;
isAdding = true;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearButton) {
inputField.setText("");
stack.clear();
currentNumber = 0;
isAdding = true;
} else if (e.getSource() == addButton) {
updateNumberStack();
isAdding = true;
} else if (e.getSource() == subtractButton) {
updateNumberStack();
isAdding = false;
} else if (e.getSource() == equalsButton) {
updateNumberStack();
int result = 0;
while (!stack.isEmpty()) {
result += stack.pop();
}
inputField.setText(result + "");
currentNumber = 0;
isAdding = true;
} else {
int digit = Integer.parseInt(e.getActionCommand());
currentNumber = currentNumber * 10 + digit;
inputField.setText(currentNumber + "");
}
}
private void updateNumberStack() {
if (currentNumber != 0) {
if (isAdding) {
stack.push(currentNumber);
} else {
stack.push(-currentNumber);
}
currentNumber = 0;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Calculator calculator = new Calculator();
calculator.setVisible(true);
});
}
}
```
该代码创建了一个包含数字按钮、加号、减号、等号和清除按钮的窗口。用户可以使用数字按钮输入数字,并使用加号和减号完成加减法运算,使用等号计算结果,使用清除按钮清除输入。计算器使用一个栈来保存输入的数字和运算符。
阅读全文