各位大神,为什么我这个连画的正方形都显示不出来呢 我是新手

mah983379392 2016-10-09 05:02:18
package actionListener;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Key_draw extends Frame{
public static void main(String[] args) {
new Key_draw().Launch02();
}
MyPanel02 mp02;
Button btn1=new Button("上");
Button btn2=new Button("下");
Button btn3=new Button("左");
Button btn4=new Button("右");
public void Launch02(){
mp02=new MyPanel02();
//this.add(mp02);
this.setLayout(new FlowLayout());
this.setSize(500, 500);
btn1.addActionListener(mp02);
btn2.addActionListener(mp02);
btn3.addActionListener(mp02);
btn4.addActionListener(mp02);
this.add(mp02);
this.add(btn1);this.add(btn2);this.add(btn3);this.add(btn4);
this.setLocation(100,100);
this.show();
}
}
class MyPanel02 extends Panel implements ActionListener{
int x=200;
int y=200;
Key_draw kd;
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.drawRect(x, y, 50, 50);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==kd.btn1){
y-=10;
}
else if(e.getSource()==kd.btn2){
y+=10;
}else
if(e.getSource()==kd.btn3){
x-=10;
}
else
if(e.getSource()==kd.btn4){
x+=10;
}
repaint();
}


}
...全文
338 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
捏造的信仰 2016-10-12
  • 打赏
  • 举报
回复
楼主要学会贴代码
public class Demo {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("no args.");
        }
    }
}
像楼主的代码层级多,又没有排版,没法看。
bubble_c 2016-10-11
  • 打赏
  • 举报
回复
你的panel是默认大小,是很小的一块,并不是撑满剩余的空间,所以你画的正方形在panel的显示范围之外,实际上可以直接画在frame上。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