62,614
社区成员
发帖
与我相关
我的任务
分享
public class Thread_JPanel extends Container implements Runnable {
public void launchframe() {
setSize(700, 700);
setLayout(null);
setLocation((int) x, (int) y);
setVisible(true);
}
public void paint(Graphics g) {
g.drawImage(car, getx(), gety(), 90, 90, null);
//System.out.println(getx()+" "+gety());
}
public void run() {
while (true) {
repaint();
}
}
}
public class Change_x_y implements Runnable{
public void run() {
while (true) {
System.out.println(x + " " + y);
if (x >= 610 || y >= 610) {
x = y = 0;
} else {
x += (double) speed / 1000 * fresh_time;
y += (double) speed / 1000 * fresh_time;
}
try {
sleep(fresh_time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Form() {
// 设置主窗体
Setform();
// 检查是否能创建系统托盘。否,则退出程序
Check_SystemTray();
// 如果可以,则创建弹出菜单以及系统托盘的对象,并设定相应的事件
try {
SetTray();
} catch (AWTException e1) {
e1.printStackTrace();
}
// 向窗口添加其他组件,并设置事件
SetOther();
b.launchframe();
thread.start();
form.add(b);
thread1.start();
form.setVisible(true);
}
public void SetTray() throws AWTException {
PopupMenu pop = new PopupMenu();
MenuItem open = new MenuItem("Show");
MenuItem close = new MenuItem("Exit");
// 创建一个位置跟随控件的弹出菜单,并添加两个菜单选项(“打开”,“退出”)
// jpopupmenu的弹出窗口位置,在JFrame中固定,不能被添加到托盘图标上,所以使用组件popupmenu
pop.add(open);
pop.add(close);
// 给pop菜单添加两个菜单选项
// 创建托盘图标
TrayIcon trayicon = new TrayIcon(car, "Java系统托盘", pop);
// TrayIcon 类的构造方法,分别是图片地址,提示信息,菜单
trayicon.setImageAutoSize(true);
SystemTray systemTray = SystemTray.getSystemTray();
systemTray.add(trayicon);
// 菜单双击选项
trayicon.addMouseListener(new MouseAdapter() {
// 鼠标事件
public void mouseClicked(MouseEvent e) {
// 判断是否双击了鼠标
if (e.getClickCount() == 2) {
form.setVisible(true);
}
}
});
// 菜单选项open 的事件监听器,并执行的操作
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
form.setVisible(true);
}
});
// 菜单选项close 的事件监听器,并执行的操作
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public class Car extends JFrame {
panel p = new panel();
static JFrame jf=new JFrame();
static Image car = Toolkit.getDefaultToolkit().getImage("src/images/car.jpg");
Car() {
initialize();
}
private void initialize() {
jf.setSize(700, 700);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体关闭模式
jf.setContentPane(p);
jf.setVisible(true);// 设置窗体的可见性
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Car c=new Car();
try {
c.SetTray();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
c.p.Change_x_y();d
try {
new Thread().sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.p.repaint();
}
}
public static void SetTray() throws AWTException {
PopupMenu pop = new PopupMenu();
MenuItem open = new MenuItem("Show");
MenuItem close = new MenuItem("Exit");
pop.add(open);
pop.add(close);
TrayIcon trayicon = new TrayIcon(car, "Java系统托盘", pop);
trayicon.setImageAutoSize(true);
SystemTray systemTray = SystemTray.getSystemTray();
systemTray.add(trayicon);
// 菜单双击选项
trayicon.addMouseListener(new MouseAdapter() {
// 鼠标事件
public void mouseClicked(MouseEvent e) {
// 判断是否双击了鼠标
if (e.getClickCount() == 2) {
jf.setVisible(true);
}
}
});
// 菜单选项open 的事件监听器,并执行的操作
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jf.setVisible(true);
}
});
// 菜单选项close 的事件监听器,并执行的操作
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
class panel extends JPanel {
Image car = Toolkit.getDefaultToolkit().getImage("src/images/car.jpg");
private int x, y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
Graphics2D gp = (Graphics2D) g;
gp.drawImage(car, getX(), getY(), 90, 90, this);
}
public void Change_x_y() {
System.out.println(x + " " + y);
if (x >= 610 || y >= 610) {
x = y = 0;
} else {
x += 5;
y += 5;
}
}
}