博文
SwingSpeciality用法(2005-09-22 17:22:00)
摘要:/*
此程序演示Swing的特性
JComponent类的特殊功能又分为:
1) 边框设置:使用setBorder()方法可以设置组件外围的边框,使用一个EmptyBorder对象能在组件周围留出空白。
2) 双缓冲区:使用双缓冲技术能改进频繁变化的组件的显示效果。与AWT组件不同,JComponent组件默认双缓冲区,不必自己重写代码。如果想关闭双缓冲区,可以在组件上施加setDoubleBuffered(false)方法。
3) 提示信息:使用setTooltipText()方法,为组件设置对用户有帮助的提示信息。
4) 键盘导航:使用registerKeyboardAction( ) 方法,能使用户用键盘代替鼠标来驱动组件。JComponent类的子类AbstractButton还提供了便利的方法--用setMnemonic( )方法指明一个字符,通过这个字符和一个当前L&F的特殊修饰共同激活按钮动作。
5) 可插入L&F:每个Jcomponent对象有一个相应的ComponentUI对象,为它完成所有的绘画、事件处理、决定尺寸大小等工作。 ComponentUI对象依赖当前使用的L&F,用UIManager.setLookAndFeel( )方法可以设置需要的L&F.
6) 支持布局:通过设置组件最大、最小、推荐尺寸的方法和设置X、Y对齐参数值的方法能指定布局管理器的约束条件,为布局提供支持。
另外还有菜单的使用方法
*/
import java.awt.*;
import java.io.File;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
public class SwingSpecialityDemo
{
public static void main(String args[])
{
try{UIManager.setLo......
PrintStream用法(2005-09-22 17:21:00)
摘要:import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)throws IOException
{
try
{
FileInputStream fis = new FileInputStream (FileDescriptor.in);//指定输入流为键盘
PrintStream ps = new PrintStream (new FileOutputStream ("D://testfile//file.log"), true);
int is;
while ((is=fis.read()) != -1)ps.println((char)is);
fis.close();
ps.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
......
OutputStreamWriter用法(2005-09-22 17:21:00)
摘要:example2:OutputStreamWriterDemo.java
/************************************************************************
以FileOutputStreamDemo.java为摸板,按回车后再按Ctrl+z结束输入,可输入中文
************************************************************************/
import java.io.*;
public class OutputStreamWriterDemo
{
public static void main(String[] args)throws IOException
{
try
{
InputStreamReader isr = new InputStreamReader (new FileInputStream (FileDescriptor.in));
OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream ("D://testfile//output.log"));
int is;
while ((is=isr.read()) != -1)osw.write(is);
isr.close();
osw.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
ca......
Layout用法(2005-09-22 17:20:00)
摘要:import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.Box;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import java.awt.event.*;
public class LayoutDemo
{
public static void main(String[] args)
{
JFrame frame = new JFrame ("Layout Manager Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
tp.addTab ("Flow",new FlowPanel());
tp.addTab ("Border",new BorderPanel());
tp.addTab ("Grid",new GridPanel());
tp.addTab ("Box",new BoxPanel());
frame.se......
JTree用法(2005-09-22 17:20:00)
摘要:import java.awt.Dimension;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.BoxLayout;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/*
JTree的构造函数:
JTree()
JTree(Hashtable value)
JTree(Object[] value)//只有这个构造函数可以创建多个根结点
JTree(TreeModel newModel)
JTree(TreeNode root)
JTree(TreeNode root, boolean asksAllowsChildren)
JTree(Vector value)
*/
public class JTreeDemo
{
public static void main (String[] args)
{
//构造函数:JTree()
JTree example1 = new JTree();
//构造函数:JTree(Object[] value)
Object[] letters= {"a", "b", "c", "d", "e"};
JTree example2 = new JTree (letters);
//构造函数:JTree(TreeNode root)(TreeNode空)
&nb......
JTextArea用法(2005-09-22 17:19:00)
摘要:import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
public class JTextAreaDemo
{
public static void main(String[] args)
{
try{javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
JTextArea ta = new JTextArea(20,20);
ta.setBorder (BorderFactory.createEmptyBorder (1,1,1,5));
ta.setColumns (0); ta.setRows (0);//相当于设置文本区组件的初始大小,并不是说一行只能写0个字符;
ta.setLineWrap (false);//设置为禁止自动换行,初始值为false.
ta.setTabSize (4);//设置制表符的大小为8个字符,初始值为4个字符
ta.setWrapStyleWord (true);
ta.setBackground (Color.white);//文本区背景
ta.setForeground (Color.red);//字体颜色
ta.setF......
JTable用法(2005-09-22 17:19:00)
摘要:import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.table.TableColumn;
public class JTableDemo
{
public static void main (String[] args)
{
/*
构造函数有很多下面先介绍几个:
JTable()
JTable(int numRows, int numColumns)
JTable(Object[][] rowData, Object[] columnNames)
*/
JTable example1 = new JTable ();//看不到但存在
JTable example2 = new JTable (8, 6);
final Object[] columnNames = {"姓名", "性别", "家庭地址",//列名最好用final修饰
"电话号码", "生日", "工作", "收入", "婚姻状况","恋爱状况"};
Object[][] rowData = {
{"ddd", "男", "江苏南京", "1378313210", "03/24/1985", "学生", "寄生中", "未婚", "没......
JTabbedPane用法(2005-09-22 17:18:00)
摘要:import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
class TabPane extends JTabbedPane
{
JPanel panel1, panel2, panel3, panel4, panel5;
public TabPane ()
{
//super (a, b);
panel1 = new JPanel ();
panel1.add(new JButton());
panel2 = new JPanel (new BorderLayout());
panel3 = new JPanel (true);
panel4 = new JPanel (new GridLayout(2,3));
panel5 = new JPanel (new FlowLayout(FlowLayout.LEFT, 1, 0));
//以上是JPanel的四种构造方法
//窗体布局器有四种设置:
//new BorderLayout () or new BorderLayout (int hgap, int vg......
JTabbeld用法(2005-09-18 17:23:00)
摘要:import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
class TabPane extends JTabbedPane
{
JPanel panel1, panel2, panel3, panel4, panel5;
public TabPane ()
{
//super (a, b);
panel1 = new JPanel ();
panel1.add(new JButton());
panel2 = new JPanel (new BorderLayout());
panel3 = new JPanel (true);
panel4 = new JPanel (new GridLayout(2,3));
panel5 = new JPanel (new FlowLayout(FlowLayout.LEFT, 1, 0));
//以上是JPanel的四种构造方法
//窗体布局器有四种设置:
//new BorderLayout () or new BorderLayout (int hgap, int vg......
JSplitPane用法(2005-09-18 17:23:00)
摘要:import javax.swing.*;
import java.awt.Dimension;
public class JSplitPaneDemo
{
public static void main (String[] args)
{
JButton button = new JButton ("button");
JLabel label = new JLabel ("label");
JSplitPane splitPane = new JSplitPane ();
splitPane.setOneTouchExpandable (true);//是该分隔面板的分隔条显示出箭头
splitPane.setContinuousLayout (true);
splitPane.setPreferredSize (new Dimension (500,600));//设置大小
splitPane.setOrientation (JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent (button);//same as: splitPane.setTopComponent(button);
splitPane.setRightComponent (label);//same as: splitPane.setBottomComponent(label);
splitPane.setDividerSize (5);//设置分隔条的粗细
splitPane.setDividerLocation(200);//设置分隔条的位置,基于setPreferredSize方法中的值,
&nb......