比如 我想写一个 在键盘上 按 向上箭头 然后就在frame里画一个圆 出来 但是怎么按 也画不出来 我这个程序 错在那了???
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class hh {
public static void main(String[] args) {
new frame("asdasdasdasd");
}
}class frame extends Frame {
frame(String s){
super(s);
this.setBounds(100, 100, 500, 500);
this.setVisible(true);
this.addKeyListener(new A());
this.addWindowListener(new B());
}
public String s;
public void ff(int k){
if(k== KeyEvent.VK_UP){ s="up";
}
}
public void paint(Graphics g){
if(s == "up"){
g.setColor(Color.BLACK);
g.fillOval(20, 20, 40,40);
}
}
}
class A extends KeyAdapter { frame f = null;
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
f = (frame)e.getSource();
f.ff(key);
}
}class B extends WindowAdapter {
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class hh {
public static void main(String[] args) {
new frame("asdasdasdasd");
}
}class frame extends Frame {
frame(String s){
super(s);
this.setBounds(100, 100, 500, 500);
this.setVisible(true);
this.addKeyListener(new A());
this.addWindowListener(new B());
}
public String s;
public void ff(int k){
if(k== KeyEvent.VK_UP){ s="up";
}
}
public void paint(Graphics g){
if(s == "up"){
g.setColor(Color.BLACK);
g.fillOval(20, 20, 40,40);
}
}
}
class A extends KeyAdapter { frame f = null;
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
f = (frame)e.getSource();
f.ff(key);
}
}class B extends WindowAdapter {
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
updateUI();
不觉得swing的界面很丑吗?