编写一个Java多线程GUI程序(GUIThread.java),有两个按钮,点击第一个按钮、启动一个线程开始计时,并把

编写一个Java多线程GUI程序(GUIThread.java),有两个按钮,点击第一个按钮、启动一个线程开始计时,并把,第1张

你要的是这个吧:

import java.awt.GridLayout

import java.awt.event.ActionEvent

import java.text.DateFormat

import java.util.Date

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

public class GUIThread extends JFrame implements java.awt.event.ActionListener{

private JButton jb1 = new JButton("计时")

private JButton jb2 = new JButton("当前时间")

private JLabel jl1 = new JLabel("")

private JLabel jl2 = new JLabel(""悄敏)

public GUIThread(){

super("GUIThread")

this.setSize(300,200)

this.setLocation(400, 400)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

this.setLayout(new GridLayout(2,2))

this.jb1.addActionListener(this)

this.jb2.addActionListener(this)

this.add(jb1)

this.add(jl1)

this.add(jb2)

this.add(jl2)

this.setVisible(true)

}

public static void main(String[] args){

new GUIThread()

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

Object jb = e.getSource()

if(jb == jb1){

new TimingTime(jl1).start()

jb1.setEnabled(false)

}else{

new NowTime(jl2).start()

}

}

}

class NowTime extends Thread{

private JLabel jl

public NowTime(JLabel jl){

this.jl = jl

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

try {

sleep(1000)

} catch (InterruptedException e) {

/岁兆/ TODO Auto-generated catch block

e.printStackTrace()

}

Date d = new Date()

this.jl.setText(d.getYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"启雀枝+d.getSeconds())

}

}

}

class TimingTime extends Thread{

private JLabel jl

private long l = new Date().getTime()

public TimingTime(JLabel jl){

this.jl = jl

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

try {

sleep(1000)

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

int emp = 1000

int i = (int) ((new Date().getTime() - l)/emp)

jl.setText(String.valueOf(i))

}

}

}

简单做了一个

import java.awt.BorderLayout

import java.awt.Button

import java.awt.Color

import java.awt.Frame

import java.awt.Graphics

import java.awt.Panel

import java.awt.Point

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

import java.awt.image.BufferedImage

import java.util.ArrayList

import java.util.List

public class MyFrame extends Frame implements Runnable,ActionListener{

double x1 = 100,y1 = 0//A的起始位置

double x2 = 100,y2 = 0//B的起始位置

double s2 = 26//B的水平速度

double g = 9.8//G

long time = 10000/桥或/模拟10秒钟

double py = 1//y轴比例尺握搭

List<Point>list1 = new ArrayList<Point>()

List<Point>段消拿 list2 = new ArrayList<Point>()

MPanel p1 = new MPanel()

Button b1 = new Button("启动")

Thread t

public MyFrame(){

b1.addActionListener(this)

this.add(b1,BorderLayout.NORTH)

this.add(p1,BorderLayout.CENTER)

this.setSize(800,600)

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {

System.exit(0)

}})

this.setVisible(true)

}

public void run(){

list1 = new ArrayList<Point>()

list2 = new ArrayList<Point>()

double xx1 = x1,yy1 = y1,xx2 = x2,yy2 = y2//本时刻位置

long start = System.currentTimeMillis()

long end = start

while(end-start<=time){

end = System.currentTimeMillis()

double t = (end - start)/1000.0

yy1 = (y1 + g*t*t/2)*py

xx2 = x1 + s2*t

yy2 = (y2 + g*t*t/2)*py

int sx1 = (int)xx1

int sy1 = (int)yy1

int sx2 = (int)xx2

int sy2 = (int)yy2

list1.add(new Point(sx1,sy1))

list2.add(new Point(sx2,sy2))

p1.repaint()

try {

Thread.sleep(50)

} catch (InterruptedException e) {

e.printStackTrace()

}

}

}

public void actionPerformed(ActionEvent e) {

if (t==null||!t.isAlive()){

t = new Thread(this)

t.start()

}

}

public class MPanel extends Panel{

BufferedImage im = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB)

public void paint(Graphics gg){

Graphics g = im.getGraphics()

g.setColor(Color.white)

g.fillRect(0,0,800,600)

g.setColor(Color.blue)

Point ppp1 = null

Point ppp2 = null

for (int i=0i<list1.size()i++){

Point pp1 = list1.get(i)

Point pp2 = list2.get(i)

if (ppp1!=null&&ppp2!=null){

g.drawLine(pp1.x,pp1.y,ppp1.x,ppp1.y)

g.drawLine(pp2.x,pp2.y,ppp2.x,ppp2.y)

}

ppp1 = pp1

ppp2 = pp2

}

if (ppp1!=null&&ppp2!=null){

g.fillOval(ppp1.x-5,ppp1.y-5,10,10)

g.fillOval(ppp2.x-5,ppp2.y-5,10,10)

}

g.dispose()

gg.drawImage(im,0,0,800,600,p1)

}

}

public static void main(String[] args) {

new MyFrame()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8267843.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存