Java实训笔记(三)

来源:互联网 发布:php 解析爱奇艺 编辑:程序博客网 时间:2024/06/11 11:56

在经过了周末的休整,在今天我们继续开始了java实训的旅程,在今天我们主要讲解:

1、  如何在窗体画图

2、  如何实现图像的移动

3、  如何使用线程

问题1:如何在窗体上输出字符串“Helloworld”?

解决问题方法:

1、  窗体字符串的输出:在java中实现字符串的输出以及图像的绘制需要用到Graphics或者Graphics2D这两个抽象类就可以实现。这两个类是JComponent类中的paintComponent方法的参数,那也就是说,要实现图形的绘制和字符串的输出,就需要继承JComponent类或者JComponent类的子类,重写paintComponent方法即可。

代码示例:

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import javax.swing.JComponent;

public class MyComp extends JComponent {

         @Override

         protected void paintComponent(Graphics g) {

// TODO Auto-generated method stub

                   super.paintComponent(g);

                   Graphics2D g2d = (Graphics2D)g;

                   //g2d.drawString("hello", 10,10);

                   g2d.drawRect(p.x, p.y, 50, 50);

         }

}

问题2:如何在鼠标点击窗体中绘制长方形?

解决问题方法:

1、  鼠标事件:鼠标事件需要用到两个接口MouseListenerMouseMotionListener,与鼠标的点击、进入、离开、按下和释放有关的就用MouseListenter,与鼠标移动和拖动有关的就用MouseMotionListener。根据题目的要求在这里我们使用MouseListener这个接口,在这个接口中有如下方法:

void

mouseClicked(MouseEvent e)
          鼠标按键在组件上单击(按下并释放)时调用。

void

mouseEntered(MouseEvent e)
          鼠标进入到组件上时调用。

void

mouseExited(MouseEvent e)
          鼠标离开组件时调用。

void

mousePressed(MouseEvent e)
          鼠标按键在组件上按下时调用。

void

mouseReleased(MouseEvent e)
          鼠标按钮在组件上释放时调用。

在这里我们可以选择mousePressedmouseClicked

2、  获得当前坐标并设置坐标

代码:

    public void mousePressed(MouseEvent e) {

        // TODO Auto-generated method stub

        Point p =e.getPoint();

        comp.setPoint(p);

        comp.repaint();

}

public class MyComp extends JComponent {

    Point p =null;

    public MyComp(){

        p = new Point();

    }

    public void move(){

        p.x+=10;

        p.y +=10;

    }

    public void setPoint(Point point){

        this.p = point;

    }

    public Point getPoint(){

        return this.p;

    }

    @Override

    protected void paintComponent(Graphics g) {

        // TODO Auto-generated method stub

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D)g;

        //g2d.drawString("hello", 10,10);

        System.out.println("aaa");

        g2d.drawRect(p.x, p.y, 50, 50);

    }

}

关键代码已经给出,其他代码大家自行补齐。

问题三:如何获得时间并让时间动起来?

1、  获得时间:需要用到Calendar类,通过查阅API得知,此类是抽象类,实例化抽象类有两种方法:一、是实例化子类的对象,二、通过调用本类的静态方法即可。

2、  如何使用线程,要使用java的线程就要学会使用一个接口和一个类,接口是Runable,类是Thread,通过查阅API得知,Thread类实现了该接口。根据以上的知识得出创建线程的两种方法:

1)  MyRunable runable = new MyRunable();//假定类MyRunable实现了Runable接口

Thread  t = new Thread(runable);

t.start();

                   2)  MyThread t = new MyThread();//假定MyThread继承了Thread

                            t.start();

完整代码如下:

public class Run {

    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub

//      MainFrame frame = new MainFrame();

//      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//      frame.setVisible(true);

        ShowTimeFrame frame = new ShowTimeFrame();

        frame.setVisible(true);

    }

}

import javax.swing.*;

public class ShowTimeFrame extends JFrame {

    public ShowTimeFrame(){

        JLabel lbl = new JLabel();

//      TimeThread t = new TimeThread(lbl);

//      Thread thread = new Thread(t);

    //thread.start();

        TimeThreadClass thread = new TimeThreadClass(lbl);

        thread.start();

        this.add(lbl);

        this.setSize(400,400);

    }

}

import java.util.Calendar;

import javax.swing.JLabel;

public class TimeThreadClass extends Thread {

    JLabel lbl =null;

    public TimeThreadClass(JLabel lbl){

        this.lbl = lbl;

    }

    public void run() {

        // TODO Auto-generated method stub

                while(true){

            Calendar date = Calendar.getInstance();

            lbl.setText("  "+date.get(Calendar.HOUR_OF_DAY)+":"+date.get(Calendar.MINUTE)+":"+date.get(Calendar.SECOND));

            try {

                Thread.currentThread().sleep(1000);

            } catch (InterruptedException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

}

问题四:如何让图形动起来?

解决方法:

1、使用线程

         2、如何在两个类传值,在两个类之间传值是通过构造方法进行;

完整代码:

import javax.swing.JFrame;

public class Run {

    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        MainFrame frame = new MainFrame();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }

}

import java.awt.BorderLayout;

import java.awt.Point;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class MainFrame extends JFrame implements ActionListener{

    MyComp comp=null;

    JButton btnStart=null,btnStop =null;

    public MainFrame(){

        comp = new MyComp();

        btnStart = new JButton("start");

        btnStop = new JButton("stop");

        JPanel p = new JPanel();

        p.add(btnStart);

        p.add(btnStop);

        btnStart.addActionListener(this);

        comp.setPoint(new Point(10,10));

        this.add(comp,BorderLayout.CENTER);

        this.add(p,BorderLayout.SOUTH);

        this.setSize(400, 400);

    }

    public void actionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub

        new PlayRunable(comp).start();

    }

//

    public void mousePressed(MouseEvent e) {

        // TODO Auto-generated method stub

        Point p =e.getPoint();

        comp.setPoint(p);

        comp.repaint();

    }

}

public class PlayRunable extends Thread {

    MyComp comp =null;

    public PlayRunable(MyComp comp){

        this.comp = comp;

    }

    public void run(){

        for(int i=0;i<400;i++){

            comp.move();

            try {

                Thread.sleep(1000);

            } catch (InterruptedException e1) {

                // TODO Auto-generated catch block

                e1.printStackTrace();

            }

            comp.repaint();

        }

    }

}

原创粉丝点击