【急求】高手帮忙编写红绿灯的JAVA程序,有图形界面灯会跳的那种!
本下午在线等!我现有红绿灯的程序,有红绿灯的图形界面程序也行,求高手帮忙!有好的答案可追加30财富值...
本下午在线等!我现有红绿灯的程序,有红绿灯的图形界面程序也行,求高手帮忙!有好的答案可追加30财富值
展开
1个回答
展开全部
看一下这个行不行?
--------------------------------------------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class App extends JFrame {
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(107, 252);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout(0, 0));
MyColorPanel panel = new MyColorPanel();
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
new Thread(panel).start();
}
public static void main(String[] args) {
new App();
}
}
class MyColorPanel extends JPanel implements Runnable {
private Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
private int[] y = { 10, 70, 130 };
private int[] time = { 3, 1, 5 };
private int index = 2;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = 50;
int x = 30;
for (int i = 0; i < y.length; i++) {
g.fillOval(x, y[i], width, width);
}
g.setColor(colors[index]);
g.fillOval(x, y[index], width, width);
}
public void run() {
while (true) {
try {
Thread.sleep(time[index] * 1000);
index--;
repaint();
if (index < 0) {
index = 2;
}
} catch (Exception e) {
}
}
}
}
--------------------------------------------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class App extends JFrame {
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(107, 252);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout(0, 0));
MyColorPanel panel = new MyColorPanel();
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
new Thread(panel).start();
}
public static void main(String[] args) {
new App();
}
}
class MyColorPanel extends JPanel implements Runnable {
private Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
private int[] y = { 10, 70, 130 };
private int[] time = { 3, 1, 5 };
private int index = 2;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = 50;
int x = 30;
for (int i = 0; i < y.length; i++) {
g.fillOval(x, y[i], width, width);
}
g.setColor(colors[index]);
g.fillOval(x, y[index], width, width);
}
public void run() {
while (true) {
try {
Thread.sleep(time[index] * 1000);
index--;
repaint();
if (index < 0) {
index = 2;
}
} catch (Exception e) {
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询