界面设计课件例题.doc_第1页
界面设计课件例题.doc_第2页
界面设计课件例题.doc_第3页
界面设计课件例题.doc_第4页
界面设计课件例题.doc_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【例】 显示一个空框架 import javax.swing.*;public class FirstFrame public static void main(String args) SimpleFrame frame = new SimpleFrame(); /设置用户关闭框架时的响应动作 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /显示该框架 /frame.show(); 已过时 frame.setVisible(true); class SimpleFrame extends JFramepublic SimpleFrame() /设置框架大小 setSize(WIDTH, HEIGHT);public static final int WIDTH = 300;public static final int HEIGHT = 200; 【例】 显示一个带标题框架 import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ch9_1 extends JFramepublic ch9_1()/标题super(我的第一个JFrame窗口);/设置大小和位置setBounds(20,20,500,300);/点击关闭按钮可以关闭窗口addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););/*/第二种方法 点击关闭按钮可以关闭窗口setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/public static void main(String args)ch9_1 f=new ch9_1();f.show();练习1建立一个有标题可以关闭的JFrame(ch9_1.java) import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ch9_1 extends JFrame public ch9_1() /标题 super(我的第一个JFrame窗口); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); /* /第二种方法 点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); */ public static void main(String args) ch9_1 f=new ch9_1(); f.show(); 练习2建立一个带有左上角图标的窗口(ch9_2.java) import javax.swing.*; import javax.swing.ImageIcon.*; public class ch9_2 extends JFrame public ch9_2() /标题 super(测试图标); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /加入图标 ImageIcon icon=new ImageIcon(icon.jpg); setIconImage(icon.getImage(); public static void main(String args) ch9_2 f=new ch9_2(); f.show(); 练习3建立一个没有最大化按钮的窗口(ch9_3.java) import javax.swing.*; public class ch9_3 extends JFrame public ch9_3() /标题 super(测试图标); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /去掉最大化按钮 setResizable(false); public static void main(String args) ch9_3 f=new ch9_3(); f.show(); 练习4去掉窗口的标题栏(ch9_4.java) import javax.swing.*; public class ch9_4 extends JFrame public ch9_4() /标题 super(测试图标); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /设置外观 setUndecorated(true);/这句可以禁用最小化、最大化、关闭按钮,并且令窗体连边界也没有 public static void main(String args) ch9_4 f=new ch9_4(); f.show(); 【例】 创建一个swing外观的并居中的窗体 import javax.swing.*;import java.awt.*;public class mainFrame extends JFrame/* *方法说明:构造器,通过传递参数来完成窗体的绘制。 *输入参数:String sTitle 窗体标题 *输入参数:int iWidth 窗体的宽度 *输入参数:int iHeight 窗体的高度 *返回类型: */ public mainFrame(String sTitle,int iWidth,int iHeight) Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();/获取屏幕尺寸 ImageIcon ii = new ImageIcon(images/middle.gif); setTitle(sTitle);/设置窗体标题 setIconImage(ii.getImage();/设置窗体的图标 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);/设置但关闭窗体时退出程序 setSize(iWidth,iHeight);/设置窗体大小 int w = getSize().width;/获取窗体宽度 int h = getSize().height;/获取窗体高度 System.out.println(窗体宽:+w+ 窗体高:+h); int x = (dim.width-w)/2; int y = (dim.height-h)/2; setLocation(x,y);/将窗体移到屏幕中间 setVisible(true);/显示窗体 public static void main(String args) JFrame.setDefaultLookAndFeelDecorated(true);/使用最新的SWING外观 mainFrame mF = new mainFrame(main Frame Demo,400,300); 一个Java GUI简单程序 【例】一个简单的Swing GUI应用程序。在一个框架窗口中显示两个标签和一个按钮:上面的标签显示一串固定的文字信息,选择按钮后在下面的标签上显示系统现在的时间。l import java.awt.*;l import java.awt.event.*;l import javax.swing.*;l import java.util.*;l / 继承JFrame类并实现ActionListener接口l public class SwingDemo extends JFrame implementsl ActionListenerl JButton b1; / 声明按钮对象l JLabel l1,l2; / 声明标签对象l SwingDemo() / 定义构造方法l super(Swing应用程序的例);/ 调用父类的构造方法l l1=new JLabel(一个GUI应用程序的例子,l JLabel.CENTER); / 定义标签,文字居中l l2=new JLabel( ); / 定义无文字标签l b1=new JButton(现在时间T);/ 定义按钮l b1.setMnemonic(KeyEvent.VK_T);/ 设置按钮的快捷键l b1.setActionCommand(time); / 设置控制名l b1.addActionListener(this); / 注册按钮事件l getContentPane().add(l1,BorderLayout.NORTH); / 添加标签l1l getContentPane().add(l2,BorderLayout.CENTER); / 添加标签l2l getContentPane().add(b1,BorderLayout.SOUTH); / 添加标签b1l l / 对按钮引发事件编程l public void actionPerformed(ActionEvent e)l / 捕获按钮事件l Calendar c1 = Calendar.getInstance();l / 获取系统日期和时间l if(e.getActionCommand().equals(time)l / 判断是否为所需的按钮事件l l2.setText(“现在时间是”+l c1.get(Calendar.HOUR_OF_DAY)l +“时”+c1.get(Calendar.MINUTE)+“分”);l /设置标签文字l l2.setHorizontalAlignment(JLabel.CENTER);l / 设置标签标签文字居中对齐l l else System.exit(0);l l public static void main(String args)/ 主方法l JFrame.setDefaultLookAndFeelDecorated(true);l / 加此语句显示为运行结果图的右图 l JFrame frame = new SwingDemo();l / 创建JFrame对象,初始不可见l frame.setDefaultCloseOperation(l JFrame.EXIT_ON_CLOSE);/ 设置框架关闭按钮事件l frame.pack(); / 压缩框架的显示区域l frame.setVisible(true); / 显示框架主窗口l l FlowLayout()布局模式:ch9_50.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ch9_50 extends JFrame public ch9_50() /标题 super(布局模式); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public static void main(String args) ch9_50 f=new ch9_50(); /为设置FlowLayout布局模式 f.getContentPane().setLayout(new FlowLayout(); /建立一个按钮 JButton jb1=new JButton(确定); /建立一个按钮 JButton jb2=new JButton(取消); /把按钮加入到窗体上 f.getContentPane().add(jb1); f.getContentPane().add(jb2); f.setVisible(true); BorderLayout布局模式:ch9_51 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ch9_51 extends JFrame public ch9_51() /标题 super(布局模式); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public static void main(String args) ch9_51 f=new ch9_51(); Container contentPane=f.getContentPane(); /设置BorderLayout布局模式 /contentPane.setLayout(new BorderLayout(); contentPane.setLayout(new BorderLayout(10,20); contentPane.add(new JButton(EAST),BorderLayout.EAST); contentPane.add(new JButton(WEST),BorderLayout.WEST); contentPane.add(new JButton(SOUTH),BorderLayout.SOUTH); contentPane.add(new JButton(NORTH),BorderLayout.NORTH); contentPane.add(new JButton(CENTER),BorderLayout.CENTER); /contentPane.add(new JLabel(CENTER,JLabel.CENTER),BorderLayout.CENTER); f.setVisible(true); GridLayout布局模式:ch9_52 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ch9_52 extends JFrame public ch9_52() /标题 super(布局模式); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public static void main(String args) ch9_52 f=new ch9_52(); Container contentPane=f.getContentPane(); /设置GridLayout布局模式 contentPane.setLayout(new GridLayout(4,5); for (int i=1;i=4;i+) for (int j=1;j=5;j+) contentPane.add(new JButton(i+行+j+列); f.setVisible(true); CardLayout布局模式:ch9_53 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ch9_53 extends JFrame implements ActionListener JPanel p1,p2; JButton b1,b2,b3,b4,b5; public ch9_53() /标题 super(布局模式); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane=getContentPane(); /设置GridLayout布局模式 contentPane.setLayout(new GridLayout(2,1); /自行将布局改为中和南的模式,然后看效果 p1=new JPanel(); p2=new JPanel(); b1=new JButton(首个); b2=new JButton(下一个); b3=new JButton(上一个); b4=new JButton(末个); b5=new JButton(指定); contentPane.add(p1); contentPane.add(p2); /p1设置为CardLayout布局模式 p1.setLayout(new CardLayout(10,10); /p2设置为FlowLayout布局模式 p2.setLayout(new FlowLayout(); /p1面板中添加10个按钮 for (int i=1;i=10;i+) p1.add(a+i,new JButton(第+i+个); /p1面板中添加5个按钮 p2.add(b1); p2.add(b2); p2.add(b3); p2.add(b4); p2.add(b5); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); setVisible(true); public static void main(String args) ch9_53 f=new ch9_53(); public void actionPerformed(ActionEvent e) /显示首个 if (e.getSource()=b1) (CardLayout)p1.getLayout().first(p1); /显示下一个 if (e.getSource()=b2) (CardLayout)p1.getLayout().next(p1); /指定显示上一个 if (e.getSource()=b3) (CardLayout)p1.getLayout().previous(p1); /显示末个 if (e.getSource()=b4) (CardLayout)p1.getLayout().last(p1); /指定显示第三个 if (e.getSource()=b5) (CardLayout)p1.getLayout().show(p1,a3); NULL布局模式: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ch9_54 extends JFrame implements ActionListener JLabel l1; JButton b1; JTextField t1,t2,t3; Font f; public ch9_54() /标题 super(布局模式); /设置大小和位置 setBounds(20,20,500,300); /点击关闭按钮可以关闭窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane=getContentPane(); /设置null布局模式 contentPane.setLayout(null); b1=new JButton(=); l1=new JLabel(+); t1=new JTextField(20); t2=new JTextField(20); t3=new JTextField(20); f=new Font(宋体,Font.BOLD,36); b1.setFont(f); l1.setFont(f); t1.setFont(f); t2.setFont(f); t3.setFont(f); t1.setForeground(Color.red); t1.setBackground(Color.cyan); t1.setBounds(20,100,100,40); contentPane.add(t1); l1.setBounds(120,100,40,40); contentPane.add(l1); t2.setBounds(160,100,100,40); contentPane.add(t2); b1.setBounds(260,100,80,40); contentPane.add(b1); t3.setBounds(340,100,100,40); contentPane.add(t3); b1.addActionListener(this); setVisible(true); public static void main(String args) ch9_54 f=new ch9_54(); public void actionPerformed(ActionEvent e) /自行实现加法代码 【例9.7】使用JPanel。 import java.awt.*; import javax.swing.*; class JPanelDemo extends JPanel JButton b1 = new JButton(JPanel); JButton b2 = new JButton(Demo); public JPanelDemo() setBackground(Color.white); add(b1); add(b2); public static void main(String args) JPanel jp = new JPanelDemo(); jp.setBorder( BorderFactory.createTitledBorder( Hello,Border); JFrame frame = new JFrame(JPanelDemo); frame.setSize(200, 150); frame.setContentPane(jp); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setVisible(true); 注:jp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.red, Color.black),Hello,Border); 【例9.8】使用JTabbedPane容器。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JTabbedPaneDemo public static void main(String args) new MyTabbedPane(); class MyTabbedPane extends JFrame implements ChangeListener,ActionListener JTabbedPane jt; JButton jb; int index = 0; MyTabbedPane() super(使用标签面板容器); jt = new JTabbedPane(); jb = new JButton5; for(int i = 0;i5;i+) jbi = new JButton(第 + i + 页面板); jbi.addActionListener(this); jt.addTab(页标签 + i,jbi); jt.addChangeListener(this); getContentPane().add(jt,BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300,200); setVisible(true); public void stateChanged(ChangeEvent e) if(e.getSource()=jt) int i = (JTabbedPane)e.getSource() .getSelectedIndex(); jbindex.setVisible(false); jbi.setVisible(true); index = i; public void actionPerformed(ActionEvent e) setTitle(“响应单击” +(JButton)e.getSource().getText(); swing的版面结构示例2:菜单界面的完整示例(ch9_49.java) import javax.swing.*; import java.awt.event.*; public class ch9_49 extends JFrame public ch9_49() super(菜单演示); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(20,20,500,300); public static void main(String args) ch9_49 f=new ch9_49(); JMenuBar mbar=new JMenuBar(); JMenu mfile=f.buildFileMenu(); mbar.add(mfile); mbar.setOpaque(true); f.setJMenuBar(mbar); f.setVisible(true); public JMenu buildFileMenu() JMenu theedit=new JMenu(文件); theedit.setIcon(new ImageIcon(cut.gif); JMenuItem newf=new JMenuItem(新建,new ImageIcon(new.gif); JMenuItem open=new JMenuItem(打开,new ImageIcon(open.gif); JMenuItem close=new JMenuItem(关闭,new ImageIcon(paste.gif); JMenuItem quit=new JMenuItem(退出,new ImageIcon(save.gif); theedit.add(newf); theedit.add(open); theedit.add(close); theedit.addSeparator(); theedit.add(quit); return theedit; 【例9.9】设计一个GUI应用程序,有两个标签l1、l2和三个按钮b1,b2,b3。l1标签显示固定的文字,l2标签的文字随选择不同的按钮而变化;选择b1按钮时,l2标签显示为“欢迎进入Java世界”,选择b2按钮时,l2标签显示当前的日期,选择b3按钮时,退出该应用程序。程序如下: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class JButtonDemo extends Jpanel implements ActionListener JButton b1,b2,b3; static JLabel l1,l2; JButtonDemo() l1 = new JLabel( 这是一个演示按钮动作的程序,JLabel.CENTER); l2 = new JLabel( ,JLabel.CENTER); b1 = new JButton(欢迎w); b1.setMnemonic(KeyEvent.VK_W);/ 设置按钮的快捷键 b1.setActionCommand(welcome); b2 = new JButton(日期d); b2.setMnemonic(KeyEvent.VK_D);/ 设置快捷字符为D b2.setActionCommand(date); b3 = new JButton(退出q); b3.setMnemonic(KeyEvent.VK_Q);/ 设置快捷字符为Q b3.setActionCommand(quit); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); add(b1);add(b2);add(b3); public void actionPerformed(ActionEvent e) Calendar c1 = Calendar.getInstance(); if(e.getActionCommand().equals(welcome) l2.setText(欢迎进入Java世界。); else if(e.getActionCommand().equals(date) l2.setText(今天是+c1.get(Calendar.YEAR)+ 年+(c1.get(Calendar.MONTH)+1)+ 月 + c1.get(Calendar.DATE) + 日); else System.exit(0); l2.setHorizontalAlignment(JLabel.CENTER); / 标签文字水平居中 public static void main(String args) JFrame frame=new JFrame(使用JButton); frame.getContentPane().add(new JButtonDemo(),BorderLayout.SOUTH); frame.getContentPane().add(l1,BorderLayout.NORTH); frame.getContentPane().add(l2,BorderLayout.CENTER); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); 本程序中命令按钮设置了快捷字母键,可用鼠标单击或按Alt + 快捷字母来选择按钮。 【例9.10】带图形和HTML文字的按钮。 import java.awt.*; import javax.swing.*; public class JButtonDemo1 extends JFrame public static void main(String args) new JButtonDemo1( ); public JButtonDemo1( ) super(Using JButton); Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout(); JButton button1 = new JButton( Java); content.add(button1); ImageIcon im = new ImageIcon( images/newssms.gif); JButton button2 = new JButton(im); content.add(button2); JButton button3 = new JButton(Java, im); content.add(button3); JButton button4 = new JButton(Java, im); button4.setHorizontalTextPosition( SwingConstants.LEFT); content.add(button4); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); 【例9.11】具有文字对齐的标签。 import javax.swing.*; import java.awt.*; public class JLabelAlignDemo extends

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论