java小程序,模拟电视机和遥控

简单说说,程序的主要使用是用来模拟实现电视机和遥控之间的操作的。总所周知,电视机关闭之后频道会保存。这里会用txt来记录数组,用gui模拟电视机和遥控器。而且,每个电视台的音量是分开储存的。

分成4个部分组成。box.java是存放main的,data.java用来负责数据的输入输出的。tvbox.java和remote.java分别表示的是电视机和遥控器.

代码如下:

box.java

 1 public class box {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         new tvbox();  
 9         new remote();  
10     }
11 
12 }

简单地调用remote()和tvbox()两个class.

 

data.java

在C盘创建data.txt和data_channel.txt用来存放电视台和各个电视台的频道的数据,开机的时候可以读取.用流对每个台的音量以及现在在看的电视台进行读写.

  1 import java.io.*;
  2 public class data {
  3     public static BufferedReader bufread;
  4     private static String path = "C:/data.txt";
  5     private static String path_channel = "C:/data_channel.txt";
  6     private static  File filename = new File(path);
  7     private static  File filename_channel = new File(path_channel);
  8    
  9     
 10     public static void creatTxtFile() throws IOException{
 11         if (!filename.exists()) {
 12             filename.createNewFile();
 13             for(int i = 0; i <30; i++)
 14                 tvbox.volume[i] = 10;
 15             data.write();
 16             System.err.println(filename + "已创建!");
 17         }
 18     }
 19     public static void creatTxtFile_channel() throws IOException{
 20         if (!filename_channel.exists()) {
 21             filename_channel.createNewFile();
 22             tvbox.channel_now = 0;
 23             data.write_channel();
 24             
 25             System.err.println(filename_channel + "已创建!");
 26         }
 27     }
 28     
 29     
 30     
 31     
 32     public static String read_test() throws IOException
 33     {
 34         BufferedReader in = new BufferedReader(new FileReader(filename)); 
 35           String line;  
 36           /*while((line = in.readLine()) != null){
 37            String[] temp = line.split("\t"); 
 38            for(int j=0;j<temp.length;j++){
 39             remoter.volume[j] = Integer.parseInt(temp[j]);
 40            }   
 41           }*/
 42           //
 43           line = in.readLine();
 44           String[] temp = line.split("\t");
 45           for(int j=0; j<temp.length; j++)
 46           {
 47               tvbox.volume[j] = Integer.parseInt(temp[j]);
 48           }
 49           //
 50           in.close();
 51           System.err.println(line);
 52           return line;
 53           
 54     }
 55     public static String read_channel_test() throws IOException
 56     {
 57         BufferedReader in = new BufferedReader(new FileReader(filename_channel));  
 58           String line;  
 59           /*while((line = in.readLine()) != null){
 60            String[] temp = line.split("\t"); 
 61            for(int j=0;j<temp.length;j++){
 62             remoter.channel_now = Integer.parseInt(temp[j]);
 63            }    
 64     }
 65     */
 66           //
 67           line = in.readLine();
 68           String[] temp = line.split("\t");
 69           for(int j=0; j<temp.length; j++)
 70           {
 71               tvbox.channel_now = Integer.parseInt(temp[j]);
 72           }
 73           //
 74           
 75           in.close();
 76           System.err.println(line);
 77           return line;
 78     }
 79     
 80     public static void write_channel() throws IOException{
 81         String newString="";
 82         newString += tvbox.channel_now +"\t";
 83         String filein = newString + "\r\n";
 84         String string_null = "";
 85         RandomAccessFile mm = null;
 86         try {
 87             mm = new RandomAccessFile(filename_channel, "rw");
 88             mm.writeBytes(string_null);
 89             mm.writeBytes(filein);
 90         } catch (IOException e1) {
 91             // TODO 自动生成 catch 块
 92             e1.printStackTrace();
 93         } finally {
 94             if (mm != null) {
 95                 try {
 96                     mm.close();
 97                 } catch (IOException e2) {
 98                     // TODO 自动生成 catch 块
 99                     e2.printStackTrace();
100                 }
101             }
102         }
103     }
104     
105     public static void write() throws IOException{
106         String newString="";
107         for(int i = 0; i < 30 ; i++)
108         {
109             newString += tvbox.volume[i]+"\t";
110         }
111         String filein = newString + "\r\n";
112         String string_null = "";
113         RandomAccessFile mm = null;
114         try {
115             mm = new RandomAccessFile(filename, "rw");
116             mm.writeBytes(string_null);
117             mm.writeBytes(filein);
118         } catch (IOException e1) {
119             // TODO 自动生成 catch 块
120             e1.printStackTrace();
121         } finally {
122             if (mm != null) {
123                 try {
124                     mm.close();
125                 } catch (IOException e2) {
126                     // TODO 自动生成 catch 块
127                     e2.printStackTrace();
128                 }
129             }
130         }
131     }
132     
133     
134     
135     
136 
137 }

 

