关于绘图

黑煤碳 2017-06-21 11:03:37
我写了一段关于绘图的程序
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Paint;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Huitu extends JFrame {
Mymb mb=new Mymb();
public static void main(String[] args) {
Huitu huitu=new Huitu();
}
public Huitu(){
mb=new Mymb();

this.add(mb);
this.setTitle("电梯");
this.setSize(600, 800);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Mymb extends JPanel{
private int y=500;
private int Zhuangtai=0;//0为无,1为上,2为下
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.setColor(Color.gray);
g.fillRect(50, 500, 150, 200);
}
public void paintComponent(Graphics g){

}
public Mymb(){
repaint();
}
public void setY(int y){
this.y=y;
}
// public int getY(){
// return y;
// }
// public int getZhuangtai(){
// return Zhuangtai;
// }
}
当我注释掉Mymb类里面的返回类型为int的方法时,程序可以绘出图像
当我不注释代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Paint;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Huitu extends JFrame {
Mymb mb=new Mymb();
public static void main(String[] args) {
Huitu huitu=new Huitu();
}
public Huitu(){
mb=new Mymb();

this.add(mb);
this.setTitle("电梯");
this.setSize(600, 800);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Mymb extends JPanel{
private int y=500;
private int Zhuangtai=0;//0为无,1为上,2为下
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.setColor(Color.gray);
g.fillRect(50, 500, 150, 200);
}
public void paintComponent(Graphics g){

}
public Mymb(){
repaint();
}
public void setY(int y){
this.y=y;
}
public int getY(){
return y;
}
public int getZhuangtai(){
return Zhuangtai;
}
}

就会显示不了那个矩形
求大神指点迷津!
...全文
321 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑煤碳 2017-10-30
  • 打赏
  • 举报
回复
无人问津? 其实我就是想用java模拟写出一个电梯的动态view图而已 有没有人?
Aoess丶 2017-10-30
  • 打赏
  • 举报
回复
getzhuangtai方法没有问题,就是gety有问题,可能是JPanel里已经有这个函数了,你换一个函数名就可以用了,比如get_y这样的
黑煤碳 2017-06-21
  • 打赏
  • 举报
回复
求大神指点一下

62,621

社区成员

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

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