1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| public class MyJPanel extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillRect(0,0,100,100);
g.setColor(Color.BLUE);
g.fillOval(150,50,380,380);
g.drawLine(10, 1, 10, 99);
g.setFont(new Font("TimesRoman", Font.BOLD ,15));
g.drawString("coucou", 10, 10);
}
}
public class Demo {
public static JFrame frame;
public static void main(String[] args) {
frame = new JFrame("test");
MyJPanel jc = new MyJPanel();
JScrollPane jsp = new JScrollPane(jc);
jc.setPreferredSize(new Dimension(600,500));
jc.setMinimumSize(new Dimension(600,500));
jc.setMaximumSize(new Dimension(600,500));
frame.setPreferredSize(new Dimension(200,200));
frame.setMinimumSize(new Dimension(200,200));
frame.setMaximumSize(new Dimension(200,200));
WindowAdapter wa = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(wa);
jsp.getHorizontalScrollBar().addMouseListener(new MyActionListener());
jsp.getVerticalScrollBar().addMouseListener(new MyActionListener());
frame.getContentPane().add(jsp);
frame.pack();
frame.setVisible(true);
}
}
class MyActionListener implements MouseListener {
public void mouseExited(MouseEvent event) {
System.out.println("mouseExited");
}
public void mouseEntered(MouseEvent event) {
System.out.println("mouseEntered");
}
public void mouseClicked(MouseEvent event) {
System.out.println("mouseClicked");
}
public void mouseReleased(MouseEvent event) {
System.out.println("mouseReleased");
Demo.frame.repaint();
}
public void mousePressed(MouseEvent event) {
System.out.println("mousePressed");
}
} |
Partager