java小程序之JFrame类

图形用户界面(GUI)组件是OOP很好的例子,开发程序创建图形用户界面,会用到JFrame、JButton、JRadioButton、JComboBox和JList 等java类来创建框架、单选按钮、组合框、列表等。
下面是一个JFrame类的代码示例:

//GUI JFrame
import javax.swing.JFrame;
public class Demo032701{
    public static void main( String [] args ){
        JFrame frame1 = new JFrame();
        frame1.setTitle( "Windows1" );
        frame1.setSize( 800, 450 );
        frame1.setLocation( 200,100 );
        frame1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame1.setVisible( true );
    }
}

添加了按钮、标签、文本域、复选框和组合框的GUI,示例代码如下:

//GUI 综合的情况
import javax.swing.*;
public class Demo032702{
    public static void main( String [] args ){
        JButton jbtOk = new JButton( "Ok" );
        JButton jbtCancel = new JButton( "Cancel" );
        JLabel jlbName = new JLabel( "enter your name:" );
        JTextField jtfName = new JTextField( "Type your name here" );
        JCheckBox jchkBold = new JCheckBox( "Bold" );
        JCheckBox jchkItalic = new JCheckBox( "Italic" );
        JRadioButton jrbRed = new JRadioButton( "Red" );
        JRadioButton jrbYellow = new JRadioButton( "Yellow" );
        JComboBox jcbColor = new JComboBox( new String[] { "Freshman", "Sophomore", "Junior", "Senior" } );

        JPanel panel = new JPanel();
        panel.add( jbtOk );
        panel.add( jbtCancel );
        panel.add( jlbName );
        panel.add( jtfName );
        panel.add( jchkBold );
        panel.add( jchkItalic );
        panel.add( jrbRed );
        panel.add( jrbYellow );
        panel.add( jcbColor );

        JFrame frame = new JFrame();
        frame.add( panel );
        frame.setTitle( "show GUI components" );
        frame.setSize( 850,450 );
        frame.setLocation( 200,100 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );
    }
}

你可能感兴趣的:(java)