从 class 获取布尔值到 main 方法。 JAVA
Get boolean value from a class to main method. JAVA
几个小时以来,我一直在尝试将 boolean
值从 class 获取到我的 main
方法。我想要变量 GeneralFrame
.
另外,使用JDialog
询问用户是新人还是退货,然后运行我的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 方法的唯一数据当然是数组。但是,您想传递一个布尔值。
你知道如何传递参数,但这里它只接受一个字符串数组。因为我们想传递一个布尔值,所以我们只传递那个布尔值 in 数组。
通过字符串数组将布尔值传递到 main 方法后,执行以下操作检索它:
boolean boolean1 = Boolean.parseBoolean(ArrayWhereIPutMyBoolean(0));
现在,boolean1 就是您从 main 方法中传入的布尔值!
在主要方法中,您是否将变量 GeneralFrame 称为 "instance name".GeneralFrame?
您可以为此使用 JOptionPane 而不是 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;
}
几个小时以来,我一直在尝试将 boolean
值从 class 获取到我的 main
方法。我想要变量 GeneralFrame
.
另外,使用JDialog
询问用户是新人还是退货,然后运行我的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 方法的唯一数据当然是数组。但是,您想传递一个布尔值。
你知道如何传递参数,但这里它只接受一个字符串数组。因为我们想传递一个布尔值,所以我们只传递那个布尔值 in 数组。
通过字符串数组将布尔值传递到 main 方法后,执行以下操作检索它:
boolean boolean1 = Boolean.parseBoolean(ArrayWhereIPutMyBoolean(0));
现在,boolean1 就是您从 main 方法中传入的布尔值!
在主要方法中,您是否将变量 GeneralFrame 称为 "instance name".GeneralFrame?
您可以为此使用 JOptionPane 而不是 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;
}