简体   繁体   English

如何判断其他课程何时更新?

[英]How can I tell when another class has been updated?

I am writing a Java program that displays a combo box that get's it's information from a properties file. 我正在编写一个Java程序,它显示一个组合框,从属性文件中获取它的信息。 There is a settings class that will allow the user to update the names of the fields that are in the combo box. 有一个设置类,允许用户更新组合框中字段的名称。 My problem is when the settings class is called and modified, I cannot figure out how to update the combo box with the new settings. 我的问题是当调用和修改设置类时,我无法弄清楚如何使用新设置更新组合框。 I have a method that will repaint the entire panel and re-load the combo box. 我有一个方法,将重新绘制整个面板并重新加载组合框。 But I do not know how to activate that method when the "apply" button is pressed in the setting class. 但是当我在设置类中按下“apply”按钮时,我不知道如何激活该方法。

Here is a rough and tough example of what I am trying to accomplish. 这是我想要完成的一个粗略而艰难的例子。

Main Class: 主类:

package testing;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Properties;

import javax.swing.*;

public class testConfigLoad extends JFrame
{
    JButton apply = new JButton("Apply");
JButton set   = new JButton("Settings");

    Properties          config      = new Properties();
    FileInputStream     fis         = null;
    FileOutputStream    fos         = null;
    final String        configFile  = "config.properties";

    OptPanel opt;

    public testConfigLoad() throws IOException
    {
        super("Test Program");
        setSize(200, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        config();
        opt = new OptPanel(config);
        buildFrame();
    }

    public void buildFrame()
    {
        set.addActionListener(new setListener());

        add(opt);
        add(apply);
        add(set);

        setVisible(true);
    }

    public void config() throws IOException
    {
        try
        {
            fis = new FileInputStream(configFile);
            config.load(fis);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File not found");
        }
        finally
        {
            if (fis != null)
            {
                fis.close();
            }
        }       
    }

    private class setListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            settings set = new settings(config);
        }
    }

    public static void main(String[] args) throws IOException
    {
        new testConfigLoad();
    }
} 

Panel that needs to be Refreshed: 需要刷新的面板:

package testing;

import java.util.Properties;
import javax.swing.*;

public class OptPanel extends JPanel
{
    String[] opts;
    JLabel optLabel = new JLabel("Available Options");
    Properties config;

    public OptPanel(Properties p)
    {
        config = p;
        opts = new String[3];
        buildPanel();
    }

    public void buildPanel()
    {
        for (int i = 0; i < opts.length; i++)
        {
            opts[i] = config.getProperty("option." + i + ".name");
        }

        JComboBox optBox = new JComboBox(opts);

        add(optLabel);
        add(optBox);
    }

    public void refPanel()
    {
        removeAll();
        this.buildPanel();
        ((JPanel) this).revalidate();
        repaint();
    }
}

And the settings class: 和设置类:

package testing;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.*;

public class settings 
{
    Properties config;
    final String        configFile  = "config.properties";

    JFrame setFrame = new JFrame("Settings");
    JLabel opt1 = new JLabel("Option 1");
    JLabel opt2 = new JLabel("Option 2");
    JLabel opt3 = new JLabel("Option 3");
    JTextField text1 = new JTextField(15);
    JTextField text2 = new JTextField(15);
    JTextField text3 = new JTextField(15);
    JButton apply = new JButton("Apply");

    public settings(Properties p)
    {
        config = p;
        setFrame.setSize(275, 200);
        setFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setFrame.setLayout(new FlowLayout());

        buildSetFrame();
    }

    public void buildSetFrame()
    {
        text1.setText(config.getProperty("option.0.name"));
        text2.setText(config.getProperty("option.1.name"));
        text3.setText(config.getProperty("option.2.name"));

        apply.addActionListener(new applyListener());

        setFrame.add(opt1);
        setFrame.add(text1);
        setFrame.add(opt2);
        setFrame.add(text2);
        setFrame.add(opt3);
        setFrame.add(text3);
        setFrame.add(apply);

        setFrame.setVisible(true);
    }

    private class applyListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            config.setProperty("option.0.name", text1.getText());
            config.setProperty("option.1.name", text2.getText());
            config.setProperty("option.2.name", text3.getText());

            FileOutputStream fos = null;
            try
            {
                fos = new FileOutputStream(configFile);
                config.store(fos, null);
            }
            catch (IOException f)
            {
                System.out.println("Error");
            }
            finally
            {
                if (fos != null)
                {
                    try
                    {
                        fos.close();
                    }
                    catch (IOException g)
                    {
                        System.out.println("Problem");
                    }
                }
            }
            setFrame.setVisible(false);

            //  This is where I need to pass something back to the 
                    //  testConfigLoad class to tell it to 
            //  run the refPanel method in the OptPanel class.
        }
    }
}   

The config file is named config.properties and looks like this: 配置文件名为config.properties,如下所示:

option.2.name=two
option.1.name=one
option.0.name=zero

One approach would be to use a callback between the OptPanel and the settings class. 一种方法是在OptPanelsettings类之间使用回调。 You could extract out the functionality to add the elements to the properties JComboBox into its own method: 您可以提取出将属性JComboBox到其自己的方法中的功能:

public void updateProperties(Properties p) {
    model.removeAllElements();
    for (String s: p.stringPropertyNames()) {
        model.addElement(p.getProperty(s));
    }
}

where model is a DefaultComboBoxModel . 其中modelDefaultComboBoxModel Then you could simply call 然后你可以简单地打电话

optPanel.updateProperties(config);

after successfully storing the properties. 成功存储属性后。


Some Points: 一些要点:

  • The preferred approach is not to use multiple JFrames . 首选方法是不使用多个JFrames One option is to use a single JFrame with a modal dialog. 一种选择是使用单个JFrame和模态对话框。 See this discussion . 见这个讨论
  • Don't extend JFrame, rather use an instance directly. 不要扩展JFrame,而是直接使用实例。
  • Java Naming conventions indicate that class names start with an uppercase letter, so settings would become Settings Java 命名约定表明类名以大写字母开头,因此settings将成为Settings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定Play商店中的APK更新时间? - How can I tell when my apk has been updated in the Play store? 如何通过AWS Java API知道何时创建了EBS快照? - From the AWS Java API, how can I tell when my EBS Snapshot has been created? 如何告诉一个类使用另一个类的方法? - How can I tell a class to use methods of another class? 满足条件后,如何让我的脚本运行另一个脚本? (Java) - How can I have my script run another script when a condition has been met? (Java) 当已在另一个微调器上选择项目时,如何从微调器中删除项目 - How can I remove an item from a spinner when it has been already selected on another spinner 在finally块中,我能否说出抛出了什么异常? - In a finally block, can I tell what exception has been thrown? 我可以判断是否调用了抽象方法吗? - Can I tell if an abstract method has been called? 如何判断我的html页面何时加载 - How can I tell when my html page has loaded 如何判断某个视图(平铺)是否已在Controller中定义? - How can I tell if a certain view (tile) has been defined in a Controller? 如何判断当前tx(Hibernate)中是否删除了Grails / GORM域实例? - How can I tell if a Grails/GORM domain instance has been deleted in the current tx (Hibernate)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM