演示圖:
如圖,使用輸入框形式將信息保存爲JSON格式數據文件,打開文件
上面工具欄分別爲:新建、打開和保存按鈕
準備工作:
導入json.jar,用於操作JSON數據格式,資源鏈接請自行百度,我的資源還沒有審覈通過
導入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打頭的工具類都是他寫的,我也是參考他的教程做出來的。