java小程序之JFrame类

淡淡的烟草味﹌ 2022-07-28 01:00 187阅读 0赞

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

  1. //GUI JFrame
  2. import javax.swing.JFrame;
  3. public class Demo032701{
  4. public static void main( String [] args ){
  5. JFrame frame1 = new JFrame();
  6. frame1.setTitle( "Windows1" );
  7. frame1.setSize( 800, 450 );
  8. frame1.setLocation( 200,100 );
  9. frame1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  10. frame1.setVisible( true );
  11. }
  12. }

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

  1. //GUI 综合的情况
  2. import javax.swing.*;
  3. public class Demo032702{
  4. public static void main( String [] args ){
  5. JButton jbtOk = new JButton( "Ok" );
  6. JButton jbtCancel = new JButton( "Cancel" );
  7. JLabel jlbName = new JLabel( "enter your name:" );
  8. JTextField jtfName = new JTextField( "Type your name here" );
  9. JCheckBox jchkBold = new JCheckBox( "Bold" );
  10. JCheckBox jchkItalic = new JCheckBox( "Italic" );
  11. JRadioButton jrbRed = new JRadioButton( "Red" );
  12. JRadioButton jrbYellow = new JRadioButton( "Yellow" );
  13. JComboBox jcbColor = new JComboBox( new String[] { "Freshman", "Sophomore", "Junior", "Senior" } );
  14. JPanel panel = new JPanel();
  15. panel.add( jbtOk );
  16. panel.add( jbtCancel );
  17. panel.add( jlbName );
  18. panel.add( jtfName );
  19. panel.add( jchkBold );
  20. panel.add( jchkItalic );
  21. panel.add( jrbRed );
  22. panel.add( jrbYellow );
  23. panel.add( jcbColor );
  24. JFrame frame = new JFrame();
  25. frame.add( panel );
  26. frame.setTitle( "show GUI components" );
  27. frame.setSize( 850,450 );
  28. frame.setLocation( 200,100 );
  29. frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  30. frame.setVisible( true );
  31. }
  32. }

发表评论

表情:
评论列表 (有 0 条评论,187人围观)

还没有评论,来说两句吧...

相关阅读