LED顯示類比(java的GUI顯示)

來源:互聯網
上載者:User

標籤:enum   java   gui   color   timer   

//先上,每一秒改變一次顏色


//這個小程式產生的jar包:http://pan.baidu.com/s/1sjFj9ST

下面上原始碼:

//1.color.java

package com.huowolf;import java.awt.Color; enum color{red(Color.RED), orange(Color.ORANGE),yellow(Color.YELLOW),green(Color.GREEN),gray(Color.GRAY),blue(Color.BLUE),pink(Color.pink),black(Color.BLACK);private Color c;private color(Color c){this.c = c;}public Color getMyColor() {return c;}}

//2.LEDFrame.java

package com.huowolf;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;public class LEDFrame extends JFrame {private static final long serialVersionUID = 1L;public  JTextField tf;public  JButton bt;public  JPanel p;public String num;LEDCanvas canvas = new LEDCanvas(this);public LEDFrame() {setTitle("LED顯示類比");setLocation(100,150);setSize(900,400);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout());add(canvas,BorderLayout.CENTER);setVisible(true); }private  void init() {p = new JPanel();add(p,BorderLayout.NORTH);JLabel l = new JLabel("請輸入你要產生的數字串:");l.setHorizontalAlignment(JLabel.LEFT);p.add(l);tf = new JTextField(10);p.add(tf);bt = new JButton("確定");p.add(bt);pack();}public  String getTF() {bt.addActionListener(new ActionListener() {//final String s;@Overridepublic void actionPerformed(ActionEvent e) { num= tf.getText(); //tf.setText(""); canvas.repaint();}});return num;}public static void main(String[] args) { new LEDFrame().init();}}

//3.LEDCanvas.java

package com.huowolf;import java.awt.Canvas;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Random;import javax.swing.Timer;public class LEDCanvas extends Canvas {private static final long serialVersionUID = 7922500886L;int [][]data=null;LEDFrame LEDf = null;public Timer timer;public Color c;public LEDCanvas(LEDFrame f){this.LEDf = f;ActionListener listener = new TimerListener();timer  = new Timer(1000,listener);timer.start();}class TimerListener implements  ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {c = ColorValue();repaint(1000);}}public void paint(Graphics g){Font f=new Font("宋體",Font.BOLD,25); g.setFont(f);g.setColor(c);String str = LEDf.getTF();int pos=20;for(int k=0;str!=null && k<str.length();k++){char ch = str.charAt(k);print(ch);int y =10;for(int i=0;i<data.length;i++){int x =pos;x+=20;for(int j=0;j<data[i].length;j++){//System.out.print(data[i][j]);if(data[i][j]== 1){g.fillOval(x,y, 12, 16);}x+=15;}//System.out.println();y += 25;}pos+=100;}}public Color ColorValue(){Random random = new Random();int r = random.nextInt(color.values().length);color c = null;for(color c1 : color.values()) {if(c1.ordinal() == r){//System.out.println(c1.getMyColor());c = c1;break;}}return  c.getMyColor();}public void print (char c){if('0'==c) {data = new int[][] {{1,1,1,1,1,},      {1,0,0,0,1,}, {1,0,0,0,1,}, {1,0,0,0,1,}, {1,1,1,1,1,}   };}if('1'==c){data = new int[][] {{0,0,0,1,0,},      {0,0,0,1,0,}, {0,0,0,1,0,}, {0,0,0,1,0,}, {0,0,0,1,0,}};}if('2'==c){data = new int[][] {{1,1,1,1,1,},      {0,0,0,0,1,}, {1,1,1,1,1,}, {1,0,0,0,0,}, {1,1,1,1,1,}};}if('3'==c){data = new int[][] {{1,1,1,1,1,},  {0,0,0,0,1,},{1,1,1,1,1,},{0,0,0,0,1,},{1,1,1,1,1,}};}if('4'==c){data = new int[][] {{1,0,0,0,1},    {1,0,0,0,1},    {1,1,1,1,1,},    {0,0,0,0,1,},    {0,0,0,0,1,}};}if('5'==c){data = new int[][] {{1,1,1,1,1,},{1,0,0,0,0,},{1,1,1,1,1,},{0,0,0,0,1,},{1,1,1,1,1,}};}if('6'==c){data = new int[][] {{1,1,1,1,1,},{1,0,0,0,0,},{1,1,1,1,1,},{1,0,0,0,1,},{1,1,1,1,1,}};}if('7'==c){data = new int[][] {{1,1,1,1,1,},{0,0,0,0,1,},{0,0,0,0,1,},{0,0,0,0,1,},{0,0,0,0,1,}};}if('8'==c){data = new int[][] {{1,1,1,1,1,},{1,0,0,0,1,},{1,1,1,1,1,},{1,0,0,0,1,},{1,1,1,1,1,}};}if('9'==c){data = new int[][] {{1,1,1,1,1,},{1,0,0,0,1,},{1,1,1,1,1,},{0,0,0,0,1,},{1,1,1,1,1,}};}}}


LED顯示類比(java的GUI顯示)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.