版上各位好,不好意思想请问各位一个问题
就是目前有个学校作业,我打算使用JPANEL来建立一个画布
目前初步程式如下
public static class canvas extends JPanel implements
MouseListener,MouseMotionListener{
int x = 0;
int y = 0;
int startx, starty, endx, endy;//起始座标与终点座标
public canvas() {
super.setBackground(Color.blue);
super.addMouseListener(this);
super.addMouseMotionListener(this);
super.setSize(800, 800);
super.setVisible(true);
}
public void mousePressed(MouseEvent e){
startx = e.getX();
starty = e.getY();
}
public void mouseReleased(MouseEvent e){
endx = e.getX();
endy = e.getY();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawLine(startx, starty, endx, endy);
}
}
public static void main(String[] args) {
JFrame Editor = new JFrame("UML Editor");
Container content = Editor.getContentPane();
content.setBackground(Color.green);
///////////////////////////////////////////////////////////////////////
JPanel toolbar = new JPanel();
toolbar.setBackground(Color.black);
content.add(toolbar, BorderLayout.WEST);
JPanel menu = new JPanel();
menu.setBackground(Color.black);
content.add(menu, BorderLayout.NORTH);
///////////////////////////////////////////////////////////////////////
canvas area=new canvas();
area.setBackground(Color.black);
content.add(area , BorderLayout.CENTER);
area.setVisible(true);
Editor.setSize(1500, 800);
Editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Editor.setVisible(true);
}