• Java AWT组件开发和Swing界面编程


    一、AWT组件开发

    1、AWT

           AWT是抽象窗口工具箱的缩写,它为编写图形用户界面提供了用户接口,通过这个接口就可以继承很多方法,省去了很多工作。AWT还能使应用程序更好地同用户进行交互。

           AWT中的容器是一种特殊的组件,他可以包含其他组件,即可以把组件方法容器中。Container类是用来存放其他组件的Component类的子类,Frame类又是Component的子类。Frame类用于创建具有标题栏和边界的窗口。这里通过继承Frame类来建立自己的界面。

    [java] view plain copy
     
     print?
    1. public class test extendsFrame{  
    2.     //创建构造器  
    3.     public test() throws HeadlessException {  
    4.         this.setTitle("第一个窗口程序");  
    5.         //x , y是距离显示器左上角的距离;w , h是窗口的长宽  
    6.         this.setBounds(100, 100, 250, 250);  
    7.         //或者使用以下方式代替上面的一句代码  
    8. //        this.setLocation(100, 100);  
    9. //        this.setSize(250 , 250);  
    10.         this.setVisible(true);  
    11.     }  
    12.     public static void main(String[] str) {  
    13.         new test();  
    14.     }  
    15. }  

           上面是窗口中一些必不可少的东西,下面是一些窗口的基础应用:

           >setTitle(“窗口”):定义窗口名称

           >add(button):把按钮添加到窗口中

           >setBackground(Color):设置窗口的背景颜色

           >setResizable(boolean):设置窗口大小是否可以改变

           >setAlwaysOnTop(boolean):设置窗口是否总在最上面

           >setBounds(x, y, w, h):设置窗口起始位置和大小

           >setVisible(boolean):设置窗口可见

           如果想创建多个窗口,只需要在主方法main()中创建多个对象就行了。

    2、布局管理器

           Java中的图形界面在布局管理上采用容器和布局管理相分离的方案,也就是说容器只是把组件放进来,但它不管怎样放置。至于如何放置需要用到布局管理器。Java中有几种布局管理器,分别是:FlowLayout , BorderLayout, GridLayout和GardLayout。

           1)、FlowLayout

           FlowLayout布局管理器是默认的布局管理器,它将组件按照从左到右、从上到下的顺序来安排,并在默认情况下使组件尽量居中放置。下面的代码要添加到test()类中:

            this.setLayout(new FlowLayout());

            //将按钮组件放入容器中

            this.add(new Button("确定"));

            this.add(new Button("取消"));

           大家可以通过创建许多按钮来观察FlowLayout对组件的摆放规律,下面是使用一个按钮监听事件来实现按钮的添加:

    [java] view plain copy
     
     print?
    1. public class test extendsFrame implements ActionListener{  
    2.     int i;  
    3.     Button b = new Button("Add");  
    4.     public test() throws HeadlessException {  
    5.         this.setTitle("第一个窗口程序");  
    6.         this.setLayout(new FlowLayout());  
    7.         this.add(b);  
    8.         b.addActionListener(this);  
    9.         this.setBounds(100, 100, 250, 250);  
    10.         this.setVisible(true);  
    11.     }  
    12.     @Override  
    13.     public void actionPerformed(ActionEvent e){  
    14.         i++;  
    15.         Button bi = newButton("Button" + i);  
    16.         this.add(bi);  
    17.         this.setVisible(true);  
    18.     }  
    19.     public static void main(String[] str) {  
    20.         new test();  
    21.     }  
    22. }  

           在本程序中,由于按钮的大小不同,其整体看起来不太整齐,但这更说明了FlowLayout布局管理器的规则。

           2)、BorderLayout

           BorderLayout布局管理器只允许在容器内放置5个组件,这5个组件的位置是由BorderLayout类中的North、South、East、West和Center5个常量来确定的,他们对应着容器中的上下左右中,用法如下:

           this.add(new Button(“按钮”) ,BorderLayout.NORTH);

           this.add(new Button(“按钮”) ,BorderLayout.CENTER);

           组件在BorderLayout中的大小都是可以改变的。一般情况下可以让中间区域大一些,而且可以只用其中几个区域。

           3)、GridLayout

           GridLayout布局管理器是矩形网格,在网格中放置组件,每个网格的高度和宽度都相等,组件的排列顺序与FlowLayout相同。组件随着网格的大小而在水平和垂直方向上拉伸,网格的大小是由容器的大小和创建网格的多少来确定的。其用法如下:

           this.setLayout(newGridLayout(2 , 3)); //创建一个2行3列的网格

           this.add(new Button(“按钮”));

           当组件数目大于网格数时,GridLayout保持行数不变而自动增加列数。

           4)、CardLayout

           CardLayout运行在一个组件中每次只显示一组组件中的某一个,用户可以根据需要来选择使用哪个组件。CardLayout类提供了如下选择组件的方法。

           >First(Container p):选择容器中的第一个组件

           >last(Container p):选择容器中的最后一个组件

           >next(Container p):选择容器中当前组件的下一个组件

           >prebvious(Container p):选择容器中当前组件的上一个组件

           >show(Container p ,String name):选择容器中指定的组件

           前面几种很容易理解,而show()方法来选择容器中指定的组件。它的第一个参数是管理组件的容器,第二个参数是标识要显示组件字符串,该字符串与将组件添加到容器时使用的字符串相同。其用法如下:

    [java] view plain copy
     
     print?
    1. public class test extendsFrame implements ActionListener{  
    2.     Panel p = new Panel();  
    3.     Button bf = new Button("First");  
    4.     Button bl = new Button("Last");  
    5.     Button bn = new Button("next");  
    6.     Button bp = newButton("previous");  
    7.     Button bg = new Button("Go");  
    8.     TextField tf = new TextField();  
    9.     //设置CardLayout布局管理器  
    10.     CardLayout cl = new CardLayout();  
    11.     public test() throws HeadlessException {  
    12.         this.setTitle("CardLayout布局管理器");  
    13.         this.setLayout(null);  
    14.         this.add(p);  
    15.         //定义p面板为CardLayout布局管理器  
    16.         p.setLayout(cl);  
    17.         //为CardLayout布局管理器添加按钮  
    18.         for (int i = 1; i <= 10; i++) {  
    19.             Button btemp = newButton("Button" + i);  
    20.             p.add(btemp, "" + i);  
    21.         }  
    22.         //为每个组件设置大小位置并添加到容器中  
    23.         p.setBounds(10, 40, 100, 100);  
    24.         this.add(bf);  
    25.         bf.addActionListener(this);  
    26.         bf.setBounds(120, 40, 60, 20);  
    27.         this.add(bl);  
    28.         bl.addActionListener(this);  
    29.         bl.setBounds(120, 70, 60, 20);  
    30.         this.add(bn);  
    31.         bn.addActionListener(this);  
    32.         bn.setBounds(120, 100, 60, 20);  
    33.         this.add(bp);  
    34.         bp.addActionListener(this);  
    35.         bp.setBounds(120, 130, 60, 20);  
    36.         this.add(bg);  
    37.         bg.addActionListener(this);  
    38.         bg.setBounds(60, 160, 40, 20);  
    39.         this.add(tf);  
    40.         tf.setBounds(20, 160, 40, 20);  
    41.         //设置窗口的位置和大小  
    42.         this.setBounds(200, 200, 210, 220);  
    43.         this.setVisible(true);  
    44.     }  
    45.     @Override  
    46.     public void actionPerformed(ActionEvent e){  
    47.         if (e.getSource() == bn) {  
    48.             cl.next(p);  
    49.         }  
    50.         if (e.getSource() == bp) {  
    51.             cl.previous(p);  
    52.         }  
    53.         if (e.getSource() == bf) {  
    54.             cl.first(p);  
    55.         }  
    56.         if (e.getSource() == bl) {  
    57.             cl.last(p);  
    58.         }  
    59.         if (e.getSource() == bg) {  
    60.             cl.show(p, tf.getText().trim());  
    61.             tf.setText("");  
    62.         }  
    63.     }  
    64.     public static void main(String[] args){  
    65.         new test();  
    66.     }  

    3、组件和监听接口

           组件和监听接口是分不开的。组件和监听接口在AWT中有很多,我们只对复杂的、难以理解的进行讲解,其他的大家可以通过API自行学习。

           1)、按钮和ActionListener

           ActionListener是由处理ActionEvent事件的监听器对象实现的。当单击按钮、在文本区域中按回车键、选择菜单项、双击列表项都会触发监听器。该接口中的方法为:

           public voidactoinPerformed(ActionEvent e)

           下面是一个实现ActionListener接口的实例:

    [java] view plain copy
     
     print?
    1. public class mytest01extends Frame implements ActionListener{  
    2.     Button b1 = new Button("进入社区");  
    3.     Button b2 = new Button("退出");  
    4.     public mytest01() throws HeadlessException{  
    5.         this.setTitle("论坛");  
    6.         this.add(b1);  
    7.         this.add(b2 , BorderLayout.SOUTH);  
    8.         //为按钮添加监听  
    9.         b1.addActionListener(this);  
    10.         b2.addActionListener(this);  
    11.         this.setBounds(100, 100, 200, 300);  
    12.         this.setVisible(true);  
    13.     }  
    14.     //实现ActionListener接口中的方法  
    15.     public void actionPerformed(ActionEvent e){  
    16.         if (e.getSource() == b1) {  
    17.             System.out.println("进入社区");  
    18.         }  
    19.         else{  
    20.             System.out.println("退出");  
    21.         }  
    22.     }  
    23.     public static void main(String[] args){  
    24.         new mytest01();  
    25.     }  
    26. }  

           2)、运用WindowListener

           WindowListener是处理WindowEvent事件的对象实现的。这个监听器确定了窗口设么时候被打开、关闭、激活、不激活、最小化和最大化。该接口中的方法有:

           >public voidwindowActivated(WindowEvent e)

           >public voidwindowClosed(WindowEvent e)

           >public voidwindowClosing(WindowEvent e)

           >public voidwindowDeactivated(WindowEvent e)

           >public voidwindowDeiconfied(WindowEvent e)

           >public void windowIconified(WindowEvente)

           >public voidwindowOpened(WindowEvent e)

           这些接口很容易但是有点繁琐,下面的实例程序非常清楚:

    [java] view plain copy
     
     print?
    1. public class mytest01extends Frame implements WindowListener{  
    2.     public mytest01() throws HeadlessException{  
    3.        this.setTitle("WindowListener");  
    4.         this.addWindowListener(this);  
    5.         this.setBounds(100, 100, 200, 300);  
    6.         this.setVisible(true);  
    7.     }  
    8.     //实现接口中的方法  
    9.     public void windowOpened(WindowEvent e) {  
    10.         System.out.println("打开");  
    11.     }  
    12.     public void windowClosing(WindowEvent e) {  
    13.         System.out.println("菜单关闭");  
    14.         this.dispose();  
    15.     }  
    16.     public void windowClosed(WindowEvent e) {  
    17.         System.out.println("释放");  
    18.     }  
    19.     public void windowIconified(WindowEvent e){  
    20.         System.out.println("最小化");  
    21.     }  
    22.     public void windowDeiconified(WindowEvente) {  
    23.         System.out.println("最大化");  
    24.     }  
    25.     public void windowActivated(WindowEvent e){  
    26.         System.out.println("激活");  
    27.     }  
    28.     public void windowDeactivated(WindowEvente) {  
    29.         System.out.println("失去焦点");  
    30.     }  
    31.     public static void main(String[] args){  
    32.         new mytest01();  
    33.     }  
    34. }  

           3)、文本组件和TextListener

           文本组件就像把窗口空白处看成记事本一样,它是一个用来写内容的组件。TextListener用来确定何时文本值改变。该接口还可以用到很多地方,其接口方法如下:

           public voidtextValueChanged(TextEvent e)

           下面通过实例来了解TextListener监听事件的使用

    [java] view plain copy
     
     print?
    1. public class mytest01extends Frame implements TextListener{  
    2.     TextArea ta = newTextArea("saasdfgadsfg");  
    3.     public mytest01() throws HeadlessException{  
    4.         this.setTitle("textArea");  
    5.         this.add(ta);  
    6.         //设置文字样式  
    7.         Font f = new Font("宋体", Font.ITALIC+ Font.BOLD, 50);  
    8.         ta.setFont(f);  
    9.         ta.addTextListener(this);  
    10.         ta.setForeground(Color.red);  
    11.         this.setBounds(100, 100, 300, 300);  
    12.         this.setVisible(true);  
    13.     }  
    14.     @Override  
    15.     public void textValueChanged(TextEvent e) {  
    16.         System.out.println(ta.getText());  
    17.     }  
    18.     public static void main(String[] args){  
    19.         new mytest01();  
    20.     }  
    21. }  

           上面的这段程序使用TextListener监听文本的输入、删除等操作,就像记事本一样可以对文本进行修改、录入。

    二、Swing界面编程

           随着Java的发展,AWT已经渐渐被淘汰,它已经不能适应发展的需要,不能满足开发功能强大的用户界面的需要。这时Swing出现了,它是建立在AWT之上的组件集,在不同的平台上都能保持组件的界面样式,因此得到了非常广泛的应用。

    1、Swing组件库

           在Swing组件中有许多种组件,它们被封装在JFC中,下面我们会对每一种组件进行详细介绍。Swing包很多,但平常用到的只有javax.swing.*和javax.swing.event.*这两个包,其他的很少用到。

           1)、JFC结构

           JFC是Java的基础类,是Java Foundation Classes的缩写形式,封装了一组用于构建图形用户界面的组件和特性。JFC包含了图形用户界面构建中需要用到的顶级容器(Applet、Dialog、Frame)、普通容器(面板、滚动面板、拆分窗格组件、选项卡插U能给个和工具条等)、特殊容器(InternalFrame、Layeredpane、root pane)、基本组件(button , combo box , list , menu , slider , spinner和textfild)等。

           2)、与AWT的区别

           最大的区别在于Swing组件的实现与本地实现无关。Swing组件比AWT组件具有更多的功能。例如在Swing中添加了按钮组件和标签组件,通过继承来更改Swing组件的行为和外观,访问技术等。

    2、Jfram窗口容器

           它定义了一个UI程序的框架,是图形程序不可缺少的一部分。它与java.awt.Frame类很类似,它是RootPaneContainer的一种,是顶层的Swing容器。通过继承jframe,所构建的容器就有一些最基本的组件。例如和关闭按钮相对应的容器有一个setDefaultCloseOperation(int operation)方法,这是每个Swing程序都有的,它有四个参数:

           >DO_NOTHING_ON_CLOSE(单击后不管用)

           >HIDE_ON_CLOSE(单击后窗口隐藏)

           >DISPOSE_ON_CLOSE(单击后窗口释放)

           >EXIT_ON_CLOSE(单击后退出)

           Jframe是最重要的顶层容器,其显示效果是一个窗口,带有标题和尺寸重置角标。可以在其中添加按钮、标签等组件。下面是一个应用JFrame类的基本程序:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame{  
    2.     public test() throws HeadlessException {  
    3.         this.setLayout(new GridLayout(2, 2));  
    4.         this.setBounds(10, 10, 600, 400);  
    5.         this.setVisible(true);  
    6.         //设置当单机窗口的关闭按钮时退出  
    7.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    8.     }  
    9.     public static void main(String[] args){  
    10.         new test();  
    11.     }  
    12. }  

    3、通过Icon接口进行图像操作

           Icon接口用于显示图像。在Icon接口中定义了许多方法,这些方法都是图像应用方面必不可少的。

           在Swing组件中支持图像显示。ImageIcon类用来描述图像。创建Icon对象的方法有3种:

           >new ImageIcon(Image i)

           >new ImageIcon(Stringfilename)(参数为图像文件的名称)

           >new ImageIcon(URL u)(参数为网络地址)

           实现Icon接口的方法也有3种:

           >PaintIcon(Graphics)

           >getIconWidth()(设置图像宽度)

           >getIconHeight()(设置图像长度)

           JLable是一种既可以包含文本又可以包含图像的控件,该控件不能响应用户的动作。创建一个JLable的方法如下:

           Jlabel j1 = new JLable(“a”);

           this.add(j1);

           Icon组件可以实现带有图标的按钮或标签。Icon是一个固定大小的图片,通常用于装饰组件。下面是一个Icon的实例应用:

    [java] view plain copy
     
     print?
      1. public class test extendsJFrame{  
      2.     JLabel j1 = new JLabel("a");  
      3.     JLabel j2 = new JLabel("b");  
      4.     public test() throws HeadlessException {  
      5.         this.setLayout(new GridLayout(2, 2));  
      6.         this.add(j1);  
      7.         this.add(j2);  
      8.         //引入原有图片  
      9.         ImageIcon i1 = new ImageIcon("D:\桌面\桌面\安卓开发工具\素材\图标\a1.png");  
      10.         j1.setIcon(i1);  
      11.         //引入自做图片  
      12.         Icon i2 = new MyIconlmp();  
      13.         j2.setIcon(i2);  
      14.         this.setBounds(10, 10, 600, 400);  
      15.         this.setVisible(true);  
      16.         //设置当单机窗口的关闭按钮时退出  
      17.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      18.     }  
      19.     //定义一个挥之图片的类  
      20.     class MyIconlmp implements Icon{  
      21.         public void paintIcon(Component c,Graphics g, int x, int y) {  
      22.             for (int i = 0; i < 3; i++) {  
      23.                 g.setColor(new Color(30*i,50*i, 60*i));  
      24.                 g.fillOval(10, 10 + 100*i, 120,80);  
      25.             }  
      26.         }  
      27.         //设置图片宽度  
      28.         public int getIconWidth() {  
      29.             return 200;  
      30.         }  
      31.         //设置图片高度  
      32.         public int getIconHeight() {  
      33.             return 200;  
      34.         }  
      35.     }  
      36.     public static void main(String[] args){  
      37.         new test();  
      38.     }  
      39. }  

    4、按钮

           JButton类用来定义按钮。JButton类的常用方法如下:

           >addActionListener():注册点击事件监听器;

           >setText():设置按钮文字;

           >setIcon():设置按钮图标。

           通过组建的setMnemonic()方法可以设置组件Mnemonic助记符。通过组件的setTipText可以设置组建的ToolTip提示信息。

           在Swing中,JButton是有AbstractButton类派生的。AbstractButton类派生了两个组件:JButton和JtoggleButton组件。JButton是Swing的按钮,而ToggleButton组件是单选按钮和复选框的基类。下面是一个按钮的应用程序。

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame implements ActionListener{  
    2.     JButton jb = new JButton("Clickme!");  
    3.     public test() throws HeadlessException {  
    4.         this.setLayout(new GridLayout(2, 2));  
    5.         this.add(jb);  
    6.         jb.addActionListener(this);  
    7.         jb.setMnemonic('b');//给按钮设置组件助记符  
    8.         jb.setToolTipText("I am abutton");  
    9.         this.setBounds(10, 10, 600, 400);  
    10.         this.setVisible(true);  
    11.         //设置当单机窗口的关闭按钮时退出  
    12.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    13.     }  
    14.     public static void main(String[] args){  
    15.         new test();  
    16.     }  
    17.     @Override  
    18.     public void actionPerformed(ActionEvent e){  
    19.         this.jb.setText("Clicked");  
    20.     }  
    21. }  

    5、复选框

           在Swing中,用JcheckBox类来定义复选框。复选框和单选按钮有点类似,但是一组复选框中可以有任何数量的复选框被选中。复选框可以为每一次的单击操作添加一个事件。但平常只会监听事件,因为它们让客户来确定该单击操作时选中还是取消选中复选框。

           复选框又被称为检测盒。JcheckBox提供选中/未选中两种状态,当用户单击复选框时改变复选框原来设置的状态。

    6、弹出式菜单

           JPopupMenu是一种Menu的组件,因此在使用JPopupMenu时都需要一个Container来放置JPopupMenu。

           通过JpopupMenu类可以定义弹出式菜单,其重要方法有:

           >add(JmenuItem e):(往菜单中增加菜单项)

           >show():(显示菜单)

           通过JMenuItem类来定义菜单项,通过addActionListener()为菜单项增加事件处理。

           JpopupMenu是一个可弹出并显示一系列选项的小窗口,可用于用户在菜单栏上选择选项时显示菜单,还可以用于当用户选择菜单项并激活时显示“右拉式(pull-right)“菜单。通过下面的程序进一步了解相关的知识:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame {  
    2.     //定义一个弹出式菜单  
    3.     JPopupMenu jpm = new JPopupMenu();  
    4.     public test() throws HeadlessException {  
    5.         this.setLayout(new GridLayout(2, 2));  
    6.         this.setBounds(10, 10, 600, 400);  
    7.         this.setVisible(true);  
    8.         //设置当单机窗口的关闭按钮时退出  
    9.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    10.         //引入jiaPopMenu()方法  
    11.         this.jiaPopMenu();  
    12.     }  
    13.     public void jiaPopMenu(){  
    14.         JMenuItem item = newJMenuItem("A");  
    15.         //为A添加监听事件  
    16.         item.addActionListener(newActionListener() {  
    17.             public voidactionPerformed(ActionEvent e) {  
    18.                 System.out.println("AClicked!!!");  
    19.             }  
    20.         });  
    21.         jpm.add(item);//将弹出项添加到菜单  
    22.         //定义菜单项  
    23.         item = new JMenuItem("B");  
    24.         item.addActionListener(newActionListener() {  
    25.             public voidactionPerformed(ActionEvent e) {  
    26.                 System.out.println("BClicked");  
    27.             }  
    28.         });  
    29.         jpm.add(item);  
    30.         //编写右键弹出单击事件  
    31.         this.addMouseListener(newMouseAdapter() {  
    32.             public voidmouseReleased(MouseEvent e) {  
    33.                 if (e.isPopupTrigger()) {  
    34.                     jpm.show(e.getComponent(),e.getX(), e.getY());  
    35.                 }  
    36.             }  
    37.         });  
    38.     }  
    39.     public static void main(String[] args){  
    40.         new test();  
    41.     }  
    42. }  

           使用JPopupMenu组件实现右键快捷菜单功能,并使用ActionListener的对象来将菜单激活,当右击时即可弹出菜单。

    7、单选按钮

           单选按钮的实质就是在一组按钮中一次只能有一个按钮被选中。单选按钮的外观类似复选框,但是复选框没有对可以选择的选项数目进行限制。对于每一组的单选按钮,必须创建一个ButtonGroup对象实例并且将每一个单选按钮添加到该ButtonGroup对象中。下面的实例讲解的单选按钮的基本用法:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame {  
    2.     //定义两个单选选项  
    3.     JRadioButton r1 = newJRadioButton("No.1");  
    4.     JRadioButton r2 = newJRadioButton("No.2");  
    5.     //定义一个按钮组  
    6.     ButtonGroup bg = new ButtonGroup();  
    7.     public test() throws HeadlessException {  
    8.         this.setLayout(new GridLayout(3, 1));  
    9.         this.setBounds(10, 10, 600, 400);  
    10.         this.setVisible(true);  
    11.         //设置当单机窗口的关闭按钮时退出  
    12.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    13.         //引入jiaPopMenu()方法  
    14.         this.Add();  
    15.     }  
    16.     public void Add(){  
    17.         //将单选按钮加入按钮组中  
    18.         bg.add(r1);  
    19.         bg.add(r2);  
    20.         //将单选按钮加入布局管理器  
    21.         this.add(r1);  
    22.         this.add(r2);  
    23.         //默认选中r2  
    24.         r2.setSelected(true);  
    25.     }  
    26.     public static void main(String[] args){  
    27.         new test();  
    28.     }  
    29. }  

    8、下拉列表框

           下拉列表框可以让人感觉到整个界面很简洁,在大的网页中都能感觉到这一点。列表可以有许多选项,所以它们通常被放置在一个滚动窗格中。组合框与下拉列表框相似,区别在于使用组合框时用户可以不从列表中选择项目,还可以选择一个项目。在某些版本的组合框中,还可以输入自己的选择,如浏览器的地址栏。

           JComboBox方法很多,它们主要用来管理列表中的数据:

           >addItem():添加一个项目到JComboBox

           >get/setSelectedIndex():获取/设置JComboBox中选中项目的索引

           >get/setSelectedItem():获取/设置选中的对象

           >removeAllItems():从JComboBox删除所有对象

           >remoteItem():从JComboBox删除特定对象

           下面是一个下拉别表框的实例,从中可以更详细的了解到下拉列表框的使用:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame {  
    2.     //定义下啦菜单  
    3.     JComboBox jcb = new JComboBox();  
    4.     public test() throws HeadlessException {  
    5.         this.setLayout(new GridLayout(3, 1));  
    6.         this.setBounds(10, 10, 600, 400);  
    7.         //引入jiaPopMenu()方法  
    8.         this.Add();  
    9.         //设置当单机窗口的关闭按钮时退出  
    10.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    11.         this.setVisible(true);  
    12.     }  
    13.     public void Add(){  
    14.         String[] str = new String[10];  
    15.         for (int i = 0; i < 10; i++) {  
    16.             //jcb.addItem("此方法直接将内容添加进列表中" +i);  
    17.             str[i] = "选项" + i;  
    18.         }  
    19.         //或者使用Vector来作为item  
    20.         //Vector v = new Vector();  
    21.         //v.add("选项1");  
    22.         //v.add("选项2");  
    23.         //v.add("选项3");  
    24.         //jcb = new JComboBox(v);  
    25.         jcb = new JComboBox(str);  
    26.         this.add(jcb);  
    27.     }  
    28.     public static void main(String[] args){  
    29.         new test();  
    30.     }  
    31. }  

    9、选项卡

           当今博客盛行的时代,选项卡经常被用到。在博客中,用户经常会改变北京样式,用不同的风格体现个性。而这些完全可以用选项卡来制作。

           使用JTabbedPane类可以把几个组件放在一个组件中,如面板。用户可以通过选择对应于目标组件的选项卡来选择要查看的组件。要创建一个选项卡窗格,只要实例化一个JTabbedPane对象,创建要显示的租金啊,然后再将这些组件添加到选项卡窗格中即可。

           当创建一个要添加到选项卡窗格的组件时,无论当前可见的是哪个子选项卡,每一个子选项都将获得相同的显示空间。只不过当前显示窗格的高度会比其他窗格稍高一点,以进行区分。下面是一个应用实例:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame {  
    2.     //定义选项卡  
    3.     JTabbedPane jtb = newJTabbedPane(JTabbedPane.BOTTOM);  
    4.     //定义选项  
    5.     JPanel jp1 = new JPanel();  
    6.     JPanel jp2 = new JPanel();  
    7.     JPanel jp3 = new JPanel();  
    8.     public test() throws HeadlessException {  
    9.         this.setLayout(new GridLayout(1, 1));  
    10.         this.setBounds(10, 10, 600, 400);  
    11.         //引入jiaPopMenu()方法  
    12.         this.Add();  
    13.         //设置当单机窗口的关闭按钮时退出  
    14.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    15.         this.setVisible(true);  
    16.     }  
    17.     public void Add(){  
    18.         //设置选项事件  
    19.         jp1.setBackground(Color.green);  
    20.         jp2.setBackground(Color.red);  
    21.         jp3.setBackground(Color.BLUE);  
    22.         this.add(jtb);  
    23.         //将选项加入选项卡中  
    24.         jtb.add("绿色背景", jp1);  
    25.         jtb.add("红色背景", jp2);  
    26.         jtb.add("蓝色背景", jp3);  
    27.     }  
    28.     public static void main(String[] args){  
    29.         new test();  
    30.     }  
    31. }  

    10、滑杆

           在应用程序中JSlider支持数值变化。它是一种迅速而简单的方式,不仅能让用户以可视形式获得他们当前选择的反馈,还能得到可以接受的值的范围。

           JSlider类定义了滑杆组件,它的重要方法有:

           >setMaxorTickSpacing():设置主刻度

           >setMinorTickSpacing():设置次刻度

           >setPaintTicks():设置是否绘制刻度

           >setPaintLabels():设置是否绘制标签

           >addChangeListener():刻度变化事件处理

           >getValue():获取当前滑块位置值

           >setValue():设置滑块初始位置

           下面是具体应用实例:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame implements ChangeListener{  
    2.     JSlider js = new JSlider();  
    3.     JLabel jl = new JLabel("20");  
    4.     public test() throws HeadlessException {  
    5.         this.setLayout(new GridLayout(4, 1));  
    6.         this.setBounds(10, 10, 600, 400);  
    7.         //引入jiaPopMenu()方法  
    8.         this.Add();  
    9.         //设置当单机窗口的关闭按钮时退出  
    10.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    11.         this.setVisible(true);  
    12.     }  
    13.     public void Add(){  
    14.         js.setMaximum(100);//最大刻度  
    15.         js.setMinimum(0);//最小刻度  
    16.         js.setOrientation(JSlider.HORIZONTAL);  
    17.         js.setValue(20);//设置滑杆初始位置  
    18.         js.setMajorTickSpacing(20);//设置主刻度  
    19.         js.setMinorTickSpacing(5);//次刻度  
    20.         js.setPaintTicks(true);//绘制刻度  
    21.         js.setPaintLabels(true);//绘制标签  
    22.         this.add(js);  
    23.         this.add(jl);  
    24.         js.addChangeListener(this);  
    25.          
    26.     }  
    27.     public static void main(String[] args){  
    28.         new test();  
    29.     }  
    30.     public void stateChanged(ChangeEvent e) {  
    31.         jl.setText(js.getValue() +"");  
    32.     }  
    33. }  

    11、滚动条

           在Swing中,组件中的内容超出其区域时,就需要使用滚动条。它和网页的滚动条相似。JscrollPane的方法如下:

           >getHorizontalScrollBar():返回水平的JscrollBar组件

           >getVerticalScrollBar():返回垂直的JscrollBar组件

           >get/setHorizontalScrollBarPolicy():Always、Never或As Needed

           >get/setVerticalScrollBarPolicy():与水平函数相同

           下面是对方法的演示:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame {  
    2.     JLabel jl = new JLabel();  
    3.     JScrollPane jsp = new JScrollPane(jl);  
    4.     JScrollBar jsb =jsp.getVerticalScrollBar();  
    5.     public test() throws HeadlessException {  
    6.         this.setLayout(new GridLayout(1, 2));  
    7.         this.setBounds(10, 10, 600, 400);  
    8.         //引入jiaPopMenu()方法  
    9.         this.Add();  
    10.         //设置当单机窗口的关闭按钮时退出  
    11.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    12.         this.setVisible(true);  
    13.     }  
    14.     public void Add(){  
    15.         this.add(jsp);  
    16.         jl.setIcon(new ImageIcon("D:\桌面\桌面\image062_s.jpg"));  
    17.         //监听滚动条的滚动  
    18.         jsb.addAdjustmentListener(newAdjustmentListener() {  
    19.             @Override  
    20.             public voidadjustmentValueChanged(AdjustmentEvent e) {  
    21.                System.out.println(jsb.getModel() + "");  
    22.             }  
    23.         });  
    24.         System.out.println("处理滚动条的四个基本属性的数据模型:minimum、maximum、value 和extent。" + jsb.getModel());  
    25.     }  
    26.     public static void main(String[] args){  
    27.         new test();  
    28.     }  
    29. }  

    12、列表框

           列表框是一个非常有用的组件,也越来越受到重视。尤其是它具有多选能力,在选择选项时可以按住Shift键进行多选。

           JList是一个有用的组件,其类似于一组复选框或一组单选按钮。通过设置,允许对列表框中的项目进行多项选择。JList的方法如下:

           >getSelectedIndex():获取选中的第一个索引

           >getSelectionMode():设置单项选择还是多项选择

           >getListData():设置在列表框中使用数据的模型

           >getSelectedValue():获取选中的第一个值

           列表框经常与滚动条搭配,因为如果没有滚动条,列表框中的内容可能无法完全显示,下面是一个实例:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame {  
    2.     //定义列表框  
    3.     JList jl = new JList(new String[]{"北京" , "天津" , "上海" , "大连" , "青岛" , "武汉" , "西安"});  
    4.     JScrollPane jsp = new JScrollPane(jl);  
    5.     public test() throws HeadlessException {  
    6.         this.setLayout(new GridLayout(4, 2));  
    7.         this.setBounds(10, 10, 600, 400);  
    8.         //引入jiaPopMenu()方法  
    9.         this.Add();  
    10.         //设置当单机窗口的关闭按钮时退出  
    11.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    12.         this.setVisible(true);  
    13.     }  
    14.     public void Add(){  
    15.         this.add(jsp);  
    16.     }  
    17.     public static void main(String[] args){  
    18.         new test();  
    19.     }  
    20. }  

    13、菜单

           JMenu、JMenuItem和JMenuBar组件是在JFrame中开发菜单系统的主要构造块。任何菜单系统的基础都是JMenuBar。它们就像Word中的文件、编辑一样,下面还有新建、复制、粘贴等一系列内容。其方法如下:

           JMenuItem和JMenu:

           >get/setAccelerator():获取/设置快捷键

           >get/setText():获取/设置菜单的文本

           >get/setIcon():获取/设置菜单使用的图片

           JMenu专用:

           >add():将JMenu或JMenuItem添加到JMenuBar或JMenu中。

           JMenu组件使用来存放和整合JMenuItem的组件,也是构成一个菜单不可缺少的组件之一。实现包含JMenuItem的弹出窗口,用户选择JMenuBar上的选项时会显示该JMenuItem。下面是一个关于菜单的应用实例:

    [java] view plain copy
     
     print?
    1. public class test extendsJFrame {  
    2.     //定义菜单条组件  
    3.     JMenuBar jmb = new JMenuBar();  
    4.     //定义3个菜单组件  
    5.     JMenu jm1 = new JMenu("文件");  
    6.     JMenu jm2 = new JMenu("编辑");  
    7.     JMenu jm3 = new JMenu("新建");  
    8.     //定义3个菜单项组件  
    9.     JMenuItem jmi1 = newJMenuItem("word");  
    10.     JMenuItem jmi2 = new JMenuItem("复制");  
    11.     JMenuItem jmi3 = new JMenuItem("粘贴");  
    12.     public test() throws HeadlessException {  
    13.         this.setLayout(new GridLayout(1, 1));  
    14.         this.setBounds(10, 10, 600, 400);  
    15.         //引入jiaPopMenu()方法  
    16.         this.Add();  
    17.         //设置当单机窗口的关闭按钮时退出  
    18.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    19.         this.setVisible(true);  
    20.     }  
    21.     public void Add(){  
    22.         //调整组件间的包含关系  
    23.         jmb.add(jm1);  
    24.         jmb.add(jm2);  
    25.         jm1.add(jm3);  
    26.         jm3.add(jmi1);  
    27.         jm2.add(jmi2);  
    28.         jm2.add(jmi3);  
    29.         this.setJMenuBar(jmb);  
    30.     }  
    31.     public static void main(String[] args){  
    32.         new test();  
    33.     }  
    34. }  

           包含关系为:JmenuBar包含JMenu,JMenu可以包含JMenu和JMenuItem

  • 相关阅读:
    单/多文档的窗体类属性修改(VC_MFC)
    RTTI 与消息机制(VC_MFC)
    对话框属性页(VC_MFC)
    插入符与路径(VC_MFC)
    重画控件与系统托盘图标编写(VC_MFC)
    颜色,文件和字体对话框(VC_MFC)
    ODBC 数据类型和API(VC)
    CTreeCtrl 和 CListCtrl 控件(VC_MFC)
    找回桌面清理向导清理多余IE图标
    win7任务栏缩略图消失的解决方法
  • 原文地址:https://www.cnblogs.com/jiangzhaowei/p/7149408.html
Copyright © 2020-2023  润新知