0

这是我的代码,问题是当它运行时 JOptionPane 出现问题。

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class MoveTyper extends JFrame
{
int Location[] = {40, 40};
public static void main (String args[])
{
    new MoveTyper();
}

public MoveTyper() 
{
    super("typetomove");
    setSize(800, 450);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void paint(Graphics g)
{
    System.out.println("poop");
    g.setColor(Color.BLUE);


        g.fillRect(Location[0], Location[1], 20, 20);

        String Axis = JOptionPane.showInputDialog("Move by X or by Y? If you would like to quit, type cancel");

            int Movement = Integer.parseInt(JOptionPane.showInputDialog("How Many would you like to move up from"));
            if(Axis.equalsIgnoreCase("X"))
            {
                Location[0] = Location[0] + Movement;
            }
            else if(Axis.equalsIgnoreCase("cancel"))
            {

            }
            else
            {
                Location[1] = Location[1] + Movement;


    }

    JOptionPane.showMessageDialog(null, "You have quit. GOOD JOB. YOU SMELL");


}

当它运行时,JOptionPane 继续弹出(一个在另一个之上),好像在一个循环中,你们能帮我找出问题吗?我似乎找不到问题所在。

4

1 回答 1

0

代码 from showInputDialog()toshowMessageDialog()不应该在paint()方法中。将其移至新方法:

public void showDialog() {
    String Axis = JOptionPane.showInputDialog("Move by X or by Y? If you would like to quit, type cancel");

    int Movement = Integer.parseInt(JOptionPane.showInputDialog("How Many would you like to move up from"));
    if(Axis.equalsIgnoreCase("X"))
    {
        Location[0] = Location[0] + Movement;
    }
    else if(Axis.equalsIgnoreCase("cancel"))
    {

    }
    else
    {
        Location[1] = Location[1] + Movement;


    }
    JOptionPane.showMessageDialog(null, "You have quit. GOOD JOB. YOU SMELL");
}

在您的情况下,从构造函数调用此方法

public MoveTyper() 
{
    super("typetomove");
    setSize(800, 450);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    showDialog();
}

所以paint()方法看起来像这样

public void paint(Graphics g)
{
    System.out.println("poop");
    g.setColor(Color.BLUE);
    g.fillRect(Location[0], Location[1], 20, 20);
}
于 2013-09-22T17:13:30.147 回答