Java在继续之前等待按下按钮

时间:2014-04-08 07:09:30

标签: java jbutton actionlistener

我试图为游戏中的设置创建一个GUI类,它是除了main之外的它自己的类。在main中我实例化了一个设置类的对象,我希望它等到它在继续main中的代码之前点击一下。实际上,它实例化对象并在main中完成代码而无需等待输入。香港专业教育学院尝试过actionListener但是无法让它工作,我的猜测是因为我将GUI与main分开。

设置类

public class Settings extends JFrame implements ActionListener
{
private int option;

public Settings()
{
    option = 0;
    //JFrame
    setTitle("Battleship Settings");
    setVisible(true);
    int xInset = getInsets().left + getInsets().right; //right left window borders
    int yInset = getInsets().top + getInsets().bottom; //top bottom window borders
    setSize(300 + xInset, 300 + yInset);
    setLocationRelativeTo(null); //center window

    //Main Panel
    JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setSize(300, 300);
    panel.setBackground(Color.blue);
    add(panel);

    // Creation of a Panel to contain the Buttons.
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(null);
    buttonPanel.setLocation(0, 0);
    buttonPanel.setSize(300, 300);
    buttonPanel.setBackground(Color.red);
    panel.add(buttonPanel);

    //Buttons
    JButton b1 = new JButton("VS AI");
    b1.setSize(180, 30);
    int b1CenterX = (int) ((buttonPanel.getSize().getWidth() / 2) - (b1.getSize().getWidth() /2));
    b1.setLocation(b1CenterX, 145);
    b1.addActionListener(this);
    buttonPanel.add(b1);

    JButton b2 = new JButton("2 Players");
    b2.setSize(180, 30);
    int b2CenterX = (int) ((buttonPanel.getSize().getWidth() / 2) - (b2.getSize().getWidth() /2));
    b2.setLocation(b2CenterX, 195);
    b2.addActionListener(this);
    buttonPanel.add(b2);

    panel.setOpaque(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE); //if you dont exit on close, the program keeps running on the background
}

public int getOption()
{
    return option;
}

public void setOption(int optn)
{
    option = optn;
}

@Override
public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().matches("VS AI"))
    {
        JOptionPane.showMessageDialog(null, "VS AI");
        option = 1;
        this.dispose();
    }
    else if (e.getActionCommand().matches("2 Players"))
    {
        JOptionPane.showMessageDialog(null, "2 Players");
        option = 2;
        this.dispose();
    }
    else
        JOptionPane.showMessageDialog(null, "Something went wrong");
}

}

这是主要的

public class Main {

public static void main(String[] args)
{
    Settings BattleSettings = new Settings();



    JOptionPane.showMessageDialog(null, "option # " +BattleSettings.getOption());
}

}

程序在为玩家提供选择按钮的机会之前在主消息中触发MessageDialog。我想在GUI窗口创建后等待,并在玩家点击按钮后恢复代码。

2 个答案:

答案 0 :(得分:0)

使用JDialog代替JFrame

有关详细信息,请参阅How to make dislaogs

事实上,您可以做的是将Settings班级设为JPanel,并将其作为邮件参数添加到JOptionPane

examples

这样可以让Settings类更灵活,更可重复使用......

答案 1 :(得分:0)

如何将该行放在actionPerformed函数中,如:

@Override
public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().matches("VS AI"))
    {
        JOptionPane.showMessageDialog(null, "VS AI");
        option = 1;
        JOptionPane.showMessageDialog(null, "option # " +option);
        this.dispose();
    }
    else if (e.getActionCommand().matches("2 Players"))
    {
        JOptionPane.showMessageDialog(null, "2 Players");
        option = 2;
        JOptionPane.showMessageDialog(null, "option # " +option);
        this.dispose();
    }
    else
        JOptionPane.showMessageDialog(null, "Something went wrong");
}

因此,在您选择vs AI2 players

后,系统会明确调用该行

修改

好的,我建议您关注@MadProgrammer的解决方案..我也认为使用JDialog是正确的方法..