如何将 JLabel 的 color/size 保存在文件中?
How can I save the color/size of a JLabel in a file?
我正在尝试将 JLabel 的颜色、文本和大小 (gridheight/gridwidth) 保存在文件中。因此,每当程序运行时,我都可以查看过去的 JLabel。
我知道如何 read/write 将字符串写入文件,但是否可以保存 JLabel 的状态?
您必须定义您需要保存什么,以及如何。
保存什么
您需要定义对您重要的内容,例如颜色可以是有或没有透明度信息。
文本可以是简单的字符串或包含字符类型、大小、装饰(粗体、下划线...)等信息的对象。
如何保存
您需要为每个信息定义一种格式(例如颜色可以用其名称 "red"
或其十六进制值 "#FF0000"
保存,或者可以是对自定义变量的引用 "mainColorTemplate1"
).
您还需要定义文件格式(xml、json、自定义二进制文件、属性 文件、yaml...)。
我建议根据您的知识尝试最简单的解决方案。将所有数据保存在一个对象中并使用 GSon 或 Faster Jackson 等库将其保存为 JSon 非常简单。
这是 json 中可能的格式:
{
"labels": [
{
"text":"My first label",
"x":2,
"y":1,
"color":"#FF0000"
},
{
"text":"My Second label",
"x":4,
"y":3,
"color":"#FFFF00"
}
]
}
一种方法是利用对象 Serialization. All Swing components implement Serializable interface. You can save the component and the next time you run the application, the component will be as you left it. Two interesting questions to read about this are: Why is Java Swing serializable? and Swing components and serialization.
JLabel 是一个序列化对象,您可以使用 ObjectOutputStream 将整个 JLabel 对象保存在一个文件中,然后从 [=22] 中读取它=]ObjectInputStream。像这样。
根据数据对象更新了之前的代码CLASS:
public static void main(String[] args) {
// Creating a JLabel
JLabel label = new JLabel("REHAN JAVED");
label.setForeground(Color.RED);
label.setSize(new Dimension(500, 500));
// adding it into the file.
addItIntoFile(new DataObject(label, 200, 50, 0, 1));
// reading file..
DataObject dataObj = readDataObject();
JLabel newLabel = dataObj.getLabel();
int x = dataObj.getXPosition();
// and take all the data from getters.
System.out.println(newLabel.getText()+"\n"+newLabel.getForeground()+"\n"+label.getSize());
}
public static void addItIntoFile(DataObject dataObj) {
File file = new File("data.txt");
try {
file.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(dataObj);
oos.close();
// You can handle the different exceptions according to you.
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataObject readDataObject() {
DataObject dataObj = null;
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("data.txt")));
dataObj = (DataObject) ois.readObject();
ois.close();
// You can handle the different exceptions according to you.
} catch (Exception e) {
e.printStackTrace();
}
// Handling null data..
if(dataObj == null) {
dataObj = new DataObject(new JLabel(), 0, 0, 0, 0);
}
return dataObj;
}
它会很好用。 :)
更新版本:
要包含宽度、高度、x 和 y 位置等网格约束,请创建一个新的 class 并使用它实现 Serializable 接口并直接存储它进入文件。
public class DataObject implements Serializable {
private JLabel label;
private int gridWidth;
private int gridHeight;
private int gridXPosition;
private int gridYPosition;
// Your Constructor..
public DataObject(JLabel label, int gridWidth, int gridHeight,
int gridXPosition, int gridYPosition) {
this.label = label;
this.gridWidth = gridWidth;
this.gridHeight = gridHeight;
this.gridXPosition = gridXPosition;
this.gridYPosition = gridYPosition;
}
// your getter and setters...
}
我正在尝试将 JLabel 的颜色、文本和大小 (gridheight/gridwidth) 保存在文件中。因此,每当程序运行时,我都可以查看过去的 JLabel。
我知道如何 read/write 将字符串写入文件,但是否可以保存 JLabel 的状态?
您必须定义您需要保存什么,以及如何。
保存什么
您需要定义对您重要的内容,例如颜色可以是有或没有透明度信息。
文本可以是简单的字符串或包含字符类型、大小、装饰(粗体、下划线...)等信息的对象。
如何保存
您需要为每个信息定义一种格式(例如颜色可以用其名称 "red"
或其十六进制值 "#FF0000"
保存,或者可以是对自定义变量的引用 "mainColorTemplate1"
).
您还需要定义文件格式(xml、json、自定义二进制文件、属性 文件、yaml...)。
我建议根据您的知识尝试最简单的解决方案。将所有数据保存在一个对象中并使用 GSon 或 Faster Jackson 等库将其保存为 JSon 非常简单。
这是 json 中可能的格式:
{
"labels": [
{
"text":"My first label",
"x":2,
"y":1,
"color":"#FF0000"
},
{
"text":"My Second label",
"x":4,
"y":3,
"color":"#FFFF00"
}
]
}
一种方法是利用对象 Serialization. All Swing components implement Serializable interface. You can save the component and the next time you run the application, the component will be as you left it. Two interesting questions to read about this are: Why is Java Swing serializable? and Swing components and serialization.
JLabel 是一个序列化对象,您可以使用 ObjectOutputStream 将整个 JLabel 对象保存在一个文件中,然后从 [=22] 中读取它=]ObjectInputStream。像这样。
根据数据对象更新了之前的代码CLASS:
public static void main(String[] args) {
// Creating a JLabel
JLabel label = new JLabel("REHAN JAVED");
label.setForeground(Color.RED);
label.setSize(new Dimension(500, 500));
// adding it into the file.
addItIntoFile(new DataObject(label, 200, 50, 0, 1));
// reading file..
DataObject dataObj = readDataObject();
JLabel newLabel = dataObj.getLabel();
int x = dataObj.getXPosition();
// and take all the data from getters.
System.out.println(newLabel.getText()+"\n"+newLabel.getForeground()+"\n"+label.getSize());
}
public static void addItIntoFile(DataObject dataObj) {
File file = new File("data.txt");
try {
file.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(dataObj);
oos.close();
// You can handle the different exceptions according to you.
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataObject readDataObject() {
DataObject dataObj = null;
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("data.txt")));
dataObj = (DataObject) ois.readObject();
ois.close();
// You can handle the different exceptions according to you.
} catch (Exception e) {
e.printStackTrace();
}
// Handling null data..
if(dataObj == null) {
dataObj = new DataObject(new JLabel(), 0, 0, 0, 0);
}
return dataObj;
}
它会很好用。 :)
更新版本:
要包含宽度、高度、x 和 y 位置等网格约束,请创建一个新的 class 并使用它实现 Serializable 接口并直接存储它进入文件。
public class DataObject implements Serializable {
private JLabel label;
private int gridWidth;
private int gridHeight;
private int gridXPosition;
private int gridYPosition;
// Your Constructor..
public DataObject(JLabel label, int gridWidth, int gridHeight,
int gridXPosition, int gridYPosition) {
this.label = label;
this.gridWidth = gridWidth;
this.gridHeight = gridHeight;
this.gridXPosition = gridXPosition;
this.gridYPosition = gridYPosition;
}
// your getter and setters...
}