JFrame编程
最基础的窗口
public test1 ( )
{ setTitle ( "test1" ) ; setSize ( 300 , 300 ) ; setLocation ( 300 , 300 ) ; jp = new JPanel ( ) ; JButton jb= new JButton ( "button" ) ; add ( jb) ; setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ; setVisible ( true ) ;
}
边界布局管理器
jb1= new JButton ( "jb1" ) ;
add ( jb1, BorderLayout. EAST) ;
jb2= new JButton ( "jb2" ) ;
add ( jb2, BorderLayout. NORTH) ;
jb3= new JButton ( "jb3" ) ;
add ( jb3, BorderLayout. SOUTH) ;
jb4= new JButton ( "jb4" ) ;
add ( jb4, BorderLayout. WEST) ;
jb5= new JButton ( "jb5" ) ;
add ( jb5, BorderLayout. CENTER) ;
流式布局管理器
setLayout ( new FlowLayout ( ) ) ;
for ( int i= 0 ; i< 5 ; i++ )
{ jb[ i] = new JButton ( "" + i+ "" ) ; add ( jb[ i] ) ;
}
this . setResizable ( false ) ;
网格式布局管理器
this . setLayout ( new GridLayout ( 3 , 3 , 15 , 15 ) ) ; for ( int i= 0 ; i< 5 ; i++ ) { jb[ i] = new JButton ( "" + i+ "" ) ; add ( jb[ i] ) ; }
面板
jp = new JPanel ( ) ; jp2= new JPanel ( ) ; for ( int i= 0 ; i< 2 ; i++ ) { jb[ i] = new JButton ( "" + i+ "" ) ; jp. add ( jb[ i] ) ; } for ( int i= 2 ; i< 5 ; i++ ) { jb[ i] = new JButton ( "" + i+ "" ) ; jp2. add ( jb[ i] ) ; } this . add ( jp, BorderLayout. NORTH) ; this . add ( jp2, BorderLayout. SOUTH) ;
标签、文本框、密码框
JLabel jl= new JLabel ( "账号" ) ; JTextField jtf= new JTextField ( 10 ) ; JLabel jl2= new JLabel ( "密码" ) ; JPasswordField jtf2= new JPasswordField ( 10 ) ;
复选框和单选按钮
jp = new JPanel ( ) ; jp2= new JPanel ( ) ; JCheckBox[ ] jcb= { null, null, null} ; for ( int i= 0 ; i< 3 ; i++ ) { jcb[ i] = new JCheckBox ( "" + i+ "" ) ; jp. add ( jcb[ i] ) ; } JRadioButton[ ] jrb= { null, null} ; ButtonGroup bg= new ButtonGroup ( ) ; for ( int i= 0 ; i< 2 ; i++ ) { jrb[ i] = new JRadioButton ( "" + i+ "" ) ; } bg. add ( jrb[ 0 ] ) ; bg. add ( jrb[ 1 ] ) ; jp2. add ( jrb[ 0 ] ) ; jp2. add ( jrb[ 1 ] ) ; add ( jp) ; add ( jp2) ;
下拉列表框和滚动条
this . setLayout ( new GridLayout ( 2 , 1 ) ) ; jp = new JPanel ( ) ; jp2= new JPanel ( ) ; String[ ] S= { "北京" , "上海" , "武汉" , "杭州" } ; JComboBox jcmb= new JComboBox ( S) ; for ( int i= 0 ; i< 4 ; i++ ) jcmb. addItem ( "" + i+ "" ) ; JList jlist= new JList ( S) ; jlist. setVisibleRowCount ( 3 ) ; JScrollPane jsp= new JScrollPane ( jlist) ; jp. add ( jcmb) ; add ( jp) ; jp2. add ( jsp) ; add ( jp2) ;
拆分窗口和插入图片
JLabel jl= new JLabel ( new ImageIcon ( "Image/龙猫2.jpg" ) ) ; String[ ] S= { "1" , "2" , "3" } ; JList jlist= new JList ( S) ; JSplitPane jsp= new JSplitPane ( JSplitPane. HORIZONTAL_SPLIT, jlist, jl) ; jsp. setOneTouchExpandable ( true ) ; add ( jsp) ;
设置图片
this . setIconImage ( ( new ImageIcon ( "Image/龙猫2.jpg" ) ) . getImage ( ) ) ;
一些相关设置的操作
JLabel jl= new JLabel ( "qq" , JLabel. CENTER) ; jl. setFont ( new Font ( "宋体" , Font. PLAIN, 16 ) ) ; jl. setForeground ( Color. BLUE) ; jl. setCursor ( Cursor. getPredefinedCursor ( Cursor. HAND_CURSOR) ) ;
选项卡
jp. setBackground ( Color. BLUE) ;
JTabbedPane jtbp= new JTabbedPane ( ) ;
jtbp. add ( "面板1" , jp) ; jtbp. add ( "面板2" , jp2) ;
菜单和工具条
JMenuBar mb= new JMenuBar ( ) ; JMenu jm= new JMenu ( "中国" ) ; JMenuItem jmi1= new JMenuItem ( "汉阳" ) ; JMenuItem jmi2= new JMenuItem ( "汉口" ) ; JMenuItem jmi= new JMenuItem ( "北京" ) ; JMenu ej= new JMenu ( "武汉" ) ; ej. add ( jmi1) ; ej. add ( jmi2) ; jm. add ( jmi) ; jm. addSeparator ( ) ; jm. add ( ej) ; JMenu jm2= new JMenu ( "美国(A)" ) ; jm2. setMnemonic ( 'A' ) ; mb. add ( jm) ; mb. add ( jm2) ; JTextField jtf= new JTextField ( ) ; JToolBar tool= new JToolBar ( ) ; JButton button= new JButton ( "New" ) ; button. setToolTipText ( "新建" ) ; tool. add ( button) ; jp. add ( mb) ; jp. add ( tool) ;
表格
Vector ziduan= new Vector ( ) ; ziduan. add ( "编号" ) ; ziduan. add ( "姓名" ) ; Vector jilu= new Vector ( ) ; for ( int i= 0 ; i< 3 ; i++ ) { Vector row= new Vector ( ) ; row. add ( "" + i+ "" ) ; row. add ( "a" + i+ "" ) ; jilu. add ( row) ; } JTable ta= new JTable ( jilu, ziduan) ; JScrollPane jsp= new JScrollPane ( ta) ; this . add ( jsp) ;