remote.java

创建GUI,其中例如

 gl1=new GridLayout(3,1,0,15);
意思就是三行一列,水平距离为0,竖直距离为15.
p1.setBounds(170, 45, 60, 150);意思是以左上角作为起始坐标,横坐标为170,纵坐标为45,创建一个60x150的GUI区域.
Bt class用来监视GUI的状况,鼠标的反应反馈到Bt class用来作为处理的指令,类似switch函数,不过这个switch是面向整个GUI区域的而不是一个个字符的命令.
  1 import java.awt.*; 
  2 import java.awt.event.*; 
  3 
  4 public class remote extends Frame {
  5 
  6     /**
  7      * @param args
  8      */
  9     private static final long serialVersionUID = 22;
 10     GridLayout gl0, gl1,gl2;
 11     Panel p0,p1,p2;
 12     Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b_cancel,b_ok,b_turn_on,b_turn_off,b_channel_up,b_channel_down,b_volume_up,b_volume_down, b_random;
 13     StringBuffer str;
 14     
 15     public remote()
 16     {
 17         gl0=new GridLayout(4,5,10,15);
 18         gl1=new GridLayout(3,1,0,15);
 19         gl2=new GridLayout(4,1,0,15);
 20         
 21         b0 = new Button("0");
 22         b0.setForeground(Color.blue);
 23         b0.addActionListener(new Bt());
 24         b1 = new Button("1");
 25         b1.setForeground(Color.blue);
 26         b1.addActionListener(new Bt());
 27         b2 = new Button("2");
 28         b2.setForeground(Color.blue);
 29         b2.addActionListener(new Bt());
 30         b3 = new Button("3");
 31         b3.setForeground(Color.blue);
 32         b3.addActionListener(new Bt());
 33         b4 = new Button("4");
 34         b4.setForeground(Color.blue);
 35         b4.addActionListener(new Bt());
 36         b5 = new Button("5");
 37         b5.setForeground(Color.blue);
 38         b5.addActionListener(new Bt());
 39         b6 = new Button("6");
 40         b6.setForeground(Color.blue);
 41         b6.addActionListener(new Bt());
 42         b7 = new Button("7");
 43         b7.setForeground(Color.blue);
 44         b7.addActionListener(new Bt());
 45         b8 = new Button("8");
 46         b8.setForeground(Color.blue);
 47         b8.addActionListener(new Bt());
 48         b9 = new Button("9");
 49         b9.setForeground(Color.blue);
 50         b9.addActionListener(new Bt());
 51         b_cancel= new Button("C");
 52         b_cancel.setForeground(Color.blue);
 53         b_cancel.addActionListener(new Bt());
 54         b_turn_on = new Button("Turn On");
 55         b_turn_on.setForeground(Color.blue);
 56         b_turn_on.addActionListener(new Bt());    
 57         b_turn_off= new Button("Turn Off");
 58         b_turn_off.setForeground(Color.blue);
 59         b_turn_off.addActionListener(new Bt());
 60         b_volume_up = new Button("Volume +");
 61         b_volume_up.setForeground(Color.blue);
 62         b_volume_up.addActionListener(new Bt());
 63         b_volume_down= new Button("Volume -");
 64         b_volume_down.setForeground(Color.blue);
 65         b_volume_down.addActionListener(new Bt());
 66         b_channel_up= new Button("Channel +");
 67         b_channel_up.setForeground(Color.blue);
 68         b_channel_up.addActionListener(new Bt());
 69         b_channel_down= new Button("Channel -");
 70         b_channel_down.setForeground(Color.blue);
 71         b_channel_down.addActionListener(new Bt());
 72         b_ok= new Button("OK");
 73         b_ok.setForeground(Color.blue);
 74         b_ok.addActionListener(new Bt());
 75         b_cancel= new Button("C");
 76         b_cancel.setForeground(Color.blue);
 77         b_cancel.addActionListener(new Bt());
 78         b_random= new Button("搜索");
 79         b_random.setForeground(Color.blue);
 80         b_random.addActionListener(new Bt());
 81         
 82         p0 = new Panel();
 83         p1 = new Panel();
 84         p2 = new Panel();
 85         
 86         str = new StringBuffer();
 87         
 88         p0.setLayout(gl0);
 89         p0.add(b7);
 90         p0.add(b8);
 91         p0.add(b9);
 92         p0.add(b4);
 93         p0.add(b5);
 94         p0.add(b6);
 95         p0.add(b1);
 96         p0.add(b2);
 97         p0.add(b3);
 98         p0.add(b_cancel);
 99         p0.add(b0);
100         p0.add(b_ok);
101         p0.setBounds(10,45, 150, 150);
102         
103         p1.setLayout(gl1);
104         p1.add(b_turn_on);
105         p1.add(b_turn_off);
106         p1.add(b_random);
107         p1.setBounds(170, 45, 60, 150);
108         
109         p2.setLayout(gl2);
110         p2.add(b_channel_up);
111         p2.add(b_channel_down);
112         p2.add(b_volume_up);
113         p2.add(b_volume_down);
114         p2.setBounds(250, 45, 60, 150);
115         
116         setLayout(null);
117         add(p0);
118         add(p1);
119         add(p2);
120         setResizable(false);
121         
122         addWindowListener(new WindowAdapter(){
123             public void windowClosing(WindowEvent e1)
124             {
125                 System.exit(0);
126             }
127         });
128         setBackground(Color.lightGray);
129         setBounds(100,350,320,250);
130         setVisible(true);
131         
132         }
133     class Bt implements ActionListener
134     {
135         /* (non-Javadoc)
136          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
137          */
138         public void actionPerformed(ActionEvent e2) 
139         {
140             try{
141                 if(tvbox.turn)
142                 {
143                 if(e2.getSource() == b_turn_on)
144                 {
145                     tvbox.turn = true;
146                     data.creatTxtFile();
147                     data.creatTxtFile_channel();
148                     data.read_channel_test();
149                     data.read_test();
150                     tvbox.tf_channel.setText(Integer.toString(tvbox.channel_now));
151                     tvbox.tf_volume.setText(Integer.toString(tvbox.volume[tvbox.channel_now]));
152                     
153                 }
154                 else if(e2.getSource() == b_turn_off)
155                 {
156                     tvbox.turn = false;
157                     tvbox.tf_channel.setText(" ");
158                     tvbox.tf.setText(" ");
159                     tvbox.tf_volume.setText(" ");
160                     }
161                 
162                 else if(e2.getSource() == b_channel_up)
163                 {
164                     tvbox.channel_now = Integer.parseInt(tvbox.tf_channel.getText());
165                     
166                     if(tvbox.channel_now == 29)
167                     {
168                         tvbox.channel_now = 0;
169                     }
170                     else 
171                     {
172                         tvbox.channel_now += 1;
173                     }
174                     tvbox.tf_channel.setText(Integer.toString(tvbox.channel_now));
175                     tvbox.tf_volume.setText(Integer.toString(tvbox.volume[tvbox.channel_now]));
176                     tvbox.tf.setText("--");
177                     str.setLength(0);
178                     data.write_channel();
179                 }
180                 else if(e2.getSource() == b_channel_down)
181                 {
182                     tvbox.channel_now = Integer.parseInt(tvbox.tf_channel.getText());
183                     
184                     if(tvbox.channel_now == 0)
185                     {
186                         tvbox.channel_now = 29;
187                     }
188                     else 
189                     {
190                         tvbox.channel_now -= 1;
191                     }
192                     tvbox.tf_channel.setText(Integer.toString(tvbox.channel_now));
193                     tvbox.tf_volume.setText(Integer.toString(tvbox.volume[tvbox.channel_now]));
194                     tvbox.tf.setText("--");
195                     str.setLength(0);
196                     data.write_channel();
197                     
198                 }
199                 else if(e2.getSource() == b_volume_up)
200                 {
201                     if(tvbox.volume[tvbox.channel_now]<100)
202                     {
203                         tvbox.volume[tvbox.channel_now] += 1;
204                     }
205                     tvbox.tf_volume.setText(Integer.toString(tvbox.volume[tvbox.channel_now]));
206                     data.write();
207                     tvbox.tf.setText("--");
208                     str.setLength(0);
209                 }
210                 else if(e2.getSource() == b_volume_down)
211                 {
212                     if(tvbox.volume[tvbox.channel_now]>0)
213                     {
214                         tvbox.volume[tvbox.channel_now] -= 1;
215                     }
216                     tvbox.tf_volume.setText(Integer.toString(tvbox.volume[tvbox.channel_now]));
217                     data.write();
218                     tvbox.tf.setText("--");
219                     str.setLength(0);
220                 }
221                 
222                 else if(e2.getSource() == b_random)
223                 {
224                     tvbox.channel_now = (int)(Math.random()*30);
225                     tvbox.tf_channel.setText(Integer.toString(tvbox.channel_now));
226                     tvbox.tf_volume.setText(Integer.toString(tvbox.volume[tvbox.channel_now]));
227                     tvbox.tf.setText("--");
228                     str.setLength(0);
229                     data.write_channel();
230                     
231                 }
232                 else if(e2.getSource() == b_cancel)
233                 {
234                     tvbox.tf.setText("--");
235                     str.setLength(0);
236                 }
237                 else if(e2.getSource() == b_ok)
238                 {
239                     tvbox.tf.setText("--");
240                     str.setLength(0);
241                     if(0 <= tvbox.channel && tvbox.channel <= 29)
242                     {
243                         tvbox.channel_now = tvbox.channel;
244                         tvbox.tf_channel.setText(Integer.toString(tvbox.channel_now));
245                     }
246                     else 
247                     {
248                         tvbox.tf.setText("error");
249                     }
250                     tvbox.tf_volume.setText(Integer.toString(tvbox.volume[tvbox.channel_now]));
251                     data.write_channel();
252                 }
253                 
254                 else
255                 {
256                     tvbox.tf.setText(str.append(e2.getActionCommand()).toString());
257                     tvbox.channel = Integer.parseInt(tvbox.tf.getText().trim());
258                 }
259                 }
260                 else
261                 {
262                     
263                     
264                     if(e2.getSource() == b_turn_on)
265                     {
266                         tvbox.turn = true;
267                         data.creatTxtFile();
268                         data.creatTxtFile_channel();
269                         data.read_channel_test();
270                         data.read_test();
271                         tvbox.tf_channel.setText(Integer.toString(tvbox.channel_now));
272                         tvbox.tf_volume.setText(Integer.toString(tvbox.volume[tvbox.channel_now]));
273                         
274                     }
275                     }
276                 
277             }
278             
279             
280             catch (NumberFormatException e)
281         {
282                 tvbox.tf_channel.setText("数字格式异常");
283             }
284             catch(StringIndexOutOfBoundsException e){ 
285                 tvbox.tf_channel.setText("字符串索引越界"); 
286                 } catch (Exception e) {
287                 // TODO Auto-generated catch block
288                 e.printStackTrace();
289             } 
290             
291         }
292         
293     }
294         
295     
296         
297         
298         
299     }
300     

 

