效果展示:
代码如下:
package 暑假io;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
public class io {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JLabel lblNewLabel_1;
private JLabel lblNewLabel_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
io window = new io();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public io() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 728, 488);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBackground(new Color(255, 204, 255));
textField.setBounds(273, 246, 252, 38);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBackground(new Color(255, 255, 204));
textField_1.setForeground(Color.BLACK);
textField_1.setBounds(273, 119, 126, 38);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
lblNewLabel_1 = new JLabel("\u5BF9\u8C01\u8BF4");
lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 18));
lblNewLabel_1.setBounds(171, 180, 86, 35);
frame.getContentPane().add(lblNewLabel_1);
JTextField textField_2 = new JTextField();
textField_2.setBackground(new Color(204, 255, 204));
textField_2.setBounds(273, 179, 126, 38);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
lblNewLabel_2 = new JLabel("\u8C01\u60F3\u8BF4");
lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 18));
lblNewLabel_2.setBounds(169, 120, 86, 35);
frame.getContentPane().add(lblNewLabel_2);
JLabel lblNewLabel = new JLabel("\u60F3\u8BF4\u7684\u8BDD");
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 18));
lblNewLabel.setBounds(171, 245, 84, 38);
frame.getContentPane().add(lblNewLabel);
JButton btnNewButton = new JButton("\u4FDD\u5B58");
btnNewButton.setBackground(new Color(255, 204, 51));
btnNewButton.setFont(new Font("宋体", Font.BOLD, 19));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String t1=textField_1.getText();
String t2=textField_2.getText();
String string=textField.getText();
if(t1.equals("")||t2.equals("")||string.equals("")) {
JOptionPane.showConfirmDialog(null,"输入框不能为空!","提示",JOptionPane.ERROR_MESSAGE) ;
}
else{
textField.setText("");
textField_1.setText("");
textField_2.setText("");
try {
Writer wo=new FileWriter("D:/Test/x1.txt",true);
wo.write(t1+"想对"+t2+"说:"+string+"\r");
wo.close();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}}
}
});
btnNewButton.setBounds(261, 330, 119, 38);
frame.getContentPane().add(btnNewButton);
}
}
本人小白,展示了基础代码,图形化界面是eclipse的GUI插件