使用 Java Swing 应用程序,如何将对话框面板中的值获取到主应用程序中?

发布于 2024-08-27 18:28:10 字数 104 浏览 9 评论 0原文

在我的 Swing 应用程序中,用户可以单击按钮打开对话框面板并输入一些值,然后他们可以单击该面板上的“确定”将其关闭并返回主程序,但是我如何将他们输入的值传递给主程序而不先将它们保存到文件中?

In my Swing app, users can click a button to open a dialog panel and enter some values, then they can click "Ok" on that panel to close it and return to the main program, but how can I pass the values they enter to the main program without saving them to a file first ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

平生欢 2024-09-03 18:28:10

您可以执行以下操作:

  • 您可以在应用程序和对话框之间创建 Observer/Observable 关系,并让对话框在对话框关闭时使用事件中的值发布事件。
  • 您可以在应用中维护对话框的句柄,当您在对话框上调用 setVisible(false) 时,您可以查询对话框的数据。

There are a couple things you could do:

  • You could create an Observer/Observable relationship between your app and the dialog, and make the dialog publish an event when the dialog closes with the values in the event.
  • You could maintain a handle to your dialog in your app, and when you call setVisible(false) on dialog, you can query the dialog for its data.
匿名的好友 2024-09-03 18:28:10

UI 通常应该分层在底层应用程序之上。 (IMO,UI 本身应该分为几层,但这是另一个咆哮。)将应用程序的相关部分传递到 UI 的创建中。当您准备好将值从 UI 传输到应用程序(可以是多页对话框上的“确定”按钮,直至每次击键)时,请将数据推送到应用程序组件。

The UI should usually be layered upon the underlying application. (IMO, the UI should itself be split into layers, but that another rant.) Pass in a relevant part of your application to the creation of your UI. When you are ready for values to go from the UI to the application (which could be an OK button on a multi-page dialog, down to every keystroke), push the data to the application component.

怪异←思 2024-09-03 18:28:10

我宁愿建议您看一下“事件总线”,而不是仅仅使用糟糕的 Java Observable/Observer API,它应该特别适合您的情况。

事件总线的两种实现:

其中之一应该帮助您解决当前的问题。

Rather than using just the poor Java Observable/Observer API, I'd rather advise you take a look at an "Event Bus", which should be particularly suited for your situation.

Two implementations of Event Buses:

  • EventBus very general event bus,
    can be used in any situation
  • GUTS Event Bus specific to Guice dependency injection library

One of these should help you with your current problem.

风吹过旳痕迹 2024-09-03 18:28:10

只需在对话框类中定义一个具有单个方法的接口(例如:“returnToCaller(returntype rt)”)即可。

对话框的构造函数将此接口的实例作为输入,并使用它在退出/确定时返回结果。

主模块(窗口或其他)只是实例化对话框,从而匿名使用该接口,定义返回方法,有点像委托(c#)。

那么调用是:

    MyDialog md = new MyDialog(new MyDialog.ReturnToCaller() {
        public void returnToCaller(ReturnValues rv) {
                         // Handle the values here
        }
    });
    md.setModal(true);
    md.setVisible(true);

Just define an interface with a single method (like: "returnToCaller(returntype rt)") in your dialog class.

The Constructor of the dialog takes an instance of this interface as input, and uses it to return results on exit/ok.

The mainmodule (window or whatever) simply instantiates the dialog and thus makes annonymous use of the interface, defininng the return method, sort of like a delegate (c#).

The call then being:

    MyDialog md = new MyDialog(new MyDialog.ReturnToCaller() {
        public void returnToCaller(ReturnValues rv) {
                         // Handle the values here
        }
    });
    md.setModal(true);
    md.setVisible(true);
鸢与 2024-09-03 18:28:10

我建议采用MVC(模型-视图-控制器)设计。所以对话框将是你的视图,也可能是控制器。但必须有一个域类作为您的模型。例如,如果创建对话框是为了编辑个人数据,那么您可以使用名为 Person 的类来保存数据。对话框的设计方式应使您可以从中设置和获取人员。

I would suggest MVC(Model-view-controller) design. So dialog will be you view and possibly controller. But have to have a domain class which will be your model. For example, if the dialog is created to edit personal data, then you can have class called Person which will hold the data. The dialog should be designed in the way so you can set and get a Person from it.

时光清浅 2024-09-03 18:28:10

实现对话框面板的类必须具有到主程序的链接,并且主程序必须提供一个带有参数的方法,这些参数将是要传输的值。

然后,您的对话框面板类侦听“确定”按钮,并在单击按钮时检索值并通过上述方法使用它们。

class Main {
    //...
    private Dialog myDialog ;

    public Main(){
        //...
        myDialog = new Dialog(this);
        //...
    }

    //...

    public void onDialogOk(String valueA, int valueB)
    {
        //...
    }
}

class Dialog implement ActionListener{
    //...
    private Main myMain ;

    public setMain(Main main){
        myMain = main;
    }

    public Dialog(Main main){
        //...
        setMain(main) ;

        //...
        JButton ok = new JButton("ok") ;
        ok.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) { 
        // retrieve form values
        String valueA = ... ;
        int valueB = Integer.parse(...);
        myMain.onDialogOK(valueA, valueB) ; //DONE
    }
}

The class implementing your dialog panel must have a link to your main program, and your main program must provide a method with parameters that will be the values to transmit.

Then your dialog panel class listen to the Ok button, and on the button click, it retrieve the values and use them with the aforementionned method.

class Main {
    //...
    private Dialog myDialog ;

    public Main(){
        //...
        myDialog = new Dialog(this);
        //...
    }

    //...

    public void onDialogOk(String valueA, int valueB)
    {
        //...
    }
}

class Dialog implement ActionListener{
    //...
    private Main myMain ;

    public setMain(Main main){
        myMain = main;
    }

    public Dialog(Main main){
        //...
        setMain(main) ;

        //...
        JButton ok = new JButton("ok") ;
        ok.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) { 
        // retrieve form values
        String valueA = ... ;
        int valueB = Integer.parse(...);
        myMain.onDialogOK(valueA, valueB) ; //DONE
    }
}
零時差 2024-09-03 18:28:10

也许您想尝试这个解决方案:

class MyDialog {

private static String[] returnValues = new String[10]
private static MyDialog dialog;

private MyDialog() {
  initDialog()
}

private void closeDialog()
{
     dispose();
}

private initDialog()
{

 //....
 btnOk = new JButton("OK");
  jTextField1 = new JTextField();
  ...
  jTextField10 = new JTextField();
  ...
  ActionListener btnOK_click = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            returnValues[0] = jTextField1.getText();
            ...
            returnValues[9] = jTextField10.getText();
            closeDialog();  
        }
  }
  btnOk.addActionListener(btnOk_click);
}

public static String[] showMyDialog() {
    dialog = new MyDialog();
    dialog.setVisible(true);
    return returnValues;
}

}

May be you would like to try this solution:

class MyDialog {

private static String[] returnValues = new String[10]
private static MyDialog dialog;

private MyDialog() {
  initDialog()
}

private void closeDialog()
{
     dispose();
}

private initDialog()
{

 //....
 btnOk = new JButton("OK");
  jTextField1 = new JTextField();
  ...
  jTextField10 = new JTextField();
  ...
  ActionListener btnOK_click = new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            returnValues[0] = jTextField1.getText();
            ...
            returnValues[9] = jTextField10.getText();
            closeDialog();  
        }
  }
  btnOk.addActionListener(btnOk_click);
}

public static String[] showMyDialog() {
    dialog = new MyDialog();
    dialog.setVisible(true);
    return returnValues;
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文