JAVA第三次平时作业
作业6:
作业要求:
完成一个application图形界面程序。用户界面要求见如下两图:
功能说明:
file菜单—包括两个菜单项(图1):
open::打开.gif的图形文件,并显示在窗口里。文
件名在下面的编辑框中给出(如p1)
exit:结束程序。
edit菜单—包括两个菜单项(图2):
paint:在窗口中画图形,图形的类型及颜色分别由下面的
复选框及选择按扭指定。
clear:清空窗口内容。
该作业要求编程并调试通过。对你设计的类要有详细的文字说明(类所封装的内容,属性、方法的功能与含义等)。
代码:
1 import java.awt.BorderLayout; 2 import java.awt.Menu; 3 import java.awt.MenuBar; 4 import java.awt.MenuItem; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.WindowAdapter; 8 import java.awt.event.WindowEvent; 9 10 import javax.sound.sampled.LineUnavailableException; 11 import javax.swing.ButtonGroup; 12 import javax.swing.ImageIcon; 13 import javax.swing.JComboBox; 14 import javax.swing.JFileChooser; 15 import javax.swing.JFrame; 16 import javax.swing.JLabel; 17 import javax.swing.JPanel; 18 import javax.swing.JRadioButton; 19 import javax.swing.JTextArea; 20 import javax.swing.SwingConstants; 21 22 23 public class myFrame extends JFrame{ 24 25 private MenuBar menuBar; 26 27 //文件,编辑 28 private Menu file,edit; 29 //打开,退出,画图,清空 30 private MenuItem open,exit,paint,clear; 31 //paint:在窗口中画图形,图形的类型及颜色分别由下面的复选框及选择按扭指定。 32 33 //文件选择 34 private JFileChooser chooser; 35 private JLabel label; 36 //编辑框 37 private JTextArea text; 38 39 //组合框 40 private ButtonGroup buttonGroup; 41 private JComboBox box; 42 private JRadioButton radioButtonRed,radioButtonYellow,radioButtonGreen; 43 44 public myFrame() { 45 // TODO Auto-generated constructor stub 46 init(); 47 myEvent(); 48 } 49 50 public void init(){ 51 super.setTitle("Menu Frame");//创建窗体对象 52 super.setBounds(300,100,1000,1000);//设置窗体位置和大小 53 //frame.setVisible(true); 54 menuBar = new MenuBar(); 55 56 initMenu(); 57 58 initButton(); 59 60 label = new JLabel(); 61 getContentPane().add(label, BorderLayout.CENTER); 62 chooser = new JFileChooser(); 63 64 text = new JTextArea(1,50); 65 text.setEnabled(false);//功能菜单禁用 66 67 box = new JComboBox();//组合框 68 box.addItem("oval"); 69 box.addItem("rectangle"); 70 box.addItem("triangle"); 71 72 initPanel(); 73 74 setVisible(true); 75 } 76 77 78 private void initMenu(){ 79 file = new Menu("File"); 80 open = new MenuItem("open"); 81 file.add(open); 82 exit = new MenuItem("exit"); 83 file.add(exit); 84 menuBar.add(file); 85 86 edit = new Menu("edit"); 87 paint = new MenuItem("paint"); 88 edit.add(paint); 89 clear = new MenuItem("clear"); 90 edit.add(clear); 91 menuBar.add(edit); 92 93 super.setMenuBar(menuBar); 94 } 95 96 private void initButton(){ 97 radioButtonRed = new JRadioButton("red"); 98 radioButtonRed.setActionCommand("red"); 99 radioButtonYellow = new JRadioButton("Yellow"); 100 radioButtonYellow.setActionCommand("yellow"); 101 radioButtonGreen = new JRadioButton("Green"); 102 radioButtonGreen.setActionCommand("green"); 103 104 buttonGroup = new ButtonGroup(); 105 buttonGroup.add(radioButtonRed); 106 buttonGroup.add(radioButtonYellow); 107 buttonGroup.add(radioButtonGreen); 108 radioButtonRed.setSelected(true); 109 } 110 111 private void initPanel(){ 112 JPanel panel = new JPanel(); 113 getContentPane().add(panel, BorderLayout.SOUTH); 114 panel.add(text, BorderLayout.WEST); 115 panel.add(box, BorderLayout.CENTER); 116 panel.add(radioButtonYellow, BorderLayout.EAST); 117 panel.add(radioButtonRed, BorderLayout.EAST); 118 panel.add(radioButtonGreen, BorderLayout.EAST); 119 } 120 121 private void myEvent(){ 122 123 // 窗体关闭监听 124 super.addWindowListener(new WindowAdapter() { 125 public void windowClosing(WindowEvent e) { 126 System.exit(0); 127 128 } 129 130 }); 131 132 133 open.addActionListener(new ActionListener() { 134 135 @Override 136 public void actionPerformed(ActionEvent e) { 137 // TODO Auto-generated method stub 138 chooser = new JFileChooser(); 139 chooser.showOpenDialog(myFrame.this); 140 String fileName = chooser.getSelectedFile().getPath(); 141 ImageIcon image = new ImageIcon(fileName); 142 label.setIcon(image); 143 label.setHorizontalAlignment(SwingConstants.CENTER); 144 text.setText(fileName); 145 } 146 }); 147 148 //退出菜单项监听 149 exit.addActionListener(new ActionListener() 150 { 151 public void actionPerformed(ActionEvent e) 152 { 153 System.exit(0); 154 } 155 156 }); 157 158 paint.addActionListener(new ActionListener() { 159 160 @Override 161 public void actionPerformed(ActionEvent e) 162 { 163 text.setText(null); 164 String col = buttonGroup.getSelection().getActionCommand(); 165 String shape = (String) box.getSelectedItem(); 166 label.setIcon(new myShape(300,400,col,shape)); 167 label.setHorizontalAlignment(SwingConstants.CENTER); 168 } 169 }); 170 171 clear.addActionListener(new ActionListener() 172 { 173 public void actionPerformed(ActionEvent e) 174 { 175 label.setText(null); 176 label.setIcon(null); 177 text.setText(null); 178 } 179 }); 180 } 181 182 }
1 import java.awt.Color; 2 import java.awt.Component; 3 import java.awt.Graphics; 4 import java.awt.Polygon; 5 6 import javax.swing.Icon; 7 8 public class myShape implements Icon{ 9 private int width, height; 10 private String col,shape; 11 public myShape(int w, int h,String col,String shape){ 12 width = w; 13 height = h -200; 14 this.col = col; 15 this.shape = shape; 16 } 17 public void paintIcon(Component container, Graphics g, int p, int q){ 18 switch (col) { 19 case "red": 20 g.setColor(Color.red); 21 break; 22 case "yellow": 23 g.setColor(Color.yellow); 24 break; 25 case "green": 26 g.setColor(Color.GREEN); 27 break; 28 default: 29 break; 30 } 31 if (shape == "oval"){ 32 g.drawOval(p, q, width, height); 33 g.fillOval(p, q, width, height); 34 } 35 else if (shape == "rectangle"){ 36 g.drawRect(p, q, width, height); 37 g.fillRect(p, q, width, height); 38 } 39 else if (shape == "triangle"){ 40 Polygon polygon = new Polygon(); 41 polygon.addPoint(150, 500); 42 polygon.addPoint(500, 500); 43 polygon.addPoint(300, 200); 44 g.drawPolygon(polygon); 45 g.fillPolygon(polygon); 46 } 47 } 48 @Override 49 public int getIconHeight() { 50 // TODO Auto-generated method stub 51 return width; 52 } 53 @Override 54 public int getIconWidth() { 55 // TODO Auto-generated method stub 56 return height; 57 } 58 }
1 import java.awt.Frame; 2 import java.awt.Menu; 3 import java.awt.MenuBar; 4 import java.awt.MenuItem; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.WindowAdapter; 8 import java.awt.event.WindowEvent; 9 10 import javax.naming.InitialContext; 11 import javax.swing.ImageIcon; 12 import javax.swing.JFileChooser; 13 import javax.swing.JFrame; 14 import javax.swing.JLabel; 15 import javax.swing.JTextArea; 16 import javax.swing.SwingConstants; 17 18 import org.omg.CORBA.PRIVATE_MEMBER; 19 20 21 public class homework6 { 22 public static void main(String[] args){ 23 myFrame test = new myFrame(); 24 } 25 }
运行结果:
作业7:
作业要求:
代码:
1 import java.applet.Applet; 2 import java.awt.BorderLayout; 3 import java.awt.Color; 4 import java.awt.Component; 5 import java.awt.Frame; 6 import java.awt.Graphics; 7 import java.awt.Menu; 8 import java.awt.MenuBar; 9 import java.awt.MenuItem; 10 import java.awt.Polygon; 11 import java.awt.event.ActionEvent; 12 import java.awt.event.ActionListener; 13 import java.awt.event.WindowAdapter; 14 import java.awt.event.WindowEvent; 15 16 import javax.swing.*; 17 import javax.swing.Icon; 18 import javax.swing.ImageIcon; 19 import javax.swing.JApplet; 20 import javax.swing.JComboBox; 21 import javax.swing.JFileChooser; 22 import javax.swing.JFrame; 23 import javax.swing.JLabel; 24 import javax.swing.JPanel; 25 import javax.swing.JRadioButton; 26 import javax.swing.JTextArea; 27 import javax.swing.SwingConstants; 28 29 30 public class homework7 extends JApplet{ 31 JMenuBar menuBar; 32 JMenu file,edit; 33 JMenuItem open,exit,paint,clear; 34 JFileChooser chooser; 35 JLabel label; 36 JTextArea text; 37 ButtonGroup buttonGroup; 38 JComboBox box; 39 JRadioButton radioButtonRed,radioButtonYellow,radioButtonBlue; 40 41 42 public void init(){ 43 this.setLayout(new BorderLayout()); 44 //frame = new Frame("Menu Frame");//创建窗体对象 45 super.setBounds(300,100,1000,1000);//设置窗体位置和大小 46 //frame.setVisible(true); 47 menuBar = new JMenuBar(); 48 49 file = new JMenu("File"); 50 open = new JMenuItem("open"); 51 file.add(open); 52 exit = new JMenuItem("exit"); 53 file.add(exit); 54 menuBar.add(file); 55 56 edit = new JMenu("edit"); 57 paint = new JMenuItem("paint"); 58 edit.add(paint); 59 clear = new JMenuItem("clear"); 60 edit.add(clear); 61 menuBar.add(edit); 62 63 //super.setMenuBar(menuBar); 64 super.getContentPane().add(menuBar,BorderLayout.NORTH); 65 radioButtonRed = new JRadioButton("red"); 66 radioButtonRed.setActionCommand("red"); 67 radioButtonYellow = new JRadioButton("Yellow"); 68 radioButtonYellow.setActionCommand("yellow"); 69 radioButtonBlue = new JRadioButton("Green"); 70 radioButtonBlue.setActionCommand("green"); 71 72 buttonGroup = new ButtonGroup(); 73 buttonGroup.add(radioButtonRed); 74 buttonGroup.add(radioButtonYellow); 75 buttonGroup.add(radioButtonBlue); 76 radioButtonRed.setSelected(true); 77 78 label = new JLabel(); 79 super.getContentPane().add(label, BorderLayout.CENTER); 80 chooser = new JFileChooser(); 81 82 text = new JTextArea(1,50); 83 text.setEnabled(false);//功能菜单禁用 84 85 box = new JComboBox();//组合框 86 box.addItem("oval"); 87 box.addItem("rectangle"); 88 box.addItem("triangle"); 89 90 JPanel panel = new JPanel(); 91 getContentPane().add(panel, BorderLayout.SOUTH); 92 panel.add(text, BorderLayout.WEST); 93 panel.add(box, BorderLayout.CENTER); 94 panel.add(radioButtonYellow, BorderLayout.EAST); 95 panel.add(radioButtonRed, BorderLayout.EAST); 96 panel.add(radioButtonBlue, BorderLayout.EAST); 97 98 setVisible(true); 99 myEvent(); 100 } 101 102 103 104 private void myEvent(){ 105 106 107 108 109 open.addActionListener(new ActionListener() { 110 111 @Override 112 public void actionPerformed(ActionEvent e) { 113 // TODO Auto-generated method stub 114 chooser = new JFileChooser(); 115 chooser.showOpenDialog(homework7.this); 116 String fileName = chooser.getSelectedFile().getPath(); 117 ImageIcon image = new ImageIcon(fileName); 118 label.setIcon(image); 119 label.setHorizontalAlignment(SwingConstants.CENTER); 120 text.setText(fileName); 121 } 122 }); 123 124 //退出菜单项监听 125 exit.addActionListener(new ActionListener() 126 { 127 public void actionPerformed(ActionEvent e) 128 { 129 System.exit(0); 130 } 131 132 }); 133 134 paint.addActionListener(new ActionListener() { 135 136 @Override 137 public void actionPerformed(ActionEvent e) 138 { 139 text.setText(null); 140 String col = buttonGroup.getSelection().getActionCommand(); 141 String shape = (String) box.getSelectedItem(); 142 label.setIcon(new myShape(300,400,col,shape)); 143 label.setHorizontalAlignment(SwingConstants.CENTER); 144 } 145 }); 146 147 clear.addActionListener(new ActionListener() 148 { 149 public void actionPerformed(ActionEvent e) 150 { 151 label.setText(null); 152 label.setIcon(null); 153 text.setText(null); 154 } 155 }); 156 } 157 158 class myShape implements Icon{ 159 private int width, height; 160 private String col,shape; 161 public myShape(int w, int h,String col,String shape){ 162 width = w; 163 height = h; 164 this.col = col; 165 this.shape = shape; 166 } 167 public void paintIcon(Component container, Graphics g, int p, int q){ 168 switch (col) { 169 case "red": 170 g.setColor(Color.red); 171 break; 172 case "yellow": 173 g.setColor(Color.yellow); 174 break; 175 case "green": 176 g.setColor(Color.green); 177 break; 178 default: 179 break; 180 } 181 if (shape == "oval"){ 182 g.drawOval(p, q, width, height); 183 g.fillOval(p, q, width, height); 184 } 185 else if (shape == "rectangle"){ 186 g.drawRect(p, q, width, height); 187 g.fillRect(p, q, width, height); 188 } 189 else if (shape == "triangle"){ 190 Polygon polygon = new Polygon(); 191 polygon.addPoint(150, 300); 192 polygon.addPoint(500, 300); 193 polygon.addPoint(300, 0); 194 g.drawPolygon(polygon); 195 g.fillPolygon(polygon); 196 } 197 } 198 @Override 199 public int getIconHeight() { 200 // TODO Auto-generated method stub 201 return width; 202 } 203 @Override 204 public int getIconWidth() { 205 // TODO Auto-generated method stub 206 return height; 207 } 208 } 209 }
运行结果:
作业8:
作业要求:
随机画线
本作业的目的是希望大家把已经学过的有关GUI图形用户界面、Applet以及多线程的知识结合起来,完成一个简单的小功能。
界面如下:
功能概述:
两组Start、Stop按钮,分别控制两个区域;
点击Start,开始随机画线;点击Stop,暂停画线;再点击Start,继续画线;
点击clear,两个区域均清屏。
该作业要求编程并调试通过。对你设计的类要有详细的文字说明(类所封装的内容,属性、方法的功能与含义等)。
(舍友写这个题的时候还加了个随机线条颜色,感兴趣的同学也可以加上玩一玩233)
代码:
1 //package homework3_8; 2 import java.applet.Applet; 3 import java.awt.BorderLayout; 4 import java.awt.Graphics; 5 import java.awt.Panel; 6 import java.awt.Point; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.util.Random; 10 11 import javax.swing.JButton; 12 13 public class Main extends Applet{ 14 15 //左开始按钮,左停止按钮,右开始按钮,右停止按钮,清空按钮 16 JButton buttonLeftStart,buttonLeftStop,buttonRightStart,buttonRightStop,buttonClearAll; 17 18 //左侧区域 19 LeftArea leftArea; 20 21 //右侧区域 22 RightArea rightArea; 23 24 Graphics graphics; 25 Panel panel; 26 Boolean leftRunning,rightRunning; 27 28 public void init(){ 29 30 setSize(500, 300);//设置窗口大小 31 graphics = this.getGraphics(); 32 33 panel = new Panel(); 34 35 leftArea = new LeftArea(); 36 rightArea = new RightArea(); 37 38 leftRunning = rightRunning = false; 39 40 //左侧开始按钮 41 buttonLeftStart = new JButton("leftStart"); 42 buttonLeftStart.addActionListener(new ActionListener() { 43 44 @Override 45 public void actionPerformed(ActionEvent e) { 46 // TODO Auto-generated method stub 47 if (!leftRunning){//如果左侧没有在画线 48 if (!leftArea.start){//如果左侧线程未开始 49 leftArea.start = true; 50 leftArea.start();//左侧线程开始 51 } 52 //leftArea.running = true; 53 leftRunning = true; 54 } 55 } 56 }); 57 panel.add(buttonLeftStart); 58 59 //左侧停止按钮 60 buttonLeftStop = new JButton("leftStop"); 61 buttonLeftStop.addActionListener(new ActionListener() { 62 63 @Override 64 public void actionPerformed(ActionEvent e) { 65 // TODO Auto-generated method stub 66 leftRunning = false; 67 } 68 }); 69 panel.add(buttonLeftStop); 70 71 //右侧开始按钮 72 buttonRightStart = new JButton("rightStart"); 73 buttonRightStart.addActionListener(new ActionListener() { 74 75 @Override 76 public void actionPerformed(ActionEvent e) { 77 // TODO Auto-generated method stub 78 if (!rightRunning){//如果右侧没有在画线 79 if (!rightArea.start){//如果右侧线程没有开始 80 rightArea.start = true; 81 rightArea.start();//右侧线程开始 82 } 83 rightRunning = true; 84 } 85 } 86 }); 87 panel.add(buttonRightStart); 88 89 //右侧停止按钮 90 buttonRightStop = new JButton("rightStop"); 91 buttonRightStop.addActionListener(new ActionListener() { 92 93 @Override 94 public void actionPerformed(ActionEvent e) { 95 // TODO Auto-generated method stub 96 rightRunning = false; 97 } 98 }); 99 panel.add(buttonRightStop); 100 101 //清空按钮 102 buttonClearAll = new JButton("clear"); 103 buttonClearAll.addActionListener(new ActionListener() { 104 105 @Override 106 public void actionPerformed(ActionEvent e) { 107 // TODO Auto-generated method stub 108 leftRunning = false; 109 rightRunning = false; 110 graphics.clearRect(0, 0, 460, 250); 111 graphics.drawRect(20,20,200,200); 112 graphics.drawRect(250,20,200,200); 113 } 114 }); 115 panel.add(buttonClearAll); 116 117 setLayout(new BorderLayout()); 118 119 add("South",panel); 120 121 } 122 123 //画左右侧区域边界 124 public void paint(Graphics graphics){ 125 graphics.drawRect(20,20,200,200); 126 graphics.drawRect(250,20,200,200); 127 } 128 129 //画线 130 public void drawLine(Point x1,Point x2){ 131 graphics.drawLine(x1.x,x1.y,x2.x,x2.y); 132 } 133 134 //左侧区域类 135 class LeftArea extends Thread{ 136 Point a1 = new Point(); 137 Point a2 = new Point(); 138 public boolean start = false; 139 public void run(){ 140 while(true){ 141 //System.out.println(running); 142 if (leftRunning == true){ 143 144 //随机画线 145 Random random = new Random(); 146 a1.x = random.nextInt(200) + 20; 147 a1.y = random.nextInt(200) + 20; 148 a2.x = random.nextInt(200) + 20; 149 a2.y = random.nextInt(200) + 20; 150 drawLine(a1, a2); 151 } 152 try{ 153 sleep(666); 154 }catch(InterruptedException ie){ 155 System.err.println("Interrupted"); 156 } 157 } 158 } 159 160 } 161 162 //右侧区域类 163 class RightArea extends Thread{ 164 Point a1 = new Point(); 165 Point a2 = new Point(); 166 boolean start = false; 167 public void run(){ 168 while(true){ 169 //System.out.println(rightRunning); 170 if (rightRunning){ 171 172 //随机画线 173 Random random = new Random(); 174 a1.x = random.nextInt(200) + 250; 175 a1.y = random.nextInt(200) + 20; 176 a2.x = random.nextInt(200) + 250; 177 a2.y = random.nextInt(200) + 20; 178 drawLine(a1, a2); 179 } 180 try{ 181 sleep(666); 182 }catch(InterruptedException ie){ 183 System.err.println("Interrupted"); 184 } 185 } 186 } 187 } 188 189 190 }
运行结果:
作业9:
作业要求:
1. 设计一个交通信号灯系统:
变量:位置、颜色(红、黄、绿)、显示时间(秒)
方法:切换信号灯
要求:
设计路口信号灯示意图界面 。
创建并启动两个线程(东西向、南北向)同时运行
根据车流量进行控制。
该作业要求编程并调试通过。对你设计的类要有详细的文字说明(类所封装的内容,属性、方法的功能与含义等)。
基础较好的同学可考虑
进一步将每个方向的信号灯分成3种车道灯:左转、直行和右转。
代码:
(魔改的代码2333)
1 //package homework9; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import java.util.Date; 8 import java.util.Random; 9 10 /* 11 利用“改变车流量”按钮随机更改车流量,红绿灯根据车流量多少进行倒计时 12 */ 13 class roadpanel extends JPanel { 14 15 //northlight为由北驶来的车辆所遵守的路灯,位于最南端 16 //eastlight为由东驶来的车辆所遵守的路灯,位于最西端 17 //southlight为由南驶来的车辆所遵守的路灯,位于最北端 18 //westlight为由西驶来的车辆所遵守的路灯,位于最东端 19 JLabel northLight1,northLight2,northLight3, 20 eastLight1,eastLight2,eastLight3, 21 southLight1,southLight2,southLight3, 22 westLight1,westLight2,westLight3,l,per; 23 Font font1,font2,font3,font4; 24 JPanel pn,pe,ps,pw; 25 26 //n,e,s,w为东南西北四个路灯的倒计时 27 JTextField n,e,s,w,liu,time; 28 29 //改变车流量的按钮 30 JButton changeNum; 31 32 //num记录车流量 33 int num=10,r; 34 String np=""; 35 public roadpanel(){ 36 setLayout(null); 37 38 //初始化字体 39 initFont(); 40 41 pn=new JPanel(); 42 pe=new JPanel(); 43 ps=new JPanel(); 44 pw=new JPanel(); 45 n=new JTextField("00",3); 46 e=new JTextField("00",3); 47 s=new JTextField("00",3); 48 w=new JTextField("00",3); 49 liu=new JTextField("",3); 50 time=new JTextField("0",3); 51 changeNum = new JButton("改变车流量"); 52 53 changeNum.addActionListener(new ActionListener() { 54 55 @Override 56 public void actionPerformed(ActionEvent e) { 57 // TODO Auto-generated method stub 58 59 //随机更改车流量 60 Random random = new Random(); 61 num = random.nextInt(10) + 5; 62 liu.setText(String.valueOf(num)); 63 time.setText(String.valueOf(num)); 64 } 65 }); 66 67 initLight(); 68 69 l=new JLabel("当前流量",SwingConstants.CENTER); 70 per=new JLabel("辆/小时",SwingConstants.CENTER); 71 add(changeNum); 72 add(pn); 73 add(pe); 74 add(ps); 75 add(pw); 76 add(l); 77 add(per); 78 add(liu); 79 add(time); 80 changeNum.setBounds(310, 200, 100, 50); 81 82 //显示当前车流量信息 83 l.setBounds(450,50,120,30); 84 liu.setBounds(570,50,50,30); 85 per.setBounds(620,50,120,30); 86 87 88 time.setBounds(100,100,100,30); 89 time.setVisible(false); 90 l.setFont(font3); 91 liu.setFont(font3); 92 liu.setText(String.valueOf(num)); 93 per.setFont(font3); 94 95 initPanel(); 96 97 //东南西北四个路灯的倒计时 98 n.setFont(font1); 99 n.setForeground(Color.red); 100 n.setBackground(Color.WHITE); 101 e.setFont(font2); 102 e.setForeground(Color.red); 103 e.setBackground(Color.WHITE); 104 s.setFont(font1); 105 s.setForeground(Color.red); 106 s.setBackground(Color.WHITE); 107 w.setFont(font2); 108 w.setForeground(Color.red); 109 w.setBackground(Color.WHITE); 110 } 111 112 public void initPanel(){ 113 //初始化panel 114 pn.setBounds(270,400,184,45); 115 pn.setLayout(new GridLayout(1,4,1,0)); 116 pn.setBackground(Color.darkGray); 117 pe.setBounds(100,150,40,164); 118 pe.setLayout(new GridLayout(4,1,0,1)); 119 pe.setBackground(Color.darkGray); 120 ps.setBounds(270,50,184,45); 121 ps.setLayout(new GridLayout(1,4,1,0)); 122 ps.setBackground(Color.darkGray); 123 pw.setBounds(550,150,40,164); 124 pw.setLayout(new GridLayout(4,1,0,1)); 125 pw.setBackground(Color.darkGray); 126 pn.add(northLight1); 127 pn.add(northLight2); 128 pn.add(northLight3); 129 pn.add(n); 130 pe.add(eastLight1); 131 pe.add(eastLight2); 132 pe.add(eastLight3); 133 pe.add(e); 134 ps.add(s); 135 ps.add(southLight1); 136 ps.add(southLight2); 137 ps.add(southLight3); 138 pw.add(w); 139 pw.add(westLight1); 140 pw.add(westLight2); 141 pw.add(westLight3); 142 } 143 144 public void initLight(){ 145 //初始化路灯 146 northLight1=new JLabel("●",SwingConstants.CENTER); 147 northLight1.setFont(font1); 148 northLight1.setForeground(Color.darkGray); 149 northLight2=new JLabel("●",SwingConstants.CENTER); 150 northLight2.setFont(font1); 151 northLight2.setForeground(Color.darkGray); 152 northLight3=new JLabel("●",SwingConstants.CENTER); 153 northLight3.setFont(font1); 154 northLight3.setForeground(Color.darkGray); 155 eastLight1=new JLabel("●",SwingConstants.CENTER); 156 eastLight1.setFont(font2); 157 eastLight1.setForeground(Color.darkGray); 158 eastLight2=new JLabel("●",SwingConstants.CENTER); 159 eastLight2.setFont(font2); 160 eastLight2.setForeground(Color.darkGray); 161 eastLight3=new JLabel("●",SwingConstants.CENTER); 162 eastLight3.setFont(font2); 163 eastLight3.setForeground(Color.darkGray); 164 southLight1=new JLabel("●",SwingConstants.CENTER); 165 southLight1.setFont(font1); 166 southLight1.setForeground(Color.darkGray); 167 southLight2=new JLabel("●",SwingConstants.CENTER); 168 southLight2.setFont(font1); 169 southLight2.setForeground(Color.darkGray); 170 southLight3=new JLabel("●",SwingConstants.CENTER); 171 southLight3.setFont(font1); 172 southLight3.setForeground(Color.darkGray); 173 westLight1=new JLabel("●",SwingConstants.CENTER); 174 westLight1.setFont(font2); 175 westLight1.setForeground(Color.darkGray); 176 westLight2=new JLabel("●",SwingConstants.CENTER); 177 westLight2.setFont(font2); 178 westLight2.setForeground(Color.darkGray); 179 westLight3=new JLabel("●",SwingConstants.CENTER); 180 westLight3.setFont(font2); 181 westLight3.setForeground(Color.darkGray); 182 } 183 184 public void initFont(){ 185 font1=new Font("黑体",Font.BOLD,40); 186 font2=new Font("黑体",Font.BOLD,35); 187 font3=new Font("黑体",Font.BOLD,25); 188 font4=new Font("黑体",Font.BOLD,15); 189 } 190 191 } 192 class Controlew implements Runnable{ 193 //控制东西走向的路灯 194 private int n,tp=0; 195 private String s="",t=""; 196 private roadpanel rpanel; 197 Date date; 198 boolean v=true; 199 public Controlew(roadpanel rp){ 200 rpanel=rp; 201 } 202 public void run(){ 203 while(true){ 204 synchronized(this){ 205 if(!v){ 206 try{ 207 this.wait(); 208 }catch(Exception e) 209 {e.printStackTrace();} 210 } 211 rpanel.northLight1.setForeground(Color.green); 212 rpanel.northLight2.setForeground(Color.red); 213 rpanel.northLight3.setForeground(Color.red); 214 rpanel.n.setForeground(Color.red); 215 rpanel.eastLight1.setForeground(Color.green); 216 rpanel.eastLight2.setForeground(Color.green); 217 rpanel.eastLight3.setForeground(Color.green); 218 rpanel.e.setForeground(Color.green); 219 rpanel.southLight1.setForeground(Color.red); 220 rpanel.southLight2.setForeground(Color.red); 221 rpanel.southLight3.setForeground(Color.green); 222 rpanel.s.setForeground(Color.red); 223 rpanel.westLight1.setForeground(Color.green); 224 rpanel.westLight2.setForeground(Color.green); 225 rpanel.westLight3.setForeground(Color.green); 226 rpanel.w.setForeground(Color.green); 227 tp=Integer.parseInt(rpanel.time.getText()); 228 if(tp!=0) 229 {n=tp+1;} 230 else 231 {n=11;} 232 t.indexOf(n); 233 rpanel.n.setText(t); 234 rpanel.s.setText(t); 235 rpanel.e.setText(t); 236 rpanel.w.setText(t); 237 while(true){ 238 date=new Date(); 239 //rpanel.d.setText(date.toString()); 240 try{ 241 Thread.currentThread().sleep(1000); 242 }catch(Exception e){} 243 if(n==4) 244 { 245 rpanel.eastLight2.setForeground(Color.yellow); 246 rpanel.eastLight3.setForeground(Color.yellow); 247 rpanel.westLight2.setForeground(Color.yellow); 248 rpanel.westLight1.setForeground(Color.yellow); 249 } 250 else 251 if(n==1) 252 break; 253 n--; 254 if(n<10) 255 s="0"+s.valueOf(n); 256 else 257 s=s.valueOf(n); 258 rpanel.n.setText(s); 259 rpanel.s.setText(s); 260 rpanel.e.setText(s); 261 rpanel.w.setText(s); 262 } 263 v=false; 264 this.notify(); 265 266 } 267 } 268 } 269 } 270 class Controlns implements Runnable{ 271 //控制南北走向的路灯 272 private int i=0,p,n,e,tp; 273 private String s="",w="",t=""; 274 private roadpanel rpanel; 275 private Controlew ew; 276 Date date; 277 public Controlns(roadpanel rp,Controlew ew){ 278 rpanel=rp; 279 this.ew=ew; 280 } 281 public void run(){ 282 283 while(true){ 284 synchronized(ew){ 285 286 if(ew.v){ 287 try{ 288 ew.wait(); 289 }catch(Exception e) 290 {e.printStackTrace();} 291 } 292 rpanel.northLight1.setForeground(Color.green); 293 rpanel.northLight2.setForeground(Color.green); 294 rpanel.northLight3.setForeground(Color.green); 295 rpanel.n.setForeground(Color.green); 296 rpanel.eastLight1.setForeground(Color.green); 297 rpanel.eastLight2.setForeground(Color.red); 298 rpanel.eastLight3.setForeground(Color.red); 299 rpanel.e.setForeground(Color.red); 300 rpanel.southLight1.setForeground(Color.green); 301 rpanel.southLight2.setForeground(Color.green); 302 rpanel.southLight3.setForeground(Color.green); 303 rpanel.s.setForeground(Color.green); 304 rpanel.westLight1.setForeground(Color.red); 305 rpanel.westLight2.setForeground(Color.red); 306 rpanel.westLight3.setForeground(Color.green); 307 rpanel.w.setForeground(Color.red); 308 tp=Integer.parseInt(rpanel.time.getText()); 309 if(tp!=0) 310 {n=tp+1;} 311 else 312 {n=rpanel.num;} 313 t.indexOf(n); 314 rpanel.n.setText(t); 315 rpanel.s.setText(t); 316 rpanel.e.setText(t); 317 rpanel.w.setText(t); 318 while(true) 319 { 320 date=new Date(); 321 //rpanel.d.setText(date.toString()); 322 try{ 323 Thread.currentThread().sleep(1000); 324 }catch(Exception e){} 325 if(n==4) 326 { 327 rpanel.northLight2.setForeground(Color.yellow); 328 rpanel.northLight3.setForeground(Color.yellow); 329 rpanel.southLight2.setForeground(Color.yellow); 330 rpanel.southLight1.setForeground(Color.yellow); 331 } 332 else 333 if(n==1) 334 break; 335 n--; 336 if(n<10) 337 s="0"+s.valueOf(n); 338 else 339 s=s.valueOf(n); 340 rpanel.n.setText(s); 341 rpanel.s.setText(s); 342 rpanel.e.setText(s); 343 rpanel.w.setText(s); 344 ew.v=true; 345 ew.notify(); 346 } 347 } 348 } 349 } 350 } 351 public class Main{ 352 public static void main(String[] args){ 353 roadpanel rp=new roadpanel(); 354 Controlew cew=new Controlew(rp); 355 Controlns cns=new Controlns(rp,cew); 356 final Thread ns=new Thread(cns); 357 final Thread ew=new Thread(cew); 358 JFrame frame=new JFrame("交通信号灯"); 359 frame.setSize(800, 600); 360 frame.add(rp); 361 frame.setVisible(true); 362 ns.start(); 363 ew.start(); 364 } 365 }
运行结果: