效果展示:
源码如下:
package 表白墙;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
public class Test1 extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test1() {
setTitle("\u8868\u767D\u5899");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(400, 200, 563, 449);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setFont(new Font("宋体", Font.BOLD, 16));
textField.setBackground(new Color(255, 255, 204));
textField.setBounds(185, 87, 188, 30);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setFont(new Font("宋体", Font.BOLD, 16));
textField_1.setBackground(new Color(204, 255, 204));
textField_1.setBounds(185, 140, 188, 30);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setFont(new Font("宋体", Font.BOLD, 16));
textField_2.setBackground(new Color(255, 204, 204));
textField_2.setBounds(185, 198, 188, 30);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblNewLabel = new JLabel("\u8C01\u60F3\u8BF4");
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 18));
lblNewLabel.setBounds(97, 90, 69, 27);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u5BF9\u8C01\u8BF4");
lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 18));
lblNewLabel_1.setBounds(97, 143, 69, 27);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u8BF4\u4EC0\u4E48");
lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 18));
lblNewLabel_2.setBounds(97, 201, 69, 27);
contentPane.add(lblNewLabel_2);
JButton btnNewButton = new JButton("\u53D1\u9001");
btnNewButton.setBackground(new Color(153, 255, 255));
btnNewButton.setFont(new Font("宋体", Font.BOLD, 19));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
File file = new File("D:/Test/biaobai.txt");
if (!file.exists()) {
file.createNewFile();
} else {
BufferedWriter br = new BufferedWriter(new FileWriter(file, true));
String x1 = textField.getText();
String x2 = textField_1.getText();
String x3 = textField_2.getText();
if (x1.equals("") || x2.equals("") || x3.equals("")) {
JOptionPane.showConfirmDialog(null, "不能有空!","提示",JOptionPane.PLAIN_MESSAGE);
} else {
br.write(x1 + "想对" + x2 + "说:" + x3 + "\n");
br.close();
textField.setText("");
textField_1.setText("");
textField_2.setText("");
}
}
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(197, 277, 116, 38);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u67E5\u770B\u8868\u767D\u8BB0\u5F55");
btnNewButton_1.setBackground(new Color(102, 153, 255));
btnNewButton_1.setFont(new Font("宋体", Font.BOLD, 12));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test1.this.dispose();
Test2 test2 = new Test2();
test2.setVisible(true);
try {
test2.xianshi();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
btnNewButton_1.setBounds(430, 10, 111, 23);
contentPane.add(btnNewButton_1);
}
}
package 表白墙;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.StyledDocument;
import javax.swing.JEditorPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.Color;
public class Test2 extends JFrame {
private JPanel contentPane;
JTextPane textPane;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// Test2 frame = new Test2();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame
*/
public Test2() {
setTitle("\u8868\u767D\u5899");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(450, 100, 446, 647);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("\u5173\u95ED");
btnNewButton.setBackground(new Color(51, 153, 255));
btnNewButton.setFont(new Font("宋体", Font.BOLD, 19));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test1 test1 = new Test1();
test1.setVisible(true);
Test2.this.dispose();
}
});
btnNewButton.setBounds(159, 559, 93, 43);
contentPane.add(btnNewButton);
textPane = new JTextPane();
textPane.setFont(new Font("宋体", Font.BOLD, 18));
textPane.setBounds(10, 10, 414, 520);
contentPane.add(textPane);
textPane.setEditable(false);
}
public void xianshi() throws IOException {
File file = new File("D:/Test/biaobai.txt");
if (!file.exists()) {
file.createNewFile();
} else {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
StringBuilder str=new StringBuilder();
while ((line = br.readLine()) != null) {
str.append(line+"\n");
}
textPane.setText(str.toString());
br.close();
}
}
}