java图形界面编程技术手册_第1页
java图形界面编程技术手册_第2页
java图形界面编程技术手册_第3页
java图形界面编程技术手册_第4页
java图形界面编程技术手册_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

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

文档简介

1、图形界面编程技术手册编写:Kende_zhu第一节基本容器JFrame1、第一个JFrame窗体程序public class JFrameDemo public static void main(String args) JFrame jf = new JFrame(这是我的第一个swing窗体);Dimension d = new Dimension(300, 200);jf.setSize设置窗体大小(d);Point p = new Point(500, 400);jf.setLocation设置窗体位置(p);jf.setVisible设置窗体可见(true);第二节标签组件JLabe

2、l1、定义标签对象public class JLabelDemo public static void main(String args) JFrame jf = new JFrame(这是我的第一个swing窗体);JLabel lab = new JLabel(KENDE, JLabel.CENTER);/实例化标签对象jf.add(lab);将Label组件添加到JFrame面板中/将Label组件添加到JFrame面板中Dimension d = new Dimension(300, 200);jf.setSize(d);Point p = new Point(500, 400);设置

3、坐标位置jf.setLocation(p);jf.setVisible(true);2、定义标签字体标签可以设置字体,包括什么字体,字体的颜色,大小,是否斜体等public class JLabelDemo2 public static void main(String args) JFrame jf = new JFrame(这是我的第一个swing窗体);JLabel lab = new JLabel(KENDE, JLabel.CENTER);/实例化标签对象Font font = new Font(Dialog, Font.ITALIC + Font.BOLD, 30);lab.set

4、Font(font);定义标签字体jf.add(lab);/将组件添加到面板中Dimension d = new Dimension(300, 200);jf.setSize(d);Point p = new Point(500, 400);jf.setLocation(p);jf.setVisible(true);3、得到本机中所有的字体public class GetAllFonts public static void main(String args) GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnviron

5、ment();String fonts = g.getAvailableFontFamilyNames();for (String name: fonts) System.out.println(name);4、在Label中设置显示的图片public class JLabelDemo3 public static void main(String args) JFrame jf = new JFrame(这是我的第一个swing窗体);Icon icon = new ImageIcon(E:picBackGroud野生动物王国.jpg);JLabel lab = new JLabel(KEN

6、DE, iconLabel中加入图片, JLabel.CENTER);/ 实例化标签对象Font font = new Font(Serif, Font.ITALIC + Font.BOLD, 30);lab.setFont(font);jf.add(lab)将标签组件添加到JFrame面板中;/ 将组件添加到面板中Dimension d = new Dimension(300, 200);jf.setSize(d);Point p = new Point(500, 400);jf.setLocation(p);jf.setVisible(true);第三节按钮组件JButton1、在JFra

7、me中增加按钮public class JButtonDemo01 public static void main(String args) JFrame frame = new JFrame(Welcome to Kendes home!);JButton bt = new JButton(按钮);定义JButton实例frame.add(bt);往JFrame中添加按钮frame.setSize(300,200);frame.setLocation(400, 300);frame.setVisible(true);2、往JButton中添加图片public class JButtonDem

8、o02 public static void main(String args) JFrame frame = new JFrame(Welcome to Kendes home!);String picPath = E:picBackGrouda.jpg;Icon icon = new ImageIcon(picPath);JButton bt = new JButton(icon);往JButton中设置图片frame.add(bt);frame.setSize(800,500);frame.setLocation(400, 300);frame.setVisible(true);第四节布

