java源代码实例

java dos输入输出
Java code
   
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> import java.util. * ; public class DaoXu { /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 System.out.println( " 请输入单个字符并回车: " ); Scanner c = new Scanner(System.in); String[] ch = new String[ 5 ]; for ( int i = 0 ; i < 5 ; i ++ ){ ch[i] = c.next(); } // Arrays.sort(ch); System.out.print( " 倒序输出: " ); for ( int j = ch.length - 1 ; j >= 0 ; j -- ) { System.out.print(ch[j] + " " ); } } }
java获取ip地址
Java code
   
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.InetAddress; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class ip extends JFrame implements ActionListener { private static final long serialVersionUID = 3339481369781127417L ; JButton jb1; JButton jb2; JButton jb3; JPanel jp; JLabel jl; JLabel jl1; JTextField jt; public ip() { this .jp = new JPanel(); this .jl = new JLabel(); this .jl1 = new JLabel( " 您的域名: " ); this .jb1 = new JButton( " 提交 " ); this .jb2 = new JButton( " 重置 " ); this .jb3 = new JButton( " 退出 " ); this .jt = new JTextField( 20 ); this .jb1.addActionListener( this ); this .jb2.addActionListener( this ); this .jb3.addActionListener( this ); this .jp.setLayout( new GridLayout( 3 , 2 )); this .jp.add( this .jl1); this .jp.add( this .jt); this .jp.add( this .jb1); this .jp.add( this .jl); this .jp.add( this .jb2); this .jp.add( this .jb3); setBounds( 200 , 200 , 500 , 240 ); add( this .jp); setVisible( true ); setDefaultCloseOperation( 3 ); } public static void main(String[] args) { new ip(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == this .jb1) { String url = this .jt.getText(); InetAddress ip = null ; try { ip = InetAddress.getByName(url); } catch (UnknownHostException e1) { e1.printStackTrace(); } this .jl.setText(ip.toString()); } else if (e.getSource() == this .jb2) { this .jl.setText( "" ); this .jt.setText( "" ); } else { System.exit( 0 ); } } }
java系统托盘的应用
Java code
   
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> package com.msg; import java.applet.Applet; import java.applet.AudioClip; import java.awt.AWTException; import java.awt.Image; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.TextArea; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel; /** * * 创建闪动的托盘图像 * @author Everest * */ public class BickerTray extends JFrame implements Runnable { private static final long serialVersionUID = - 3115128552716619277L ; private SystemTray sysTray; // 当前操作系统的托盘对象 private TrayIcon trayIcon; // 当前对象的托盘 private ImageIcon icon = null ; private TextArea ta = null ; private static int count = 1 ; // 记录消息闪动的次数 private boolean flag = false ; // 是否有新消息 private static int times = 1 ; // 接收消息次数 public BickerTray() { this .createTrayIcon(); // 创建托盘对象 Image image = this .getToolkit().getImage(getRes( " com/img/f32.gif " )); this .setIconImage(image); init(); } public URL getRes(String str){ return this .getClass().getClassLoader().getResource(str); } /** * 初始化窗体的方法 */ public void init() { this .setTitle( " 消息盒子 " ); ta = new TextArea( "" ); ta.setEditable( false ); this .add(ta); this .setSize( 400 , 400 ); // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this .setLocationRelativeTo( null ); // 添加窗口最小化事件,将托盘添加到操作系统的托盘 /* this.addWindowListener(new WindowAdapter() { public void windowIconified(WindowEvent e) { addTrayIcon(); } }); */ addTrayIcon(); this .setVisible( true ); } /** * 添加托盘的方法 */ public void addTrayIcon() { try { sysTray.add(trayIcon); // 将托盘添加到操作系统的托盘 setVisible( false ); // 使得当前的窗口隐藏 new Thread( this ).start(); } catch (AWTException e1) { e1.printStackTrace(); } } /** * 创建系统托盘的对象 步骤: * 1,获得当前操作系统的托盘对象 * 2,创建弹出菜单popupMenu * 3,创建托盘图标icon * 4,创建系统的托盘对象trayIcon */ public void createTrayIcon() { sysTray = SystemTray.getSystemTray(); // 获得当前操作系统的托盘对象 icon = new ImageIcon(getRes( " com/img/f17.gif " )); // 托盘图标 PopupMenu popupMenu = new PopupMenu(); // 弹出菜单 MenuItem mi = new MenuItem( " 打开 " ); MenuItem exit = new MenuItem( " 退出 " ); popupMenu.add(mi); popupMenu.add(exit); // 为弹出菜单项添加事件 mi.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ta.setText(ta.getText() + " \n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第 " + times + " 次接收时间: " + new Date().toLocaleString()); // 设置通知消息内容 BickerTray. this .setExtendedState(JFrame.NORMAL); BickerTray. this .setVisible( true ); // 显示窗口 BickerTray. this .toFront(); // 显示窗口到最前端 flag = false ; // 消息打开了 count = 0 ; times ++ ; } }); exit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit( 0 ); } }); trayIcon = new TrayIcon(icon.getImage(), " 消息盒子 " , popupMenu); /** 添加鼠标监听器,当鼠标在托盘图标上双击时,默认显示窗口 */ trayIcon.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2 ) { // 鼠标双击 ta.setText(ta.getText() + " \n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第 " + times + " 次接收时间: " + new Date().toLocaleString()); // 设置通知消息内容 BickerTray. this .setExtendedState(JFrame.NORMAL); BickerTray. this .setVisible( true ); // 显示窗口 BickerTray. this .toFront(); flag = false ; // 消息打开了 count = 0 ; times ++ ; } } }); } /** * 线程控制闪动 */ public void run() { while ( true ) { if (flag){ // 有新消息 try { if (count == 1 ){ // 播放消息提示音 // AudioPlayer p = new AudioPlayer(getRes("file:com/sound/Msg.wav")); // p.play(); p.stop(); try { AudioClip p = Applet.newAudioClip( new URL( " file:sound/msg.wav " )); p.play(); } catch (MalformedURLException e) { e.printStackTrace(); } } // 闪动消息的空白时间 this .trayIcon.setImage( new ImageIcon( "" ).getImage()); Thread.sleep( 500 ); // 闪动消息的提示图片 this .trayIcon.setImage(icon.getImage()); Thread.sleep( 500 ); } catch (Exception e) { e.printStackTrace(); } count ++ ; } else { // 无消息或是消息已经打开过 this .trayIcon.setImage(icon.getImage()); try { Thread.sleep( 20000 ); flag = true ; } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * @param args */ public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated( true ); try { UIManager.setLookAndFeel( new SubstanceBusinessBlueSteelLookAndFeel()); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } SwingUtilities.invokeLater( new Runnable() { public void run() { new BickerTray(); } }); } }
<!--End_rbody_72511717//-->

你可能感兴趣的:(java,thread,.net,swing,dos)