如何在 JLabel 和 Jext 框中显示数据 java 在不知道属性文件中的数据的情况下从属性文件动态摆动
How to show Data in JLabel and Jext Boxes java swing dynamically from properties file Without knowing data in properties file
我正在开发一个 java swing 模块,我想在其中 return 将数据 java swing 文本框和标签。
文本框和 JLabel
应根据数据检索动态变化,我不知道属性名称及其值...但我必须在不知道属性文件的情况下从属性文件中检索数据属性名称及其值转换为 Jlabel
和 Jtextboxes
.
...并且它们应该根据数据而变化...例如 属性 名称应该进入 Jlabel
并且它的值应该进入 Jtextboxes
...
我使用集合框架的集合接口,所以我从属性文件中获取所有数据到它的键和键值中...但我不知道如何在 Jlabel
和 [=19 中显示它=]s
public class ConfigSwingDemo extends JFrame
{
private File configFile = new File("momark.properties");
private Properties configProps;
private JButton buttonSave = new JButton("Save");
static List<JLabel> listOfLabels = new ArrayList<JLabel>();
static List<JTextField> listOfTextFields = new ArrayList<JTextField>();
public ConfigSwingDemo()
{
super("Properties Configuration Demo");
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 5, 10);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridy = 1;
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
add(buttonSave, constraints);
buttonSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
saveProperties();
JOptionPane.showMessageDialog(ConfigSwingDemo.this,
"Properties were saved successfully!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(ConfigSwingDemo.this,
"Error saving properties file: " + ex.getMessage());
}
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
try {
loadProperties();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "The momark.properties file does not exist, default properties loaded.");
}
Set<Object> keys = configProps.keySet();
for(Object k:keys){
String key = (String)k;
System.out.println(key+": "+configProps.getProperty(key));
}
}
/////////////////
private void loadProperties() throws IOException {
Properties defaultProps = new Properties();
// sets default properties
configProps = new Properties(defaultProps);
// loads properties from file
InputStream inputStream = new FileInputStream(configFile);
configProps.load(inputStream);
inputStream.close();
}
private void saveProperties() throws IOException {
//configProps.setProperty("server.url", textUrl.getText());
OutputStream outputStream = new FileOutputStream(configFile);
configProps.store(outputStream, "properties setttings");
outputStream.close();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ConfigSwingDemo();
}
});
}
}
/*输出控制台是:
property1 : value1
property2 : value2
property3 : value3
property4 : value4
我想在不知道属性数据的情况下动态显示 Jlabel 和 Jtext 字段中的输出..所以如果属性增加,JLabels 和文本框也会根据属性增加*/
好的,所以你有一个 key/value 对的属性,假设键代表标签,值代表文本,你可以使用 propertyNames 来获取枚举并遍历列表......将允许您创建 labels/fields。
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 5, 10);
constraints.anchor = GridBagConstraints.WEST;
Set<Object> keys = configProps.keySet();
for (Object k : keys) {
JLabel label = new JLabel(k.toString());
JTextField field = new JTextField(configProps.getProperty(k.toString()), 10);
constraints.gridx = 0;
add(label, constraints);
constraints.gridx++;
add(field, constraints);
constraints.gridy++;
}
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
add(buttonSave, constraints);
但是您可能需要另一个将 keys/labels 映射到字段的地图,假设您想要再次保存值...
public class ConfigSwingDemo extends JFrame {
private Map<String, JTextField> fieldsMap = new HashMap<>(25);
//...
for (Object k : keys) {
JLabel label = new JLabel(k.toString());
JTextField field = new JTextField(configProps.getProperty(k.toString()), 10);
fieldsMap.put(k.toString(), field);
然后,我们要保存这些值,您可以使用类似...
configProps.clear();
for (String key : fieldsMap.keySet()) {
JTextField field = fieldsMap.get(key);
configProps.setProperty(key, field.getText());
}
将值复制回 Properties
并保存。
现在,说了这么多,我建议使用 JTable
,因为您已经有了一个基本模型(Properties
中的 key/value 对),它将是简单多了
有关详细信息,请参阅 How to Use Tables
我正在开发一个 java swing 模块,我想在其中 return 将数据 java swing 文本框和标签。
文本框和 JLabel
应根据数据检索动态变化,我不知道属性名称及其值...但我必须在不知道属性文件的情况下从属性文件中检索数据属性名称及其值转换为 Jlabel
和 Jtextboxes
.
...并且它们应该根据数据而变化...例如 属性 名称应该进入 Jlabel
并且它的值应该进入 Jtextboxes
...
我使用集合框架的集合接口,所以我从属性文件中获取所有数据到它的键和键值中...但我不知道如何在 Jlabel
和 [=19 中显示它=]s
public class ConfigSwingDemo extends JFrame
{
private File configFile = new File("momark.properties");
private Properties configProps;
private JButton buttonSave = new JButton("Save");
static List<JLabel> listOfLabels = new ArrayList<JLabel>();
static List<JTextField> listOfTextFields = new ArrayList<JTextField>();
public ConfigSwingDemo()
{
super("Properties Configuration Demo");
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 5, 10);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridy = 1;
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
add(buttonSave, constraints);
buttonSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
saveProperties();
JOptionPane.showMessageDialog(ConfigSwingDemo.this,
"Properties were saved successfully!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(ConfigSwingDemo.this,
"Error saving properties file: " + ex.getMessage());
}
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
try {
loadProperties();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "The momark.properties file does not exist, default properties loaded.");
}
Set<Object> keys = configProps.keySet();
for(Object k:keys){
String key = (String)k;
System.out.println(key+": "+configProps.getProperty(key));
}
}
/////////////////
private void loadProperties() throws IOException {
Properties defaultProps = new Properties();
// sets default properties
configProps = new Properties(defaultProps);
// loads properties from file
InputStream inputStream = new FileInputStream(configFile);
configProps.load(inputStream);
inputStream.close();
}
private void saveProperties() throws IOException {
//configProps.setProperty("server.url", textUrl.getText());
OutputStream outputStream = new FileOutputStream(configFile);
configProps.store(outputStream, "properties setttings");
outputStream.close();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ConfigSwingDemo();
}
});
}
}
/*输出控制台是:
property1 : value1
property2 : value2
property3 : value3
property4 : value4
我想在不知道属性数据的情况下动态显示 Jlabel 和 Jtext 字段中的输出..所以如果属性增加,JLabels 和文本框也会根据属性增加*/
好的,所以你有一个 key/value 对的属性,假设键代表标签,值代表文本,你可以使用 propertyNames 来获取枚举并遍历列表......将允许您创建 labels/fields。
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 5, 10);
constraints.anchor = GridBagConstraints.WEST;
Set<Object> keys = configProps.keySet();
for (Object k : keys) {
JLabel label = new JLabel(k.toString());
JTextField field = new JTextField(configProps.getProperty(k.toString()), 10);
constraints.gridx = 0;
add(label, constraints);
constraints.gridx++;
add(field, constraints);
constraints.gridy++;
}
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
add(buttonSave, constraints);
但是您可能需要另一个将 keys/labels 映射到字段的地图,假设您想要再次保存值...
public class ConfigSwingDemo extends JFrame {
private Map<String, JTextField> fieldsMap = new HashMap<>(25);
//...
for (Object k : keys) {
JLabel label = new JLabel(k.toString());
JTextField field = new JTextField(configProps.getProperty(k.toString()), 10);
fieldsMap.put(k.toString(), field);
然后,我们要保存这些值,您可以使用类似...
configProps.clear();
for (String key : fieldsMap.keySet()) {
JTextField field = fieldsMap.get(key);
configProps.setProperty(key, field.getText());
}
将值复制回 Properties
并保存。
现在,说了这么多,我建议使用 JTable
,因为您已经有了一个基本模型(Properties
中的 key/value 对),它将是简单多了
有关详细信息,请参阅 How to Use Tables