简体   繁体   English

如何打开新的jFrame对话框并返回主界面?

[英]How to open new jFrame dialogue and return to main interface?

I tried this 我试过这个

private void botaoConfIOMouseClicked(java.awt.event.MouseEvent evt) {                                         
ConfigurarIO popup = new ConfigurarIO();
popup.setVisible(true);

botaoConfIO.setEnabled(false); //this line to avoid multiple dialogues
setIO=popup.getConfig();  //i need to get this boolean from the dialogue "ConfigurarIO"
//part of the program only to make my logic from the setIO 
if(setIO[0]==false){
    jToggleButton1.setEnabled(false);
    jToggleButton1.setText("Saída");
}
else{
    jToggleButton1.setEnabled(true);
    if(jToggleButton1.isSelected()) jToggleButton1.setText("Pino 1 ON");
    else jToggleButton1.setText("Pino 1 OFF");

} } }}

And this is the dialogue 这就是对话

public class ConfigurarIO extends javax.swing.JFrame {

boolean[] inOut=new boolean[8];
boolean ok=false;
/** Creates new form ConfigurarIO */
public ConfigurarIO() {
    initComponents();
}

public boolean[] getConfig(){
    return inOut;
}

public boolean getOK(){
    return ok;
}

public void setOK(){
    ok=false;
}
//the logic was emited
private void botaoOKMouseClicked(java.awt.event.MouseEvent evt) {                                     
dispose();
ok=true;
System.out.println(ok);
}    

The problem is that the setIO is not modified by the second interface and, If I set this to make a loop broken only by the "ok" boolean, the window with the setting interface doesn't open. 问题是setIO没有被第二个接口修改,如果我将其设置为仅通过“ok”布尔值来打破循环,则具有设置界面的窗口不会打开。 This is a very explored problem but I am new to Netbeans and I couldn't find it on Google. 这是一个非常探索的问题,但我是Netbeans的新手,我在Google上找不到它。 Thanks for the attention 感谢您的关注

Print screen: http://4.bp.blogspot.com/-B7VWmPelJek/T2ysJV8PJcI/AAAAAAAABqQ/0waWxxEEHkw/s320/temp.png 打印屏幕:http: //4.bp.blogspot.com/-B7VWmPelJek/T2ysJV8PJcI/AAAAAAAABqQ/0waWxxEEHkw/s320/temp.png

You haven't said whether a frame is required for some reason, or whether a dialog would do, or whether whatever it is needs to be modal. 你还没有说过是否由于某种原因需要一个框架,或者一个对话框是否会这样做,或者它是否需要是模态的。

The reason the frame doesn't show up if you loop is that you're on the Swing dispatch thread (since you are in a routine that responded to a mouse click), and until it returns, it isn't going to update the screen. 如果你循环框架没有出现的原因是你在Swing调度线程上(因为你处于一个响应鼠标点击的例程),并且直到它返回,它不会更新屏幕。

You cannot just call a method on the "frame dialog" to get a value until you know that the dialog has set the value. 您不能只在“框架对话框”上调用方法来获取值,直到您知道对话框已设置值为止。 I would pass my calling class to the dialog as a parameter on the constructor, and then have the dialog code call a method on the calling class when it's all done. 我会将我的调用类作为构造函数的参数传递给对话框,然后让对话框代码在调用类完成后调用一个方法。 If you need to know when this happens, then you'll have to treat it as an event in your calling class; 如果您需要知道何时发生这种情况,那么您必须将其视为您的呼叫类中的事件; I can't guess what you need for that without knowing more about what you're trying to do overall. 如果不了解更多关于你要做的事情,我无法猜测你需要什么。

If you need to wait until the dialog is done, and don't need the user to be able to do anything until it is done, then what you want is a "modal" dialog, and I recommend looking at JOptionPane and its various dialog options for what you want to do. 如果你需要等到对话框完成,并且不需要用户能够做任何事情直到完成,那么你想要的是一个“模态”对话框,我建议看看JOptionPane及其各种对话框你想做什么的选择。 THEN the call from your class can be synchronous, ie, you can call the dialog and, when the call completes, the dialog is all done. 然后,您的类的调用可以是同步的,即,您可以调用对话框,并且在调用完成时,对话框全部完成。 Then you don't need to pass the calling class to the frame, since it doesn't need to notify you that it's done; 然后你不需要将调用类传递给框架,因为它不需要通知你已完成; you know it's done when your call completes, and you can call a method such as you have already done to get the value that you want. 你知道在你的电话完成时已经完成了,你可以调用你已经完成的方法来获得你想要的值。

Incidentally, your subclass-of-JFrame constructor doesn't call super(); 顺便说一下,你的JFrame构造函数的子类不会调用super(); I recommend you do that... 我建议你这样做......

rc RC

// we will make this modal=true, to block access to the parent frame
public class ConfigurarIO extends javax.swing.JDialog {

For more details, see: 有关详细信息,请参阅:

  1. How to Make Dialogs 如何制作对话框
  2. How to Use Modality in Dialogs 如何在对话框中使用模态

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM