演示图:
如图,使用输入框形式将信息保存为JSON格式数据文件,打开文件
上面工具栏分别为:新建、打开和保存按钮
准备工作:
导入json.jar,用于操作JSON数据格式,资源链接:json.jar
导入Af开头的工具包(这步骤可以省略,主要用于方便布局和对JSON数据进行操作,如果没有请自行解决)
导入Image图片资源,用于上方工具栏
上代码:
import java.awt.BorderLayout;
import java.io.File;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.json.JSONObject;
import af.common.json.AfJSON;
import af.swing.AfPanel;
import af.swing.layout.AfColumnLayout;
import af.swing.layout.AfRowLayout;
public class MyFrame extends JFrame
{
JTextField nameField = new JTextField(20);
JTextField idField = new JTextField(20);
JComboBox<String> sexField = new JComboBox<>();
JTextField phoneField = new JTextField(20);
public MyFrame(String title)
{
super(title);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
//添加中间组件
initMainPanle();
//添加工具栏
initToolBar();
}
public void initMainPanle()
{
//创建布局器
AfPanel main = new AfPanel();
this.getContentPane().add(main, BorderLayout.CENTER);
main.padding(20);
//纵向布局
main.setLayout(new AfColumnLayout(10));
AfPanel row1 = new AfPanel();
row1.setLayout(new AfRowLayout(10));
row1.add(new JLabel("姓名"), "50px");
row1.add(nameField, "1w");
AfPanel row2 = new AfPanel();
row2.setLayout(new AfRowLayout(10));
row2.add(new JLabel("学号"), "50px");
row2.add(idField, "1w");
AfPanel row3 = new AfPanel();
row3.setLayout(new AfRowLayout(10));
row3.add(new JLabel("性别"), "50px");
row3.add(sexField, "1w");
sexField.addItem("男");
sexField.addItem("女");
AfPanel row4 = new AfPanel();
row4.setLayout(new AfRowLayout(10));
row4.add(new JLabel("电话"), "50px");
row4.add(phoneField, "1w");
main.add(row1);
main.add(row2);
main.add(row3);
main.add(row4);
}
private void initToolBar()
{
JToolBar toolBar = new JToolBar();
this.getContentPane().add(toolBar, BorderLayout.PAGE_START);
JButton newButton = createButton("image_new.png", "新建");
JButton openButton = createButton("image_open.png", "打开");
JButton saveButton = createButton("image_save.png", "保存");
toolBar.add(newButton);
toolBar.add(openButton);
toolBar.add(saveButton);
//添加按钮点击事件
newButton.addActionListener(e->{
doNew();
});
openButton.addActionListener(e->{
doOpen();
});
saveButton.addActionListener(e->{
doSave();
});
}
//创建图标按钮
private JButton createButton(String filePath, String toolText)
{
JButton button = new JButton();
button.setToolTipText(toolText);
button.setFocusable(false);
String file = "/images/" + filePath;
URL url = getClass().getResource(file);
//加载图片资源
button.setIcon(new ImageIcon(url));
return button;
}
//新建(清空文本)
private void doNew()
{
nameField.setText("");
idField.setText("");
phoneField.setText("");
sexField.setSelectedIndex(0);
}
//打开(读取JSON文件)
private void doOpen()
{
//创建文件选择器(对话框)
JFileChooser chooser = new JFileChooser();
//创建文件过滤器
FileNameExtensionFilter filter = new FileNameExtensionFilter("JSON", "json", "stu");
//过滤文件
chooser.setFileFilter(filter);
//打开对话框
int reg = chooser.showOpenDialog(this);
if(reg == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
try
{
//使用AFJSON工具类读取文件
String message = AfJSON.fromFile(file, "utf-8").toString();
//解析JSON格式文件
JSONObject j = new JSONObject(message);
String name = j.getString("name");
String id = j.getString("id");
boolean sex = j.getBoolean("sex");
String phone = j.getString("phone");
//给每一个控件赋值
nameField.setText(name);
idField.setText(id);
if(sex)
sexField.setSelectedIndex(0);
else
sexField.setSelectedIndex(1);
phoneField.setText(phone);
JOptionPane.showMessageDialog(this, "打开成功");
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(this, "出错");
}
}
}
//保存(写入JSON文件)
private void doSave()
{
String name = nameField.getText().trim(); //trim()方法去除字符串两边的空白
String id = idField.getText().trim();
String phone = phoneField.getText().trim();
boolean sex = (sexField.getSelectedIndex() == 0);
//判断是否有空值
if(name.isEmpty() || id.isEmpty() || phone.isEmpty())
{
JOptionPane.showMessageDialog(this, "输入不得为空!");
return;
}
//创建JSON对象
//转成JSON格式
JSONObject j = new JSONObject();
j.put("name", name);
j.put("id", id);
j.put("phone", phone);
j.put("sex", sex);
//打开文件选择对话框
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JSON", "json");
chooser.setFileFilter(filter);
int reg = chooser.showSaveDialog(this);
if(reg == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
//判断文件后缀
if(!file.getName().endsWith("stu"))
{
//将文件后缀改为.stu
String filePath = file.getAbsolutePath() + ".stu";
file = new File(filePath);
}
try
{
AfJSON.toFile(j, file, "UTF-8");
JOptionPane.showMessageDialog(this, "保存成功");
//清空显示
doNew();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(this, "出错");
}
}
}
}
在这里,我要感谢@邵发老师,里面Af打头的工具类都是他写的,我也是参考他的教程做出来的。
|
请发表评论