这是我的代码,问题是当它运行时,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继续弹出(一个在另一个上面),好像在一个循环中,你们可以帮助我找出问题吗?我似乎无法找到问题。
答案 0 :(得分:0)
showInputDialog()
到showMessageDialog()
的代码不应采用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);
}