代码改变世界

随笔分类 -  Java

JFrame中的paint()方法

2012-10-04 03:33 by youxin, 3822 阅读, 收藏, 编辑
摘要: 看下面的代码,public class super_paint extends JFrame{ private JButton jt; public super_paint() { jt=new JButton("Hello"); jt.setSize(20,10); Container container=getContentPane(); this.add(jt); } @Override public void paint(Graphics g) { ... 阅读全文

java getName() getClass()用法

2012-10-03 20:34 by youxin, 1488 阅读, 收藏, 编辑
摘要: In java there is a way that makes us enabled to get the object class name at runtime. It can be done by calling thegetClass()method on the class object. Then by calling the methodgetName()we can get the name of the object class.In our example java program we have created a classRoseIndiaand we have 阅读全文

java ArrayList和Iterator

2012-10-03 14:41 by youxin, 1052 阅读, 收藏, 编辑
摘要: 创建ArrayL对象ArrayList a=new ArrayList();加入元素a.add(E e);在指定的位置修改元素a.set(1,"world");把位置1的元素设为world插入元素a.add(int index .E e);在位置index 插入元素remove(intindex) 移除指定位置元素import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class ArrayListTest { public static void main(Stri 阅读全文

java.util.Timer

2012-10-02 23:08 by youxin, 446 阅读, 收藏, 编辑
摘要: java.util.Timer定时器,实际上是个线程,定时调度所拥有的TimerTasks。一个TimerTask实际上就是一个拥有run方法的类,需要定时执行的代码放到run方法体内,TimerTask一般是以匿名类的方式创建。java.util.Timer timer = new java.util.Timer(true); // true 说明这个timer以daemon方式运行(优先级低, // 程序结束timer也自动结束),注意,javax.swing // 包中也有一个Timer类,如果import中用到swing包, // 要注意名字的冲突。 TimerTask task = 阅读全文

java swing timer

2012-10-02 16:39 by youxin, 400 阅读, 收藏, 编辑
摘要: A Swing timer (an instance ofjavax.swing.Timer) fires one or more action events after a specified delay. Don't confuse Swing timers with the general-purpose timer facility that was added to thejava.utilpackage in release 1.3. This page describes only Swing timers.In general, we recommend using S 阅读全文

java 多线程之 Executors

2012-10-02 15:17 by youxin, 318 阅读, 收藏, 编辑
摘要: Executorshttp://docs.oracle.com/javase/tutorial/essential/concurrency/executors.htmlIn all of the previous examples, there's a close connection between the task being done by a new thread, as defined by itsRunnableobject, and the thread itself, as defined by aThreadobject. This works well for sm 阅读全文

java 多线程 之 生产者和消费者

2012-10-02 14:52 by youxin, 277 阅读, 收藏, 编辑
摘要: Guarded block:可以翻译为临界区。Threads often have to coordinate their actions. The most common coordination idiom is theguarded block. Such a block begins by polling a condition(轮询一个条件) that must be true before the block can proceed. There are a number of steps to follow in order to do this correctly.Suppos 阅读全文

java JDBC配置和使用

2012-09-30 17:02 by youxin, 9938 阅读, 收藏, 编辑
摘要: 去mysql网站http://dev.mysql.com/downloads/connector/ 下载 JDBC driver http://dev.mysql.com/doc/refman/5.1/en/connector-j-installing.html You can install th 阅读全文

big java 线程 竞争条件

2012-09-30 15:55 by youxin, 888 阅读, 收藏, 编辑
摘要: 当多个线程共享对通用对象的访问时,他们就能彼此冲突,为说明所引发的问题,本节将探讨一个实例,在程序中,多个线程处理一个银行账户,每个线程重复的存或取许多钱,然后休眠一小段时间。下面是DepositRunable类的run方法publicvoidrun(){try{for(inti = 1; i <=count; i++) {account.deposit(amount);Thread.sleep(DELAY);}}catch(InterruptedException exception) {//TODO: handle exception}}withdrawRunnable 类与depo 阅读全文

java Nested Classes

2012-09-28 08:59 by youxin, 271 阅读, 收藏, 编辑
摘要: The Java programming language allows you to define a class within another class. Such a class is called anested classand is illustrated here:class OuterClass { ... class NestedClass { ... }}Terminology:Nested classes are divided into two categories: static and non-static. Nested clas... 阅读全文

转:总结java的interface和abstract class

2012-09-28 08:48 by youxin, 478 阅读, 收藏, 编辑
摘要: 先说说interface和abstract method语法中需要注意的地方。Interface:1. An interface can contain fields, but these are implicitly static and final.2. You can choose to explicitly declare the methods in an interface as public, but they are public even if you don’t say it.3. Interface cannot define static methodAbstract: 阅读全文

Swing线程之SwingUtilities.invokeLater 解释

2012-09-28 00:23 by youxin, 2490 阅读, 收藏, 编辑
摘要: 在官方的文章里:http://docs.oracle.com/javase/tutorial/uiswing/painting/step1.html告诉我们要创建一个gui。All Graphical User Interfaces require some kind of main application frame in which to display. In Swing, this is an instance ofjavax.swing.JFrame. Therefore, our first step is to instantiate this class and make su 阅读全文

Java Socket一对一聊天

2012-07-21 16:28 by youxin, 870 阅读, 收藏, 编辑
摘要: 下面的是服务器端代码:package 一对一聊天; import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;/** * 模拟qq聊天功能: 实现客户端与服务器(一对一)的聊天功能,客户端首先发起聊天,输入的内容在服务器端和客户端显示, * 然后服务器端也可以输入信息,同样信息也在客户端和服务器端显示 */// 服务器类p 阅读全文

Java JList 使用

2012-07-21 00:55 by youxin, 2667 阅读, 收藏, 编辑
摘要: 先看ListModel。ListModel是一个interface,主要的功能是定义一些方法,让JList或JComboBox这些组件取得每个项目的值,并可限定项目的显示 时机与方式,下面为ListModel这个interface所定义的方法:ListModel interface定义的方法: void addListDataListener(ListDataListener l):当data model的长度或内容值有任何改变时,利用此 方法就可以处理ListDataListener的事件。da... 阅读全文

JScrollPane用法以及注意点

2012-07-20 23:05 by youxin, 3202 阅读, 收藏, 编辑
摘要: JScrollPane滚动条组件,注意你想添加一个组件到里面,下面的方法不行:JTextArea jta=new JTextArea();JScrollPane jsp=new JScrollPane();jsp.add(jta);jta是不会显示的。这个很特殊,有2中方法向JScrollPane添加组件:1.在构造方法中把要添加的组件传进去。JTextArea jta=new JTextArea();JScrollPane jsp=new JScrollPane(jta);2.使用scrollPane.getViewport().add(label) 形式:public class Not. 阅读全文

转: java的InputStream和OutputStream的理解

2012-07-20 21:25 by youxin, 365 阅读, 收藏, 编辑
摘要: 1、在java中stream代表一种数据流(源),java io的底层数据元,---(想像成水龙头)2、任何有能力产生数据流(源)的javaio对象就可以看作是一个InputStream对象。既然它能产生出数据,我们就可以将数据取出,java对封装的通用方法就read()方法了--(出水龙头)3、任何有能力接收数据源(流)的javaio对象我们就可以看作是一个OutputStream对象同样,它能接收数据,我们就可以调用它的write方法,来让它接收数据--(进水龙头了,呵呵)4、当然,我们可以在Inputstream和OutputStream数据源的基础上,从实际需要触发,来重新封装出不同性 阅读全文

Java DataInputStream 与 DataOutputStream

2012-07-20 21:18 by youxin, 1379 阅读, 收藏, 编辑
摘要: DataOutputStreamA data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。下面向一个文件写入数据:public class DataInput { ... 阅读全文

java Byte 和byte 差别及byte[ ]和string转换

2012-07-20 20:45 by youxin, 7175 阅读, 收藏, 编辑
摘要: 先看Byte,是一个类:public final class ByteTheByteclass wraps a value of primitive typebytein an object. An object of typeBytecontains a single field whose type isbyte.In addition, this class provides several methods for converting abyteto aStringand aStringto abyte, as well as other constants and methods . 阅读全文

java 3中方法复制一个文件

2012-07-20 20:31 by youxin, 428 阅读, 收藏, 编辑
摘要: 主要用的了InputStream类,BufferedInputStream类等。import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;/** * 测试类 * @author hanlw * 2012 -07 - 04 */public class Test_two { public static void main(String[] a... 阅读全文

java .io OutputStream 与InputStream

2012-07-20 20:10 by youxin, 949 阅读, 收藏, 编辑
摘要: outputStream首先声明这是一个抽象类,所以关于输出的类都继承与这个类。三个基本的写方法abstractvoidwrite(intb):往输出流中写入指定的字节。voidwrite(byte[]b):往输出流中写入数组b中的所有字节。voidwrite(byte[]b,intoff, intlen):往输出流中写入数组b中从偏移量off开始的len个字节的数据记住write的操作都是对byte 数据操作。Output streams exist to allow data to be written to some data consumer; what sort ofconsumer 阅读全文
点击右上角即可分享
微信分享提示