JavaWing图形界面编程技术手册

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

图形界面编程技术手册编写:Kende_zhu第一节基本容器JFrame1、第一个JFrame窗体程序publicclassJFrameDemo{publicstaticvoidmain(String[]args){JFramejf=newJFrame(这是我的第一个swing窗体);Dimensiond=newDimension(300,200);jf.setSize(d);Pointp=newPoint(500,400);jf.setLocation(p);jf.setVisible(true);}}第二节标签组件JLabel1、定义标签对象publicclassJLabelDemo{publicstaticvoidmain(String[]args){JFramejf=newJFrame(这是我的第一个swing窗体);JLabellab=newJLabel(KENDE,JLabel.CENTER);//实例化标签对象jf.add(lab);//将Label组件添加到JFrame面板中Dimensiond=newDimension(300,200);jf.setSize(d);Pointp=newPoint(500,400);jf.setLocation(p);jf.setVisible(true);}}2、定义标签字体标签可以设置字体,包括什么字体,字体的颜色,大小,是否斜体等publicclassJLabelDemo2{publicstaticvoidmain(String[]args){JFramejf=newJFrame(这是我的第一个swing窗体);JLabellab=newJLabel(KENDE,JLabel.CENTER);//实例化标签对象Fontfont=newFont(Dialog,Font.ITALIC+Font.BOLD,30);lab.setFont(font);jf.add(lab);//将组件添加到面板中Dimensiond=newDimension(300,200);jf.setSize(d);Pointp=newPoint(500,400);jf.setLocation(p);jf.setVisible(true);}}3、得到本机中所有的字体publicclassGetAllFonts{publicstaticvoidmain(String[]args){GraphicsEnvironmentg=GraphicsEnvironment.getLocalGraphicsEnvironment();Stringfonts[]=g.getAvailableFontFamilyNames();for(Stringname:fonts){System.out.println(name);}}}4、在JLabel中设置显示的图片publicclassJLabelDemo3{publicstaticvoidmain(String[]args){JFramejf=newJFrame(这是我的第一个swing窗体);Iconicon=newImageIcon(E:\\picBackGroud\\野生动物王国.jpg);JLabellab=newJLabel(KENDE,icon,JLabel.CENTER);//实例化标签对象Fontfont=newFont(Serif,Font.ITALIC+Font.BOLD,30);lab.setFont(font);jf.add(lab);//将组件添加到面板中Dimensiond=newDimension(300,200);jf.setSize(d);Pointp=newPoint(500,400);jf.setLocation(p);jf.setVisible(true);}}第三节按钮组件JButton1、在JFrame中增加按钮publicclassJButtonDemo01{publicstaticvoidmain(String[]args){JFrameframe=newJFrame(WelcometoKende'shome!);JButtonbt=newJButton(按钮);frame.add(bt);frame.setSize(300,200);frame.setLocation(400,300);frame.setVisible(true);}}2、往JButton中添加图片publicclassJButtonDemo02{publicstaticvoidmain(String[]args){JFrameframe=newJFrame(WelcometoKende'shome!);StringpicPath=E:\\picBackGroud\\a.jpg;Iconicon=newImageIcon(picPath);JButtonbt=newJButton(icon);frame.add(bt);frame.setSize(800,500);frame.setLocation(400,300);frame.setVisible(true);}}第三节菜单栏组件publicclassIsMenuItemextendsJFrameimplementsActionListener{privatestaticfinallongserialVersionUID=1L;JMenuItemmiBlue=newJMenuItem(Blue);JMenuItemmiGreen=newJMenuItem(Green);JMenuItemmiRed=newJMenuItem(Red);JTextFieldtfMain=newJTextField(20);publicIsMenuItem(){this.setSize(600,400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JMenuBarmenuBar=newJMenuBar();JMenumenuColor=newJMenu(Color);menuBar.add(menuColor);menuColor.add(miBlue);menuColor.add(miGreen);menuColor.add(miRed);this.getContentPane().add(menuBar,BorderLayout.NORTH);JPanelpanel=newJPanel();this.getContentPane().add(panel,BorderLayout.CENTER);panel.add(tfMain);tfMain.setBorder(BorderFactory.createLoweredBevelBorder());miBlue.addActionListener(this);miGreen.addActionListener(this);miRed.addActionListener(this);this.setVisible(true);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==miBlue){tfMain.setBackground(Color.BLUE);}elseif(e.getSource()==miGreen){tfMain.setBackground(Color.GREEN);}elseif(e.getSource()==miRed){tfMain.setBackground(Color.RED);}}publicstaticvoidmain(String[]args){newIsMenuItem();}}第四节布局管理器1、FlowLayout布局管理器FlowLayout属于流式布局管理器,使用此种布局方式,所有的组件会像流水一样依次排列。publicclassFlowLayoutDemo01{publicstaticvoidmain(String[]args){JFrameframe=newJFrame(Welcometokende'shome);frame.setLayout(newFlowLayout(FlowLayout.CENTER,20,20));JButtonbutton=null;for(inti=0;i9;i++){button=newJButton(按钮-+i);frame.add(button);}frame.setSize(800,500);frame.setLocation(400,300);frame.setVisible(true);}}效果图:2、BorderLayout布局管理器BorderLayout将一个窗体的版面划成东,西,南,北,中五个区域,可以直接将需要的组件放到五个区域即可publicclassBorderLayoutDemo01{publicstaticvoidmain(String[]args){JFrameframe=newJFrame(Welcometokende'shome);frame.setLayout(newBorderLayout(20,20));frame.add(newJButton(东),BorderLayout.EAST);frame.add(newJButton(西),BorderLayout.WEST);frame.add(newJButton(南),BorderLayout.SOUTH);frame.add(newJButton(北),BorderLayout.NORTH);frame.add(newJButton(中),BorderLayout.CENTER);frame.setSize(500,300);frame.setLocation(400,300);frame.setVisible(true);}}3、GridLayout布局管理器GridLayout布局管理器是以表格的形式进行管理的,在使用此布局管理器的时候必须设置显示的行数和列数。publicclassGridLayoutDemo01{publicstaticvoidmain(String[]args){JFrameframe=newJFrame(Welcometokende'shome);frame.setLayout(newGridLayout(3,5,3,3));JButtonbutton=null;for(inti=0;i13;i++){button=newJButton(按钮-+i);frame.add(button);}frame.setSize(500,300);frame.setLocation(400,300);frame.setVisible(true);}}效果图:4、CardLayout布局管理器CardLayout就是将一组组件彼此重叠的进行布局,就像一张张卡片一样,这样每次只会展现一个界面publicclassCardLayoutDemo01{publicstaticvoidmain(String[]args){JFrameframe=newJFrame(Welcometokende'shome);CardLayoutcard=newCardLayout();frame.setLayout(card);Containercon=frame.getContentPane();con.add(newJLabel(标签-A,JLabel.CENTER),first);con.add(newJLabel(标签-B,JLabel.CENTER),second);con.add(newJLabel(标签-C,JLabel.CENTER),third);con.add(newJLabel(标签-D,JLabel.CENTER),fourth);con.add(newJ

1 / 25
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功