tvbox.java

和remote类似,只是一些反馈的JTextField 用来显示当前的音量和频道情况。

 

  1 import java.awt.*; 
  2 import java.awt.event.*; 
  3 import javax.swing.*; 
  4 public class tvbox extends Frame {
  5 
  6     /**
  7      * @param args
  8      */
  9     private static final long serialVersionUID = 11;
 10     GridLayout gl0, gl1,gl2;
 11     Panel p0,p1,p2;
 12     public static JTextField tf_channel, tf_volume; //频道和音频的显示区域,字符串返回
 13     public static JTextField tf,tf_text_channel,tf_text_volume,tf_text_buffer; 
 14     Button b_turn_on,b_turn_off,b_channel_up,b_channel_down,b_volume_up,b_volume_down, b_random;
 15     StringBuffer str;
 16     static int channel,volume_now,channel_now;
 17     static boolean turn;
 18     public static int[] volume;
 19 
 20     public tvbox()
 21     {
 22         gl0=new GridLayout(3,2,10,2);
 23         gl1=new GridLayout(1,3,10,0);
 24         gl2=new GridLayout(1,4,7,0);
 25         
 26         volume = new int[30];
 27         
 28         tf_text_channel = new JTextField(7);
 29         tf_text_channel.setEditable(false);
 30         tf_text_channel.setText("频道:");
 31         tf_text_buffer = new JTextField(7);
 32         tf_text_buffer.setEditable(false);
 33         tf_text_buffer.setText("缓冲:");
 34         tf_text_volume = new JTextField(7);
 35         tf_text_volume.setEditable(false);
 36         tf_text_volume.setText("音量:");
 37         tf_channel = new JTextField(15);
 38         tf_channel.setHorizontalAlignment(JTextField.RIGHT);
 39         tf_channel.setEditable(false);
 40         tf_channel.setText("");
 41         tf_volume = new JTextField(15);
 42         tf_volume.setHorizontalAlignment(JTextField.RIGHT);
 43         tf_volume.setEditable(false);
 44         tf_volume.setText("");
 45         tf = new JTextField(3);
 46         tf.setHorizontalAlignment(JTextField.RIGHT);
 47         tf.setText("0");
 48         tf.setEnabled(false);
 49         
 50         b_turn_on = new Button("Turn On");
 51         b_turn_on.setForeground(Color.blue);
 52         b_turn_on.addActionListener(new Bt());    
 53         b_turn_off= new Button("Turn Off");
 54         b_turn_off.setForeground(Color.blue);
 55         b_turn_off.addActionListener(new Bt());
 56         b_volume_up = new Button("Volume +");
 57         b_volume_up.setForeground(Color.blue);
 58         b_volume_up.addActionListener(new Bt());
 59         b_volume_down= new Button("Volume -");
 60         b_volume_down.setForeground(Color.blue);
 61         b_volume_down.addActionListener(new Bt());
 62         b_channel_up= new Button("Channel +");
 63         b_channel_up.setForeground(Color.blue);
 64         b_channel_up.addActionListener(new Bt());
 65         b_channel_down= new Button("Channel -");
 66         b_channel_down.setForeground(Color.blue);
 67         b_channel_down.addActionListener(new Bt());
 68         b_random= new Button("搜索");
 69         b_random.setForeground(Color.blue);
 70         b_random.addActionListener(new Bt());
 71         
 72         p0 = new Panel();
 73         p1 = new Panel();
 74         p2 = new Panel();
 75         
 76         str = new StringBuffer();
 77         
 78         p0.setLayout(gl0);
 79         p0.add(tf_text_channel);
 80         p0.add(tf_channel);
 81         p0.add(tf_text_volume);
 82         p0.add(tf_volume);
 83         p0.add(tf_text_buffer);
 84         p0.add(tf);
 85         p0.setBounds(10, 25, 300, 90);
 86         
 87         p1.setLayout(gl1);
 88         p1.add(b_turn_on);
 89         p1.add(b_turn_off);
 90         p1.add(b_random);
 91         p1.setBounds(10, 125, 300, 45);
 92         
 93         p2.setLayout(gl2);
 94         p2.add(b_channel_up);
 95         p2.add(b_channel_down);
 96         p2.add(b_volume_up);
 97         p2.add(b_volume_down);
 98         p2.setBounds(10, 180, 300, 45);
 99         
100         setLayout(null);
101         add(p0);
102         add(p1);
103         add(p2);
104         setResizable(false);
105         
106         addWindowListener(new WindowAdapter(){
107             public void windowClosing(WindowEvent e1)
108             {
109                 System.exit(0);
110             }
111         });
112         setBackground(Color.lightGray);
113         setBounds(100,100,320,240);////////////////////////////////
114         setVisible(true);
115         
116         }
117     
118     class Bt implements ActionListener
119     {
120         /* (non-Javadoc)
121          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
122          */
123         public void actionPerformed(ActionEvent e2) 
124         {
125             try{
126                 if(turn)
127                 {
128                 if(e2.getSource() == b_turn_on)
129                 {
130                     turn = true;
131                     data.creatTxtFile();
132                     data.creatTxtFile_channel();
133                     data.read_channel_test();
134                     data.read_test();
135                     tf_channel.setText(Integer.toString(channel_now));
136                     tf_volume.setText(Integer.toString(volume[channel_now]));
137                     
138                 }
139                 else if(e2.getSource() == b_turn_off)
140                 {
141                     turn = false;
142                     tf_channel.setText(" ");
143                     tf.setText(" ");
144                     tf_volume.setText(" ");
145                     }
146                 
147                 else if(e2.getSource() == b_channel_up)
148                 {
149                     channel_now = Integer.parseInt(tf_channel.getText());
150                     
151                     if(channel_now == 29)
152                     {
153                         channel_now = 0;
154                     }
155                     else 
156                     {
157                         channel_now += 1;
158                     }
159                     tf_channel.setText(Integer.toString(channel_now));
160                     tf_volume.setText(Integer.toString(volume[channel_now]));
161                     tf.setText("--");
162                     str.setLength(0);
163                     data.write_channel();
164                 }
165                 else if(e2.getSource() == b_channel_down)
166                 {
167                     channel_now = Integer.parseInt(tf_channel.getText());
168                     
169                     if(channel_now == 0)
170                     {
171                         channel_now = 29;
172                     }
173                     else 
174                     {
175                         channel_now -= 1;
176                     }
177                     tf_channel.setText(Integer.toString(channel_now));
178                     tf_volume.setText(Integer.toString(volume[channel_now]));
179                     tf.setText("--");
180                     str.setLength(0);
181                     data.write_channel();
182                     
183                 }
184                 else if(e2.getSource() == b_volume_up)
185                 {
186                     if(volume[channel_now]<100)
187                     {
188                         volume[channel_now] += 1;
189                     }
190                     tf_volume.setText(Integer.toString(volume[channel_now]));
191                     data.write();
192                     tf.setText("--");
193                     str.setLength(0);
194                 }
195                 else if(e2.getSource() == b_volume_down)
196                 {
197                     if(volume[channel_now]>0)
198                     {
199                         volume[channel_now] -= 1;
200                     }
201                     tf_volume.setText(Integer.toString(volume[channel_now]));
202                     data.write();
203                     tf.setText("--");
204                     str.setLength(0);
205                 }
206                 
207                 else if(e2.getSource() == b_random)
208                 {
209                     channel_now = (int)(Math.random()*30);
210                     tf_channel.setText(Integer.toString(channel_now));
211                     tf_volume.setText(Integer.toString(volume[channel_now]));
212                     tf.setText("--");
213                     str.setLength(0);
214                     data.write_channel();
215                     
216                 }
217                 
218                 
219                 
220                 else
221                 {
222                     tf.setText(str.append(e2.getActionCommand()).toString());
223                     channel = Integer.parseInt(tf.getText().trim());
224                 }
225                 }
226                 else
227                 {
228                     
229                     
230                     if(e2.getSource() == b_turn_on)
231                     {
232                         turn = true;
233                         data.creatTxtFile();
234                         data.creatTxtFile_channel();
235                         data.read_channel_test();
236                         data.read_test();
237                         tf_channel.setText(Integer.toString(channel_now));
238                         tf_volume.setText(Integer.toString(volume[channel_now]));
239                         
240                     }
241                     }
242                 
243             }
244             
245             
246             catch (NumberFormatException e)
247         {
248                 tf_channel.setText("数字格式异常");
249             }
250             catch(StringIndexOutOfBoundsException e){ 
251                 tf_channel.setText("字符串索引越界"); 
252                 } catch (Exception e) {
253                 // TODO Auto-generated catch block
254                 e.printStackTrace();
255             } 
256             
257         }
258         
259     }
260     
261     
262         
263         
264     }
265     

 

运行结果如上

posted @ 2012-11-05 11:49  方包  阅读(3592)  评论(0编辑  收藏  举报