9、局管理器1、FlowLayout布局管理器FlowLayout属于流式布局管理器,使用此种布局方式,所有的组件会像流水一样依次排列。public class FlowLayoutDemo01 public static void main(String args) JFrame frame = new JFrame(Welcome to kendes home);frame.setLayout设置JFrame容器的流式布局管理器(new FlowLayout(FlowLayout.CENTER, 20组件的水平间距, 20组件的垂直间距);JButton button = null;for(i

10、nt i = 0; i 9; i +)button = new JButton(按钮- + i);frame.add(button);frame.setSize(800,500);frame.setLocation(400, 300);frame.setVisible(true);效果图:2、BorderLayout布局管理器BorderLayout将一个窗体的版面划成东,西,南,北,中五个区域,可以直接将需要的组件放到五个区域即可public class BorderLayoutDemo01 public static void main(String args) JFrame frame

11、= new JFrame(Welcome to kendes home);frame.setLayout设置JFrame容器的Border布局管理器(东西南北中)(new BorderLayout(20, 20);frame.add(new JButton(东), BorderLayout.EAST将按钮组件放在BorderLayout的东部);frame.add(new JButton(西), BorderLayout.WEST);frame.add(new JButton(南), BorderLayout.SOUTH);frame.add(new JButton(北), BorderLay

12、out.NORTH);frame.add(new JButton(中), BorderLayout.CENTER);frame.setSize(500,300);frame.setLocation(400, 300);frame.setVisible(true);3、GridLayout布局管理器GridLayout布局管理器是以表格的形式进行管理的,在使用此布局管理器的时候必须设置显示的行数和列数。public class GridLayoutDemo01 public static void main(String args) JFrame frame = new JFrame(Welco

13、me to kendes home);frame.setLayout(new GridLayout(3, 5表示3行5列, 3, 3水平间距和垂直间距)添加表格形式的布局管理器;JButton button = null;for(int i = 0; i 13; i +)button = new JButton(按钮- + i);frame.add(button);frame.setSize(500,300);frame.setLocation(400, 300);frame.setVisible(true);效果图:4、CardLayout布局管理器CardLayout就是将一组组件彼此重叠

14、的进行布局,就像一张张卡片一样,这样每次只会展现一个界面public class CardLayoutDemo01 public static void main(String args) JFrame frame = new JFrame(Welcome to kendes home);CardLayout card = new CardLayout();frame.setLayout(card);将JFrame容器设置为CardLayout布局方式Container con = frame.getContentPane()得到JFrame窗体的contentPane对象;con.add将指

15、定的组件添加到此JFrame容器的尾部(new JLabel(标签-A, JLabel.CENTER), first);con.add(new JLabel(标签-B, JLabel.CENTER), second);con.add(new JLabel(标签-C, JLabel.CENTER), third);con.add(new JLabel(标签-D, JLabel.CENTER), fourth);con.add(new JLabel(标签-E, JLabel.CENTER), fifth);frame.pack()自动调整窗体的大小;frame.setVisible(true);c

16、ard.show(con, third);翻转到具有指定name的组件:比如翻转到指定name为third的组件,这个时候对应的组件为标签 Cfor (int i = 0; i 窗口被选中) ;public void windowClosed(WindowEvent e)System.out.println(windowClosed - 窗口被关闭) ;public void windowClosing(WindowEvent e)System.out.println(windowClosing - 窗口关闭) ;System.exit(1) ;public void windowDeacti

17、vated(WindowEvent e)System.out.println(windowDeactivated - 取消窗口选中) ;public void windowDeiconified(WindowEvent e)System.out.println(windowDeiconified - 窗口从最小化恢复) ;public void windowIconified(WindowEvent e)System.out.println(windowIconified - 窗口最小化) ;public void windowOpened(WindowEvent e)System.out.p

18、rintln(windowOpened - 窗口被打开) ;public class MyEventWindowEventJFrame01public static void main(String args)JFrame frame = new JFrame(Welcome To kendes home) ; frame.addWindowListener(new MyWindowEventHandle() ;/ 加入事件frame.setSize(300,150) ;frame.setBackground(Color.WHITE) ;frame.setLocation(300,200) ;

19、frame.setVisible(true) ;注意:发现以上程序虽然实现了窗口关闭,但是没有必要实现那么多的方法,可以如何做呢,其实在事件中提供了很多的适配器class MyWindowEventHandle2 extends WindowAdapterpublic void windowClosing(WindowEvent e)System.out.println(windowClosing - 窗口关闭) ;System.exit(1) ;public class MyEventWindowEventJFrame02public static void main(String args

20、)JFrame frame = new JFrame(Welcome To kendes home) ; frame.addWindowListener(new MyWindowEventHandle2() ;/ 加入事件frame.setSize(300,150) ;frame.setBackground(Color.WHITE) ;frame.setLocation(300,200) ;frame.setVisible(true) ;现在只要求一个关闭,资源太浪费了,可以使用匿名内部类的做法减少代码量public class MyEventWindowEventJFrame03public

21、 static void main(String args)JFrame frame = new JFrame(Welcome To kendes home) ; frame.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.out.println(windowClosing - 窗口关闭) ;System.exit(1) ;) ;/ 加入事件frame.setSize(300,150) ;frame.setBackground(Color.WHITE) ;frame.setL

22、ocation(300,200) ;frame.setVisible(true) ;2、按钮事件如果要使得按钮有效,那么只需要使用ActionListener接口class ActionHandleprivate JFrame frame = new JFrame(Welcome To kendes home) ; private JButton but = new JButton(显示);private JLabel lab = new JLabel() ;private JTextField text = new JTextField(10) ;private JPanel pan = n

23、ew JPanel() ;public ActionHandle()Font fnt = new Font(Serief,Font.ITALIC + Font.BOLD,28) ;lab.setFont(fnt) ;/ 设置标签的显示文字lab.setText(等待用户输入信息!) ;but.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)/ if(e.getSource() instanceof JButton)/ 判断是否是按钮if(e.getSource()=but)lab.s

24、etText(text.getText() ;) ;frame.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(1) ;) ;/ 加入事件frame.setLayout(new GridLayout(2,1) ;pan.setLayout(new GridLayout(1,2) ;pan.add(text);pan.add(but) ;frame.add(pan) ;frame.add(lab) ;frame.pack() ;frame.setBackground(

25、Color.WHITE) ;frame.setLocation(300,200) ;frame.setVisible(true) ;public class MyActionEventDemo01public static void main(String args) new ActionHandle() ;完成一个简单的用户登录,用户名是kende,密码是123class LoginCheckprivate String name ;private String password ;public LoginCheck(String name,String password)

26、 = name ;this.password = password ;public boolean validate()if(kende.equals(name)&123.equals(password)return true ;elsereturn false ;class ActionHandle2private JFrame frame = new JFrame(Welcome To kendes home) ; private JButton submit = new JButton(登陆);private JButton reset = new JButton(重置);private

27、 JLabel nameLab = new JLabel(用户名:) ;private JLabel passLab = new JLabel(密 码:) ;private JLabel infoLab = new JLabel(用户登陆系统) ;private JTextField nameText = new JTextField(10) ;private JPasswordField passText = new JPasswordField() ;private JPanel pan = new JPanel() ;public ActionHandle2()Font fnt = ne

28、w Font(Serief,Font.ITALIC + Font.BOLD,12) ;infoLab.setFont(fnt) ;/ 设置标签的显示文字submit.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(e.getSource()=submit)String tname = nameText.getText() ;String tpass = new String(passText.getPassword() ;LoginCheck log = new LoginCh

29、eck(tname,tpass) ;if(log.validate()infoLab.setText(登陆成功,欢迎光临!) ;elseinfoLab.setText(登陆失败,错误的用户名或密码!) ;) ;reset.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(e.getSource()=reset)nameText.setText() ;passText.setText() ;infoLab.setText(用户登陆系统) ;) ;frame.addWindowLis

30、tener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(1) ;) ;/ 加入事件frame.setLayout(null) ;nameLab.setBounds(5,5,60,20) ;passLab.setBounds(5,30,60,20) ;infoLab.setBounds(5,65,220,30) ;nameText.setBounds(65,5,100,20) ;passText.setBounds(65,30,100,20) ;submit.setBounds(165,5,60,2

31、0) ;reset.setBounds(165,30,60,20) ;frame.add(nameLab) ;frame.add(passLab) ;frame.add(infoLab) ;frame.add(nameText) ;frame.add(passText) ;frame.add(submit) ;frame.add(reset) ;frame.setSize(280,130) ;frame.setBackground(Color.WHITE) ;frame.setLocation(300,200) ;frame.setVisible(true) ;public class MyA

32、ctionEventDemo03public static void main(String args) new ActionHandle2() ;3、键盘事件class MyKeyHandle extends JFrame implements KeyListenerprivate JTextArea text = new JTextArea() ;public MyKeyHandle()super.setTitle(Welcome To kendes home) ;JScrollPane scr = new JScrollPane(text) ;scr.setBounds(5,5,300,

33、200) ;super.add(scr) ;text.addKeyListener(this) ;super.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(1) ;) ;/ 加入事件super.setSize(310,210) ;super.setVisible(true) ;public void keyPressed(KeyEvent e)text.append(键盘“ + KeyEvent.getKeyText(e.getKeyCode()+ ”键按下n)

34、;public void keyReleased(KeyEvent e)text.append(键盘“ + KeyEvent.getKeyText(e.getKeyCode()+ ”键松开n) ;public void keyTyped(KeyEvent e)text.append(输入的内容是: + e.getKeyChar() + n) ; ;public class MyKeyEventDemo01public static void main(String args)new MyKeyHandle() ;以上的键盘事件中,类必须要实现接口中的所有方法,太麻烦,那么在swing中也提供了

35、键盘处理的Adapter类,直接使用此类即可。class MyKeyHandle2 extends JFrameprivate JTextArea text = new JTextArea() ;public MyKeyHandle2()super.setTitle(Welcome To kendes home) ;JScrollPane scr = new JScrollPane(text) ;scr.setBounds(5,5,300,200) ;super.add(scr) ;text.addKeyListener(new KeyAdapter()public void keyTyped

36、(KeyEvent e)text.append(输入的内容是: + e.getKeyChar() + n) ; ) ;super.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(1) ;) ;/ 加入事件super.setSize(310,210) ;super.setVisible(true) ;public class MyKeyEventDemo02public static void main(String args)new MyKeyHandle2() ;

37、4、鼠标事件class MyMouseHandle extends JFrame implements MouseListenerprivate JTextArea text = new JTextArea() ;public MyMouseHandle()super.setTitle(Welcome To kendes home) ;JScrollPane scr = new JScrollPane(text) ;scr.setBounds(5,5,300,200) ;super.add(scr) ;text.addMouseListener(this) ;super.addWindowLi

38、stener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(1) ;) ;/ 加入事件super.setSize(310,210) ;super.setVisible(true) ;public void mouseClicked(MouseEvent e)int c = e.getButton() ;String mouseInfo = null ;if(c=MouseEvent.BUTTON1)mouseInfo = 左键 ;if(c=MouseEvent.BUTTON3)mouseInfo =

39、 右键 ;if(c=MouseEvent.BUTTON2)mouseInfo = 滚轴 ;text.append(鼠标单击: + mouseInfo + n) ;public void mouseEntered(MouseEvent e)text.append(鼠标进入组件。n) ;public void mouseExited(MouseEvent e)text.append(鼠标离开组件。n) ;public void mousePressed(MouseEvent e)text.append(鼠标按下。n) ;public void mouseReleased(MouseEvent e)text.append(鼠标松开。n) ;public class MyMouseEven

温馨提示

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

评论

0/150

提交评论