这是一个基础的记事本程序,其中包含添加文字水印的功能。用户可以在添加水印菜单中输入要添加的水印文字,程序将在文本框中截图并添加水印,最后将图片保存为jpg格式的文件。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class NotepadWithWatermark extends JFrame implements ActionListener{
private JTextArea textArea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane(textArea);
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("文件");
private JMenuItem openMenuItem = new JMenuItem("打开");
private JMenuItem saveMenuItem = new JMenuItem("保存");
private JMenu editMenu = new JMenu("编辑");
private JMenuItem copyMenuItem = new JMenuItem("复制");
private JMenuItem pasteMenuItem = new JMenuItem("粘贴");
private JMenuItem cutMenuItem = new JMenuItem("剪切");
private JMenuItem watermarkMenuItem = new JMenuItem("添加水印");
public NotepadWithWatermark(){
this.setTitle("记事本");
this.setSize(500, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(menuBar);
this.getContentPane().add(scrollPane, BorderLayout.CENTER);
menuBar.add(fileMenu);
menuBar.add(editMenu);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(watermarkMenuItem);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
copyMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this);
watermarkMenuItem.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openMenuItem){
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件", "txt");
fileChooser.setFileFilter(filter);
int status = fileChooser.showOpenDialog(this);
if (status == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
try {
textArea.setText("");
textArea.append(FileUtil.readFile(file));
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "无法打开文件 " + file.getName());
}
}
}
else if(e.getSource() == saveMenuItem){
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件", "txt");
fileChooser.setFileFilter(filter);
int status = fileChooser.showSaveDialog(this);
if (status == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
try {
FileUtil.writeFile(file, textArea.getText());
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "无法保存文件 " + file.getName());
}
}
}
else if(e.getSource() == copyMenuItem){
textArea.copy();
}
else if(e.getSource() == pasteMenuItem){
textArea.paste();
}
else if(e.getSource() == cutMenuItem){
textArea.cut();
}
else if(e.getSource() == watermarkMenuItem){
String watermarkText = JOptionPane.showInputDialog(this, "请输入要添加的水印文字:");
BufferedImage image = new BufferedImage(textArea.getWidth(), textArea.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
textArea.paint(graphics2D);
graphics2D.setColor(Color.RED);
graphics2D.setFont(new Font("Arial", Font.BOLD, 24));
graphics2D.drawString(watermarkText, 10, 30);
graphics2D.dispose();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int status = fileChooser.showSaveDialog(this);
if (status == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
try {
ImageIO.write(image, "jpg", file);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "无法保存文件 " + file.getName());
}
}
}
}
public static void main(String[] args) {
NotepadWithWatermark notepad = new NotepadWithWatermark();
notepad.setVisible(true);
}
}
class FileUtil{
public static String readFile(File file) throws IOException{
StringBuilder stringBuilder = new StringBuilder();
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(file));
char[] buffer = new char[1024];
int readSize;
while ((readSize = reader.read(buffer)) != -1){
stringBuilder.append(buffer, 0, readSize);
}
reader.close();
return stringBuilder.toString();
}
public static void writeFile(File file, String content) throws IOException{
java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(file));
writer.write(content);
writer.flush();
writer.close();
}
}
用户可以在添加水印菜单中输入要添加的水印文字,程序将在文本框中截图并添加水印,最后将图片保存为jpg格式的文件。
关于代码的详细解释和实现细节,可以参考以下链接:
Java图形界面:使用JFrame创建一个简单的文本编辑器
Java图形界面:在JFrame中添加文本框
Java图形界面:在JFrame中添加菜单
Java图形界面:在JFrame中添加文件选择器
Java图形界面:在JFrame中添加水印工具