从类到主方法获取布尔值。JAVA

从类到主方法获取布尔值。JAVA,java,swing,jframe,jbutton,jdialog,Java,Swing,Jframe,Jbutton,Jdialog,我花了几个小时试图从类中获取boolean值到我的main方法。我想要变量GeneralFrame 使用JDialog询问用户是新用户还是返回用户,然后运行myJFrame是否正确?这是我的密码: package portofoliexpense; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swi

我花了几个小时试图从类中获取
boolean
值到我的
main
方法。我想要变量
GeneralFrame

使用
JDialog
询问用户是新用户还是返回用户,然后运行my
JFrame
是否正确?这是我的密码:

package portofoliexpense;   

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author AlexandrosAndreadis
 */
public class PortofoliExpenseDialog extends JDialog implements ActionListener {     
    boolean GeneralFrame = false;           

    //dimiourgia minimatos
    JPanel messagePane = new JPanel();          

    JPanel buttonPanel = new JPanel(); // ftiaxnw button
    JButton existinguser = new JButton("Existing User");
    JButton newuser = new JButton("New User");

    public PortofoliExpenseDialog (JFrame parent, String title) {
        super(parent, title);

        setLocationRelativeTo(null);
        //dimiourgia minimatos
        messagePane.add(new JLabel("Hello, click one of the above to continue!"));

        getContentPane().add(messagePane);// ypodoxeas gia ola ta //components           

        buttonPanel.add(existinguser);
        buttonPanel.add(newuser);
        getContentPane().add(buttonPanel, BorderLayout.PAGE_END); // border //layout sto telos tou dialog 


        newuser.addActionListener(this);
        existinguser.addActionListener(this);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false); // den allazei megethos
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == existinguser) {
           GeneralFrame = true; 

        }

        if (source == newuser){
           GeneralFrame = false;       
        }

    }        
}

我试了很多东西。我还使用了
return
,但无法获取它

Main方法只接受字符串数组:

public static void main(String[] args) { 
因此,可以发送给main方法的唯一数据当然是数组。但是,您希望传入一个布尔值

您知道如何传递参数,但这里它只接受字符串数组。因为我们想要传递一个布尔值,所以让我们在数组中传递该布尔值

通过字符串数组将布尔值传递到main方法后,执行以下操作检索它:

    boolean boolean1 = Boolean.parseBoolean(ArrayWhereIPutMyBoolean(0));

现在,boolean1是您从main方法传入的布尔值

Main方法只接受字符串数组:

public static void main(String[] args) { 
因此,可以发送给main方法的唯一数据当然是数组。但是,您希望传入一个布尔值

您知道如何传递参数,但这里它只接受字符串数组。因为我们想要传递一个布尔值,所以让我们在数组中传递该布尔值

通过字符串数组将布尔值传递到main方法后,执行以下操作检索它:

    boolean boolean1 = Boolean.parseBoolean(ArrayWhereIPutMyBoolean(0));

现在,boolean1是您从main方法传入的布尔值

在main方法中,您是否将变量GeneralFrame作为“实例名称”调用。GeneralFrame?

在main方法中,您是否将变量GeneralFrame作为“实例名称”调用。GeneralFrame?

您可以使用a而不是
JDialog

以下是一个例子:

static boolean isNewUser;
public static void main(String[] args) throws InvocationTargetException, InterruptedException {
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            isNewUser = isNewUser();
        }
    });
    System.out.println("Is new user: " + isNewUser);
}

public static boolean isNewUser() {
    Object[] options = { "Existing User", "New User" };
    int resp = JOptionPane.showOptionDialog(null, "Hello, click one of the above to continue!", "User Type", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    return resp == JOptionPane.NO_OPTION;
}
您可以使用a来代替
JDialog

以下是一个例子:

static boolean isNewUser;
public static void main(String[] args) throws InvocationTargetException, InterruptedException {
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            isNewUser = isNewUser();
        }
    });
    System.out.println("Is new user: " + isNewUser);
}

public static boolean isNewUser() {
    Object[] options = { "Existing User", "New User" };
    int resp = JOptionPane.showOptionDialog(null, "Hello, click one of the above to continue!", "User Type", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    return resp == JOptionPane.NO_OPTION;
}

如果你能解释一下代码。会好起来的!!!谢谢你,这正是我想要的。@AlexAndreadis没有太多需要解释的,你需要知道的只是
showOptionDialog(…)
方法的作用,你可以在本教程中找到很多关于它的细节,它基本上创建了一个对话,并替换了原来的“是”、“否”带有
选项
数组中的值的按钮,等待用户选择一个选项,并将所选选项返回为
布尔值
,我也可以看到
布尔值isNewUser=isNewUser()
SwingUtilities.invokeAndWait(new Runnable(){@Override public void run(){boolean isNewUser=isNewUser();}}})工作得更好
@AlexAndreadis可以工作,但由于这是与GUI相关的代码,它应该在
EDT
上执行,这就是我使用
SwingUtilities.invokeAndWait(…)
的原因。嗯,好的!但是使用
SwingUtilities.invokeAndWait(…)
我无法获得isNewUser的布尔类型。它说找不到符号。我在main方法上有它,然后我键入'if(isNewUser==true){//do sth}'。但是如果我使用
布尔值isNewUser=isNewUser()非常好用。如果你能解释一下代码的话。会好起来的!!!谢谢你,这正是我想要的。@AlexAndreadis没有太多需要解释的,你需要知道的只是
showOptionDialog(…)
方法的作用,你可以在本教程中找到很多关于它的细节,它基本上创建了一个对话,并替换了原来的“是”、“否”带有
选项
数组中的值的按钮,等待用户选择一个选项,并将所选选项返回为
布尔值
,我也可以看到
布尔值isNewUser=isNewUser()
SwingUtilities.invokeAndWait(new Runnable(){@Override public void run(){boolean isNewUser=isNewUser();}}})工作得更好
@AlexAndreadis可以工作,但由于这是与GUI相关的代码,它应该在
EDT
上执行,这就是我使用
SwingUtilities.invokeAndWait(…)
的原因。嗯,好的!但是使用
SwingUtilities.invokeAndWait(…)
我无法获得isNewUser的布尔类型。它说找不到符号。我在main方法上有它,然后我键入'if(isNewUser==true){//do sth}'。但是如果我使用
布尔值isNewUser=isNewUser()
工作正常。您的
main()
方法的代码在哪里?您的
main()
方法的代码在哪里?