【发布时间】:2016-05-05 07:06:12
【问题描述】:
我是 Java 新手,我正在处理一个学校项目。
在这个项目中,我必须使用 KeyListener 创建一个矩阵。 我是这样设计的:
if (!(e.getKeyChar()>='0' && e.getKeyChar()<='9' || e.getKeyChar()=='-' || e.getKeyCode()==10) ) {
if(!(e.getKeyChar() == 'w' || e.getKeyChar() == 'b')){
JOptionPane.showMessageDialog(lince, "Not Allowed", "Fatal Error", JOptionPane.ERROR_MESSAGE);
}
}else{
if(e.getKeyCode()==10){
try{
MatrixI[i][j]=Integer.parseInt(Posicion);
j++;
Posicion = "";
if (j== 4){
i++;
j=0;
}if (i==5){
if (!(i >= 6)){
JOptionPane.showMessageDialog(lince, "You cannot add more numbers", "Not Allowed!!!", JOptionPane.ERROR_MESSAGE);
contar();
}
}
}catch(ArrayIndexOutOfBoundsException ex){
JOptionPane.showMessageDialog(lince, "You cannot add more numbers", "Not Allowed!!!", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
catch(NumberFormatException ex){
JOptionPane.showMessageDialog(lince, "Just write a number", "Not Allowed!!!", JOptionPane.ERROR_MESSAGE);
}
}else{
Posicion=Posicion+e.getKeyChar();
this.escribe(lince.getGraphics());
}
}
public void escribe(Graphics g){
g.setColor(Color.red);
g.setFont(new Font("Arial", Font.CENTER_BASELINE,12));
g.drawString(Posicion, 30+((1+j)*50),((1+i)*35));
}
这部分打印矩阵,但位置不好,我想移动它。
有人知道吗?
我该如何移动它?
【问题讨论】:
-
Graphics#drawString(String, int, int)其中int的值为x/y ...尝试更改y的值
标签: java matrix jframe keyevent