从类到主方法获取布尔值。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
询问用户是新用户还是返回用户,然后运行myJFrame
是否正确?这是我的密码:
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()
方法的代码在哪里?