java自习室占座模拟,哪位高手帮忙看一下,小妹感激不尽!

来源:百度知道 编辑:UC知道 时间:2024/06/24 18:01:31
想模拟一个100人自习室,所有的人按照先后顺序可以使用一个位置,以一个椭圆代表学生,想在自习室StudyRoomSeats中画图,为什么总是有问题阿!急!
public class StudyRoomFrame extends JFrame{
//全局变量 seats,在frame中画出一个长450,宽450的panel,
//并用drawstring方法打格,分成100个小方块
StudyRoomSeats seats=new StudyRoomSeats();
//占座用到的起始点(x,y),若此座已有人占用,则标致位不为0
int[] flagx=new int[46];
int[] flagy=new int[46];
Random r = new Random();
//计算Student的人数
int studentCount=0;
//构造函数:frame中加入seats和一个Panel(里面有newstudent和exit buttons)
public StudyRoomFrame (){
this.setLayout(new BorderLayout());
this.add(seats,BorderLayout.CENTER);
this.add(new StudyRoomButtons(),BorderLayout.SOUTH);
}
//main()显示frame
public static void main(String[] args) {
StudyRoomFrame frame=new StudyRoomFrame();
frame.setSize(458,515);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Study Room");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//

你的思路不是很好:
我先帮你改下错误,在调节一下思路吧。
错误在Students类中
flagx[x]=x/10;flagy[y]=y/10; // x和y越界
改成flagx[x / 10] = x / 10;flagy[y / 10] = y / 10;
你画不出圆是因为paintComponent方法一直没有被调用,自己测试。
repaint方法不会去调用paintComponent的。
清理思路:
1.Students类中定义draw(Graphics g)方法,画椭圆。
2.NewStudents implements ActionListener 中调用StudyRoomSeats的actionPerformed(ActionEvent ae)方法,方法自己定义。
3.StudyRoomSeats的actionPerformed调用Students类中的actionPerformed(ActionEvent ae)方法,确定自己坐在什么地方。
学习面向对象的思想,很重要

呵呵 真的没有耐心看完。
你提问的时候把问题直接说出来。

class StudyRoomButtons extends JPanel{
public StudyRoomButtons(){
JButton jbtnewstu=new JButton("new Student");
newStudent newstu=new newStudent();
jbtnewstu.addActionListener(newstu);
this.add(jbtnewstu);

JButton jbtexit=new JButton("exit");
exit exitstu=new exit();
jbtexit.addActionListener(exitstu);
this.add(jbtexit); //
}
}

this.add(jbt