如何在 JLabel 和 Jext Box 中动态显示数据 java 从属性文件动态摆动 不知道属性文件中的数据



我正在研究Java Swing模块,我想在其中将数据返回到Java Swing文本框和标签中。

文本框和JLabel应该根据数据检索动态变化,我不知道属性名称及其值......但是我必须在不知道属性名称及其值的情况下从属性文件中检索数据 JlabelJtextboxes.

。它们应该根据数据而变化...就像属性名称应该进入Jlabel并且它的值应该进入Jtextboxes......

我使用了收集框架的设置接口,所以我将属性文件中的所有数据都获取到其键和键的值中...但我不知道如何在JlabelJTextBox中显示它

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 和文本框也会根据属性增加*/

好的

,所以你在键/值对中有一个属性,假设键代表标签和值代表文本,你可以使用 propertyNames 来获取枚举并迭代列表......这将允许您创建标签/字段。

    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);

但是您可能需要另一个将键/标签映射到字段的映射,假设您想再次保存值......

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中的键/值对),它会简单得多

有关更多详细信息,请参阅如何使用表

最新更新