我最近开始使用WindowBuilder for Java进行编程,但我遇到了一个非常恼人的问题,我似乎无法解决这个问题。
问题是在YesOrNo方法完成(第131行)完成之前执行调试(System.out.println(answer);第133行)。所以它在我的计划中“超前”。
我的代码:
main.java
package me.fractional.main;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
public class main {
GUI go = new GUI();
Studio so = new Studio();
private JFrame frmFractionalc;
static JLabel logo = new JLabel("");
static JButton InputNext = new JButton("Continue");
static JLabel OutputMainText = new JLabel("");
static JTextField InputText = new JTextField();
static JButton InputYes = new JButton("Yes");
static JButton InputNo = new JButton("No");
public static int counter = 1;
public int answer = 2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
main window = new main();
window.frmFractionalc.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public main() {
initialize();
intro();
}
public void Tutorial()
{
System.out.println("Tutorial");
OutputMainText.setVisible(true);
go.OutputMainText("Welcome to! Click Next Turn to Continue.");
}
public void MainMenu()
{
System.out.println("MainMenu");
}
private void initialize() {
frmFractionalc = new JFrame();
frmFractionalc.getContentPane().setBackground(new Color(176, 224, 230));
frmFractionalc.setBackground(new Color(0, 128, 128));
frmFractionalc.setTitle("Fractional © - GameDevStory");
frmFractionalc.setBounds(100, 100, 500, 300);
frmFractionalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFractionalc.getContentPane().setLayout(null);
logo.setHorizontalAlignment(SwingConstants.CENTER);
logo.setBounds(42, 44, 400, 150);
frmFractionalc.getContentPane().add(logo);
frmFractionalc.getContentPane().add(InputNext);
OutputMainText.setHorizontalAlignment(SwingConstants.CENTER);
OutputMainText.setBounds(10, 69, 464, 105);
frmFractionalc.getContentPane().add(OutputMainText);
frmFractionalc.getContentPane().add(InputText);
frmFractionalc.getContentPane().add(InputYes);
frmFractionalc.getContentPane().add(InputNo);
}
public void intro()
{
logo.setIcon(new ImageIcon(main.class.getResource("/me/fractional/main/logo.png")));
InputNext.setForeground(new Color(0, 0, 0));
InputNext.setBounds(200, 239, 89, 23);
InputNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
switch(counter)
{
case 1:
logo.setVisible(false);
go.OutputMainText("Welcome to GameDevStudio!");
counter = 2;
break;
case 2:
go.OutputMainText("Enter a name for your game studio below [10 characters max!]");
InputText.setBounds(162, 174, 154, 20);
InputText.setColumns(10);
InputText.setVisible(true);
counter = 3;
break;
case 3:
so.setName(InputText.getText());
if(so.getName().length() < 1 || so.getName() == null)
{
go.OutputMainText("Invalid name!");
InputText.setVisible(false);
counter = 2;
}
else
{
go.OutputMainText(so.getName() + ", that's a name I'd use!");
InputText.setVisible(false);
counter = 4;
}
break;
case 4:
InputNext.setVisible(false);
go.OutputMainText("Would you like to run the tutorial?");
answer = go.YesOrNo(1);
System.out.println(answer);
break;
}
}
});
}
}
GUI.java
package me.fractional.main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import me.fractional.main.main;
public class GUI {
static int returnvalue;
public void OutputMainText(String text)
{
main.OutputMainText.setText(text);
main.OutputMainText.setVisible(true);
}
public int YesOrNo(final int option)
{
main.InputYes.setBounds(112, 239, 89, 23);
main.InputNo.setBounds(287, 239, 89, 23);
main.InputYes.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
returnvalue = 1;
main.InputYes.setVisible(false);
main.InputNo.setVisible(false);
if(option == 1)
{
main.OutputMainText.setText("");
}
}
});
main.InputNo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
returnvalue = 0;
main.InputYes.setVisible(false);
main.InputNo.setVisible(false);
if(option == 1)
{
main.OutputMainText.setText("");
}
}
});
return returnvalue;
}
}
Studio.java
package me.fractional.main;
public class Studio {
String s_name;
public String getName()
{
return s_name;
}
public void setName(String name)
{
s_name = name;
}
}
答案 0 :(得分:1)
Swing是一个事件驱动的环境,这意味着你的程序会为用户生成输入,他们可以随心所欲地与他们互动。
不是像控制台程序那样的线性输入过程,而是通过事件通知用户交互。
这意味着您的YesOrNo
方法将在调用任何actionPerformed
方法之前返回。
如果由于某种原因需要停止应用程序流程,您只能选择使用某种对话框。
请查看How to use dialogs了解详情
Ps-当你在那里时,你可能想看看How to use layouts,它们会让你在长期错误中生活更轻松