synchronized同步关键字就是关键代码

解决方案 »

  1.   

    看一直没人写,看来我真的要试试了。不过很不好意思,我现在不知道为什么要有Canvas这个类。我就用Component的子类对象写了,估计会用J类的。:)
      

  2.   

    import javax.swing.*;
    import javax.swing.border.EtchedBorder;
    import java.awt.*;
    import java.awt.event.*;interface MyInitScheme {
    void initVarient();
    void initFace();
    }
    class MyMenu extends JMenu implements MyInitScheme {
    public MyMenu(String text) {
    super(text);
    initVarient();
    initFace();
    }
    public void initFace() {
    setBackground(SystemColor.desktop);
    setFont(new Font("Serif", 0, 16));
    setForeground(SystemColor.info);
    setBorder(BorderFactory.createEtchedBorder());
    }
    public void initVarient(){};
    }
    class MyMenuItem extends JMenuItem implements MyInitScheme {
    public MyMenuItem(String text) {
    super(text);
    initVarient();
    initFace();
    }
    public void initFace() {
    setBackground(SystemColor.desktop);
    setFont(new Font("Serif", 0, 16));
    setForeground(SystemColor.info);
    setBorder(BorderFactory.createEtchedBorder());
    }
    public void initVarient(){};
    }class MyThread extends Thread {
    private Component p;
    private boolean stop=false; public void run() {
    while(!stop) {
    try {
    sleep(200);
    } catch (InterruptedException e) {
    System.out.println(e);
    }
    p.repaint();
    }
    }
    public void stopThread() {
    stop=true;
    }
    public void start() {
    stop=false;
    super.start();
    } public MyThread(Component cp) {
    p=cp;
    }
    }
    class MyFrame extends JFrame implements MyInitScheme {
    class MyContent extends JPanel {
    int x=10,y=10,r=30;

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    java.util.Random rand=new java.util.Random();
    r=10+rand.nextInt(90);
    g.drawOval(x, y, r, r);
    }
    public MyContent() {
    setBackground(SystemColor.activeCaption);
    }
    }
    JPanel p1=new MyContent();
    JPanel p2=new MyContent();
    private JMenuBar mnuBar = new JMenuBar();
    private JMenu mnuFile = new MyMenu("File");
    private JMenuItem mnuFileExit = new MyMenuItem("Exit");
    private JMenu mnuHelp = new MyMenu("Help");
    private JMenuItem mnuHelpContact = new MyMenuItem("Contact Me");

    public void initVarient() {};
    public void initFace() {
    this.setSize(new Dimension(320, 240));
    this.setResizable(false);
    getContentPane().setLayout(null);
    getContentPane().setBackground(SystemColor.activeCaption);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mnuBar.setBackground(SystemColor.desktop);
    mnuBar.setBorder(BorderFactory.createRaisedBevelBorder()); mnuFile.setMnemonic('F');
    mnuFileExit.setMnemonic('x');
    mnuFileExit.addActionListener(new ActionListener () {
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    } ); mnuHelp.setMnemonic('H');
    mnuHelpContact.setMnemonic('M');
    mnuHelpContact.addActionListener(new ActionListener () {
    public void actionPerformed(ActionEvent e) {
    try {
    Process p=Runtime.getRuntime().exec("C:/Program Files/"
    +"Internet Explorer/IExplore.EXE"
    +" mailto:[email protected]");
    } catch (Exception ex) {
    System.out.println(ex);
    }
    }
    } );
    mnuFile.add(mnuFileExit);
    mnuHelp.add(mnuHelpContact);
    mnuBar.add(mnuFile);
    mnuBar.add(mnuHelp); this.setJMenuBar(mnuBar);
    p1.setBounds(new Rectangle(0,0,150,180));
    p2.setBounds(new Rectangle(160,0,150,180));
    getContentPane().add(p1);
    getContentPane().add(p2);
    } public MyFrame(String title) {
    super(title);
    initVarient();
    initFace();
    show();
    }
    }
    public class ThreadApp {
    public static void main(String[] args) {
    MyFrame frame=new MyFrame("Test Threads");
    final MyThread t1=new MyThread(frame.p1);
    final MyThread t2=new MyThread(frame.p2);
    frame.getContentPane().addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    t1.stopThread();
    t2.stopThread();
    }
    });
    t1.start();
    t2.start();
    }
    }