图形用户界面例子

上传人:小** 文档编号:46438982 上传时间:2021-12-13 格式:DOC 页数:12 大小:349KB
返回 下载 相关 举报
图形用户界面例子_第1页
第1页 / 共12页
图形用户界面例子_第2页
第2页 / 共12页
图形用户界面例子_第3页
第3页 / 共12页
点击查看更多>>
资源描述
例1创建一个宽200像素,高200像素,可见,标题为hello的窗口。代码如下:import javax.swi ng.*;class my1public static void main(String args)JFrame f=new JFrame(hello);f.setSize(200,200); f.setVisible(true);程序运行结果如图所示:例1的运行结果例2 :import javax.swi ng.*;class my2public static void main(String args)JFrame f=new JFrame(hello);f.setSize(300,300);f.setVisible(true);JDialog d=new JDialog(f,mydialog,true);d.setSize(200,200);d.setVisible(true);程序运行结果如图所示:Q aydialuE例2执行结果例3:import javax.swi ng.JButt on;import javax.swi ng.JFrame;import javax.swi ng.JPa nel;public class comp1 public static void main(String args) JButton b1 = new JButto n( test1);JButton b2 = new JButto n(test2);JFrame jf = new JFrame(test);JPa nel p = new JPa nel();p.add(b1);p.add(b2);jf.add(p);jf.pack();jf.setVisible(true);程序运行结果如图所示:例3运行结果例4:import javax.swi ng.*;import java.awt.*;class my1public static void main(String args)JFrame frame=new JFrame(hello);JPa nel n amePa nel=new JPa nel();JTextField nameField仁new JTextField(”请输入用户名,用户名不超过25个字符。”);JTextField nameField2=new JTextField(请输入昵称,昵称不超过25 个字符。,25);JTextField nameField3=new JTextField(20);JTextField nameField4=new JTextField(20);JButton bl = new JButton(确定);JButton b2 = new JButton(取消);Container conten t=frame.getC onten tPa ne();n amePa nel.add( nameField1);n amePa nel.add( nameField2);n amePa nel.add( nameField3);n amePa nel.add( nameField4);n amePa nel.add(b1);n amePa nel.add(b2);conten t.add( namePa nel);frame.setSize(520,200);frame.setVisible(true);程序运行结果如图所示:例4运行结果例5:import javax.swi ng.*;public class taExample exte nds JFrame String s=Java这个名字n+来自于一个有趣的故事。n+有一天,几个Java成员 组的成员n+正在一边喝咖啡n+ 一边讨论着给 Oak语言起个新名字,n+当时他们正喝 着Java咖啡n+忽然有个成员说就叫Java怎么样? n+这个提议n+得到了其他人的一致同意。n+从那时起n+Java就借着In ternet的东风,飘香于世了。;JTextArea ta仁new JTextArea(4,10);JScrollPane sp = new JScrollPane(ta1);taExample()ta1.setText(s);this.add(sp);this.setSize(200,200);this.setVisible(true);public static void main(String args) taExample tae1= new taExample();程序运行结果如图所示:山苗这个若字来自于一平有謹的故事有一天几个J a阳咸员鉅的成员 正在一边唱咖啡一边讨论着给0目為言起亍新名令 当时他们正唱蒼Jav別冊卡 I 惣蛭百个成员说就孰日怎么祥叫 这亍提富1 ij的白遼个名字来自于一r有趣的故爭.有一天几日成员爼的成员正在一边唱咖時1边忖论着给0自腐育起甲新名字I 当吋他们正嶋着J刖抽10呻 翘然有T成员说就叫J刑日億么祥?得到了莒他人的一叙同意.从月附起日阳就惜着I rite met的东凤,訓香于世了.例5运行结果例6:import javax.swi ng.*;import java.awt.*;class labelDemo exte nds JFrameJLabel starLabel=new JLabel( new ImageIc on (e:javastar.gif);JLabel sun Label=new JLabel( new ImageIc on (e:javas un. gif);JLabel textLabel仁new JLabel(星星 ”,JLabel.CENTER);JLabel textLabel2=new JLabel(太阳 ”,JLabel.CENTER);Container conten t=this.getC onten tPa ne();public labelDemo()conten t.setLayout (new GridLayout(2,2);conten t.add(textLabell);conten t.add(starLabel);conten t.add(textLabel2);conten t.add(s un Label);public static void main(String args)labelDemo demo1= new labelDemo(); demo1.setSize(400,200);demol.setVisible(true);程序运行结果如图所示:例6运行结果例7:import java.awt.GridLayout;import java.awt.eve nt.*;import javax.swi ng.*;public class choice exte nds JFrame impleme nts ItemListe ner String num = 08001, 08112, 02005, 06004 , name = java, 数据库,软件 工程,C 语言, credit = 4, 4, 3,5 ;JTextField tf1 = new JTextField(10), tf2 = new JTextField(4);JLabel choiceLabel = new JLabel(请选择课程号:),nameLabel = new JLabel(课程名:), creLabel = new JLabel(学分:);JComboBox jc = new JComboBox();choice() this.setLayout (new GridLayout(3,2,10,10);for (int i = 0; i num.len gth; i+)jc.addltem( nu mi);jc.addItemListe ner(this);this.add(choiceLabel);this.add(jc);this.add( nameLabel);this.add(tf1);this.add(creLabel);this.add(tf2);this.setSize(300, 200);public void itemStateCha nged(ltemEve nt e) int x = 0, y = 0;Stri ng s = (Stri ng) e.getltem();for (; y num.len gth; y+)if (s = nu my)x = jc.getSelectedl ndex();tfl.setText (n amex);tf2.setText(credit x);public static void main( Stri ng args) choice c = new choice。;c.setVisible(true);c.setDefaultCloseOperatio n(JFrame.EXIT_ON_CLOSE); 程序运行结果如图所示:例7运行结果例8:import java.awt.FlowLayout;import java.util.Vector;import javax.swi ng.*;public class jlExample exte nds JFrame JComboBox jc=new JComboBox();JList jl;JLabel jl1= new JLabel(选择汉堡种类:”),jl2=new JLabel(选择饮料种类:); jlExample()Vector v=new Vector();v.add(鸡肉汉堡);v.add(火腿汉堡);v.add(芝士汉堡);jl=new JList(v);jc.addltem(” 可乐);jc.addltem(” 橙汁);jc.addltem(” 牛奶);jc.addltem(” 咖啡);this.setLayout (new FlowLayout();this.add(jl1);JScrollPa ne scrollPa ne = new JScrollPa ne(jl);this.add(scrollPa ne);this.add(jl2);this.add(jc);this.setSize(250, 300);this.setVisible(true);public static void main( Stri ng args) new jlExample();程序运行结果如图所示:例8运行结果例9,下例中模仿记事本的菜单进行了编程。代码如下:import java.awt.*;import java.awt.eve nt.*;import javax.swi ng.*;public class jmExample exte nds JFrame jmExample()Container con=this.getC onten tPa ne();JPa nel jp=new JPa nel();con. add(jp);JMe nuBar menubar=new JMe nuBar(); this.setJMe nuBar(me nubar);JMenu file=new JMenu(文件);JMenultem fjm1 =new JMenultem(” 新建);JMenultem fjm2 =new JMenultem(” 打开);JMenultem fjm3 =new JMenultem(” 保存);JMenultem fjm4 =new JMenultem(” 另存为); file.add(fjm1);file.add(fjm2);file.add(fjm3);file.add(fjm4);file.addSeparator();JMenultem fjm5 =new JMenultem(” 页面设置);JMenuItem fjm6 =new JMenuItem( 打印 );file.add(fjm5);file.add(fjm6);file.addSeparator();JMenuItem fjm7 =new JMenuItem( 退出 );file.add(fjm7);JMenu edit=new JMenu( 编辑 );JMenuItem ejm1 =new JMenuItem( 撤销 );edit.add(ejm1);edit.addSeparator();JMenuItem ejm2 =new JMenuItem( 剪切 );JMenuItem ejm3 =new JMenuItem( 复制 );JMenuItem ejm4 =new JMenuItem( 粘贴 );JMenuItem ejm5 =new JMenuItem( 删除 );edit.add(ejm2);edit.add(ejm3);edit.add(ejm4);edit.add(ejm5);edit.addSeparator();JMenuItem ejm6 =new JMenuItem( 查找 );JMenuItem ejm7 =new JMenuItem( 替换 );edit.add(ejm6);edit.add(ejm7);menubar.add(file);menubar.add(edit);this.setSize(300, 300);this.setVisible(true);public static void main(String args) jmExample jme=new jmExample();程序运行结果如图所示:例9运行结果例10,在窗口中添加一个打开文件按钮和一个标签,当单击打开文件按钮时,弹出一个文件打开对话框,用户选择文件后,将该文件的完整路径及文件名在标签中显示。代码如下:import java.awt.*;import java.awt.eve nt.*;import java.io.File;import javax.swi ng.*;import javax.sw in g.filechooser.FileFilter;public class fcExample exte nds JFrame impleme nts Actio nListe ner private JLabel jl;private JFileChooser jfc1;fcExample()jl=new JLabel();jfc1= new JFileChooser(); myFileFilter myf1 =new myFileFilter(); jfc1.addChoosableFileFilter(myf1); JButton jb=new JButton(打开文件); jb.addActi on Liste ner(this);this.getCo nte ntPa ne().add(jl,BorderLayout.CENTER); this.getCo nten tPa ne().add(jb,BorderLayout.SOUTH);public static void main(String args) fcExample jfc=new fcExample();jfc.setDefaultCloseOperatio n( EXIT_ON_CLOSE);jfc.setSize(300, 300);jfc.setVisible(true);public void action Performed(Act ion Eve nt e) int result=jfc1.showOpe nDialog(this);if (result = jfc1.APPROVE_OPTION) Stri ng s = jfc1.getSelectedFile().getAbsolutePath();jl.setText(s);class myFileFilter exte nds FileFilterpublic boolea n accept(File f) return (f.isDirectory()|f.getName().e ndsWith(.java);public String getDescription() return (Java file (*.java);程序运行结果如图所示:口爲* 判脚:曲口曰|匸Z: 口泄加K廿n呵jErris |E;HJT 恥市例10运行结果例11:import java.awt.*;import javax.swi ng.*;public class flowExample public static void main(String args) JFrame f= new JFrame(” 流布局管理器”);JPa nel cp=new JPa nel();JButton b1= new JButton(first);JButton b2=new JButton(second);JButton b3=new JButton(third);JButton b4=new JButto n( fourth);JButton b5=new JButton(fifth);JButton b6=new JButton(sixth);cp.setLayout (new FlowLayout(FlowLayout.LEFT);f.setC onten tPa ne(cp);cp.add(b1);cp.add(b2);cp.add(b3);cp.add(b4);cp.add(b5);cp.add(b6);f.setDefaultCloseOperati on (JFrame.EXIT_ON_CLOSE);f.setSize(600, 100);f.setVisible(true);程序运行结果如图所示:例11运行结果当缩小窗口大小时,各个按钮的位置都会相应的发生变化。如图所示:当窗口缩小时,各组件的变化情况例12:import java.awt.*;import javax.swi ng.*;public class bordExample exte nds JFrame private JButton b;Stri ng name=North,South,East,West,Ce nter; private BorderLayout bl;public bordExample(Stri ng s)super(s);bl=new BorderLayout(10, 10);Container con=this.getC onten tPa ne();con. setLayout(bl);b=new JButt onn ame .len gth;for(int i=0;iname.length;i+)bi=new JButton(namei);con .add(b0,BorderLayout.NORTH);con .add(b1,BorderLayout.SOUTH);con. add(b2,BorderLayout.EAST);con .add(b3,BorderLayout.WEST);con .add(b4,BorderLayout.CENTER);this.setSize(300, 200);this.setVisible(true);public static void main( Stri ng args) bordExample be=new bordExample(” 边界布局管理器 ); be.setDefaultCloseOperatio n( JFrame.EXIT_ON_CLOSE);程序运行结果如图所示:边界布层管理器匚n xNormWestCetrterEastSouth例12运行结果
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!