Chinaunix首页 | 论坛 | 博客
  • 博客访问: 362196
  • 博文数量: 100
  • 博客积分: 2586
  • 博客等级: 少校
  • 技术积分: 829
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-09 15:20
个人简介

我是一个Java爱好者

文章分类

全部博文(100)

文章存档

2014年(2)

2013年(7)

2012年(2)

2010年(44)

2009年(28)

2008年(17)

我的朋友

分类: Java

2009-07-20 15:46:55

Checkbox

                                                                                                         

     Checkbox类用来建立单选按钮和多选按钮(也叫复选框)。

     创建多选按钮,只需要使用构造函数:

( label, boolean state)

     创建单选按钮,需要使用构造函数:

( label, boolean state,  group)

     单选按钮和多选按钮的语义事件为ItemEvent,对应的监听器接口为ItemListener,该接口中只有一个itemStateChanged方法

     编程实例:创建一个多选按钮和两个属于同一组的单选按钮,并对每个按钮的选中情况进行处理

package checkbox;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;;

publicclass TestCheckbox extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

   

    Checkbox checkbox=new Checkbox("上海市",true);

    JCheckBox jcheckbox1=new JCheckBox("北京市");

    JCheckBox jcheckbox2=new JCheckBox("南京市",true);

    Image img=this.getToolkit().getImage("..""ico.jpg");

    ImageIcon imgicon=new ImageIcon(img);

       //Icon icon=imgicon;

    CheckboxGroup cbg=new CheckboxGroup();

    Checkbox checkbox1=new Checkbox("18",true,cbg);

    Checkbox checkbox2=new Checkbox("20",false,cbg);

   

    Choice cho=new Choice();

    class choiceItemListener implements ItemListener

    {

       publicvoid itemStateChanged(ItemEvent e)

       {

           if(e.getItem().equals("男孩"))

           {

              System.out.println("我是男孩");

           }

           else

           {

              System.out.println("我是女孩");

           }

       }

    }

    class checkboxItemListener implements ItemListener

    {

       publicvoid itemStateChanged(ItemEvent e)

       {

           Checkbox cb=new Checkbox();

           cb=(Checkbox)e.getItemSelectable();

           /*if(cb.getLabel().equals("18"))

           {

              System.out.println("18岁啦");

           }

           else

           {

              System.out.println("20岁啦");

           }

           if(cb.getState()==true)

           {

              System.out.println(cb.getLabel());

           }*/

           if(cb==checkbox1)

           {

              System.out.println("18岁啦");

           }

           else

           {

              System.out.println("20岁啦");

           }

          

          

       }

    }

   

    public TestCheckbox()

    {

       //用于产生流式部局管理器

       FlowLayout fl=new FlowLayout();

       this.setLayout(fl);

       cho.add("男孩");

       cho.add("女孩");

       this.add(cho);

       this.add(checkbox);

       this.add(jcheckbox1);

       this.add(jcheckbox2);

       this.add(checkbox1);

       this.add(checkbox2);

       cho.addItemListener(new choiceItemListener());

       checkboxItemListener cbi=new checkboxItemListener();

       checkbox1.addItemListener(cbi);

       checkbox2.addItemListener(cbi);

       addWindowListener(new WindowAdapter()

       {

           publicvoid windowClosing(WindowEvent e)

           {

              e.getWindow().dispose();

           }

       });

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestCheckbox mainFrame=new TestCheckbox();

       mainFrame.setSize(400,400);

       mainFrame.setTitle("TestCheckbox");

       mainFrame.setVisible(true);

    }

}

PanelScrollPane

                                                                                                         

     Panel类是一个容器类,用于产生一种特殊的空白面板,可以容纳其他的组件,但不能独立存在

Panel的两个构造函数:

public Panel()

使用默认的布局管理器创建新面板。所有面板的默认布局管理器都是 FlowLayout

public Panel(LayoutManager layout)

创建具有指定布局管理器的新面板。

参数:

layout - 此面板的布局管理器。

     ScrollPane类是一种容器类,用于产生滚动窗口,通过滚动条在一个较小的窗口中显示较大的子部件。在ScrollPane容器内只能增加一个组件。

 ScrollPanel的两个构造函数:
   public ScrollPane()
           throws HeadlessException

创建一个具有滚动条策略 "as needed" 的新滚动窗格容器。

   public ScrollPane(int scrollbarDisplayPolicy)
           throws HeadlessException

创建新的滚动窗格容器

滚动条的显示策略可以设置如下:

  1. as needed: 创建滚动条,且只在滚动窗格需要时显示
  2. always: 创建滚动条,且滚动窗格总是显示滚动条
  3. never: 滚动窗格永远不创建或显示滚动条

编程举例:如何使用ScrollPane 如图:

package TestArea;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass TestTextArea extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

    public TestTextArea()

    {

       //关闭窗体

       this.setDefaultCloseOperation(EXIT_ON_CLOSE);

       JTextArea jta=new JTextArea(30,20);

       ScrollPane sp=new ScrollPane();

       sp.add(jta);

       //把容器添加到窗体

       //this.add(sp);

       this.getContentPane().add(sp);

      

       addWindowListener(new WindowAdapter()

       {

           publicvoid windowClosing(WindowEvent e)

           {

              //e.getWindow().dispose();

           }

       });

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestTextArea mainFrame =new TestTextArea();

       mainFrame.setTitle("TestTextArea");

       mainFrame.setSize(200,200);

       mainFrame.setVisible(true);

    }

}

布局管理器

                                                                                                         

     一个容器中的各个组件之间的位置和大小关系就称之为布局

     Java语言提供了布局管理器来管理组件在容器中的布局,而不是直接使用位置坐标来设置各个组件的位置和大小

     AWT中的布局管理器类:

     BorderLayout

     FlowLayout

     GridLayout

     CardLayout

     GridBagLayout

BorderLayout编程实例

                                                                                                         

如图:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclassTestBorderLayout extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

    public TestTextArea()

    {

       //关闭窗体

       this.setDefaultCloseOperation(EXIT_ON_CLOSE);

       this.add(new JButton("North"), "North");

        this.add(new JButton("South"), "South");

       this.add(new JButton("East"), "East");

       this.add(new JButton("West"), "West");

       this.add(new JButton("Center"), "Center");

       addWindowListener(new WindowAdapter()

       {

           publicvoid windowClosing(WindowEvent e)

           {

              //e.getWindow().dispose();

           }

       });

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestBorderLayout mainFrame =newTestBorderLayout ();

       mainFrame.setTitle("TestBorderLayout");

       mainFrame.setSize(300,200);

       mainFrame.setVisible(true);

    }

}

FlowLayout编程实例

                                                                                                         

package TestArea;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass TestTextArea extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

    public TestTextArea()

    {

       //关闭窗体

       this.setDefaultCloseOperation(EXIT_ON_CLOSE);

       this.setLayout(new FlowLayout());

       this.add(new JButton("East"));

       this.add(new JButton("South"));

       this.add(new JButton("West"));

       this.add(new JButton("North"));

       this.add(new JButton("Center"), "Center");

       addWindowListener(new WindowAdapter()

       {

           publicvoid windowClosing(WindowEvent e)

           {

              //e.getWindow().dispose();

           }

       });

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestFlowLayout mainFrame =newTestFlowLayout ();

       mainFrame.setTitle("TestFlowLayout");

       mainFrame.setSize(300,200);

       mainFrame.setVisible(true);

    }

}

GridLayout编程实例

                                                                                                         

     GridLayout布局管理器将容器划分成若干行列的网格,在容器上添加组件时它们会按从左到右、从上到下的顺序在风格中排列

     GridLayout的构造方法中,需要指定在容器上划分的网格的行、列数

CardLayout

                                                                                                         

     CardLayout布局管理器能够实现将多个组件放在同一容器区域内的交替显示,相当于多张卡片摞在一起,在任何时候都只有最上面的一个可见

编程实例

创建两个Panel对象,每个Panel上都能拥有一个布局管理器,左边的Panel使用GridLayout布局器放置3个按钮,右边的Panel上使用CardLayout布局管理器来放置卡片,最后在窗口上使用BorderLayout放置这两个Panel面板。右边的Panel中带有5张卡片(用5个按钮模拟),按下左边的Panel中的prev按钮,依次向前显示,按下next按钮,依次向后显示,按下three按钮,显示第三张卡片

package cardlayout;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass TestImage extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

    JPanel plCard=new JPanel();

    CardLayout cl=new CardLayout();

    public TestImage()

    {

       this.setLayout(new BorderLayout());

       this.setDefaultCloseOperation(EXIT_ON_CLOSE);

       JPanel pl=new JPanel();

       pl.setLayout(new GridLayout(2,1));

       JButton btPrev=new JButton(/*"前一个"*/);

       JButton btNext=new JButton(/*"下一个"*/);

       ImageIcon icon1=new ImageIcon("image""back.gif");

       ImageIcon icon2=new ImageIcon("image""next.gif");

       btPrev.setIcon(icon1);

       btNext.setIcon(icon2);

       pl.add(btPrev);

       pl.add(btNext);

       this.add(pl,"West");

      

       plCard.setLayout(cl);

       ImageIcon icon3=new ImageIcon("image""001.gif");

       ImageIcon icon4=new ImageIcon("image""002.gif");

       ImageIcon icon5=new ImageIcon("image""003.gif");

       ImageIcon icon6=new ImageIcon("image""004.gif");

       ImageIcon icon7=new ImageIcon("image""005.gif");

       ImageIcon icon8=new ImageIcon("image""006.gif");

       JLabel lb1=new JLabel();

       JLabel lb2=new JLabel();

       JLabel lb3=new JLabel();

       JLabel lb4=new JLabel();

       JLabel lb5=new JLabel();

       JLabel lb6=new JLabel();

       lb1.setIcon(icon3);

       lb2.setIcon(icon4);

       lb3.setIcon(icon5);

       lb4.setIcon(icon6);

       lb5.setIcon(icon7);

       lb6.setIcon(icon8);

       plCard.add(lb1,"1");

       plCard.add(lb2,"2");

       plCard.add(lb3,"3");

       plCard.add(lb4,"4");

       plCard.add(lb5,"5");

       plCard.add(lb6,"6");

       this.add(plCard);

       MyActionListener my=new MyActionListener();

       btPrev.addActionListener(my);

       btNext.addActionListener(my);

    }

    class MyActionListener implements ActionListener

    {

       publicvoid actionPerformed(ActionEvent e)

       {

           //if(e.getActionCommand().equals("前一个"))

           if(e.getID()==e.COMPONENT_EVENT_MASK)

           {

              cl.previous(plCard);

           }

           else

           {

              cl.next(plCard);

           }

       }

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestImage mainFrame=new TestImage();

       mainFrame.setTitle("显示图片");

       mainFrame.setBounds(100,100,400,300);

       mainFrame.setVisible(true);

    }

}

package cardlayout;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestCardLayout extends JFrame

{

       private static final long serialVersionUID = 1L;

       CardLayout cl=new CardLayout();

       Panel plCard=new Panel();

       public TestCardLayout()

       {

              this.setDefaultCloseOperation(EXIT_ON_CLOSE);

              Panel pl=new Panel();

              GridLayout gl=new GridLayout(5,1);

              pl.setLayout(gl);

              JButton btFirst=new JButton("First");

              JButton btPrev=new JButton("Prev");

              JButton btNext=new JButton("Next");

              JButton btThree=new JButton("Three");

              JButton btLast=new JButton("Last");

              pl.add(btFirst);

              pl.add(btPrev);

              pl.add(btNext);

              pl.add(btThree);

              pl.add(btLast);

              this.add(pl,"West");

              plCard.setLayout(cl);

              //images文件夹和bin目录在同一目录下

              ImageIcon icon1=new ImageIcon("images""gcz01.jpg");

              ImageIcon icon2=new ImageIcon("images""gcz02.jpg");

              ImageIcon icon3=new ImageIcon("images""gcz03.jpg");

              ImageIcon icon4=new ImageIcon("images""gcz04.jpg");

              ImageIcon icon5=new ImageIcon("images""gcz05.jpg");

              JButton bt1=new JButton("One",icon1);

              JButton bt2=new JButton("Two",icon2);

              JButton bt3=new JButton("Three",icon3);

              JButton bt4=new JButton(icon4);

              JButton bt5=new JButton();

              bt5.setIcon(icon5);

              plCard.add(bt1,"1");

              plCard.add(bt2,"2");

              plCard.add(bt3,"3");

              plCard.add(bt4,"4");

              plCard.add(bt5,"5");

              this.add(plCard);

              MyActionListener ml=new MyActionListener();

              btPrev.addActionListener(ml);

              btNext.addActionListener(ml);

              btThree.addActionListener(ml);

              btFirst.addActionListener(ml);

              btLast.addActionListener(ml);

       }

       class MyActionListener implements ActionListener

       {

              public void actionPerformed(ActionEvent e)

              {

                     if(e.getActionCommand().equals("Prev"))

                     {

                            cl.previous(plCard);

                     }

                     else if(e.getActionCommand().equals("First"))

                     {

                            cl.first(plCard);

                     }

                     else if(e.getActionCommand().equals("Last"))

                     {

                            cl.last(plCard);

                     }

                     else if(e.getActionCommand().equals("Next"))

                     {

                            cl.next(plCard);

                     }

                     else

                     {

                            cl.show(plCard,"3");

                     }

              }

       }

       public static void main(String[] args)

       {

              // TODO Auto-generated method stub

              TestCardLayout mainFrame=new TestCardLayout();

              mainFrame.setBounds(100,100,400,300);

              mainFrame.setTitle("TestCardLayout");

              mainFrame.setVisible(true);

       }

}

取消布局管理器

                                                                                                         

     调用Container.setLayout(null)方法取消布局管理器设置,在这种情况下,可以调用Component.setBounds方法来用绝对坐标设置容器上的每个组件的大小和位置。

     不使用布局管理器将会给程序带来一个潜在的问题,当容器大小改变时,所有组件仍保持原来的位置和大小,交导致整个程序界面比较难看。

SwingJFC

                                                                                                         

     所有的Swing组件,位于java.swing包中,它们是构筑在AWT上层的GUI组件,Swing组件是JComponent类的子类,JComponent又是java.awt.Containt的子类

     为保证可移植性,Swing完全用Java语言编写

     Swing提供了比AWT更多的组件库,例如,JtableJtree,JcomboBox

     Swing也增加了AWT中原有组件的功能,例如,与AWT中的Button对应的Swing组件是Jbutton

     JFCjava Foundation Class)是指Sun对早期的JDK进行扩展的部分,集合了Swing组件和其他能简化开发的API类,包括Swingjava2D,accessibility,internationalization

编程实例:从AWTSwing

JFrame

                                                                                                         

     JFrame是与AWT中的Frame相对应的Swing组件

     JFrame上面只能有一个唯一的组件,这个组件为JRootPane,调用JFrame.getContentPane()方法可获得JFrame中内置的JrootPane对象。

     应用程序不能直接在JFrame实例对象上增加组件和设置布局管理器,而应该在JRootPane对象上增加子组件和设置布局管理器。

     调用Jframer setDefaultCloseOperation方法,可以设置单击窗口上的关闭按钮时的事件处理方式,例如:当设置值为JFrame.EXIT_ON_CLOSE时,单击JFrame窗口上的关闭按钮,将直接关闭JFrame框架窗口并结束程序运行。

编程实例:使用JFrame来创建程序的主框架窗口

import java.awt.GridLayout;

import javax.swing.*;

publicclass TestJFrame extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

    public TestJFrame()

    {

       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JButton bt=new JButton("OK");

       JButton bt1=new JButton("EXIT");

       //this.getContentPane().add(bt);

       JPanel pl=new JPanel();

       pl.setLayout(new GridLayout(2,1));

       pl.add(bt);

       pl.add(bt1);

       this.getContentPane().add(pl);

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestJFrame mainFrame=new TestJFrame();

       mainFrame.setSize(300,200);

       mainFrame.setVisible(true);

    }

}

JScrollPane

                                                                                                         

     JScrollPane是与AWT中的ScrollPane相对应的Swing组件

     最基本的JScrollPane由水平和垂直方向上的JScrollBar、以及一个JViewport组件

     调用JScrollPane.getViewport方法,可以获得代表滚动窗口中的视图区域的JViewport对象。

     调用JViewport.setViesport方法,可以将滚动窗口中要显示的内容作为子组件增加到JViewport上。

     编程实例:使用JScrollPane创建滚动窗口

import javax.swing.*;

publicclass TestJFrame extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

    public TestJFrame()

    {

       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JScrollPane jsp=new JScrollPane();

       JTextArea jta=new JTextArea(20,30);

       jsp.getViewport().add(jta);

       this.getContentPane().add(jsp);

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestJFrame mainFrame=new TestJFrame();

       mainFrame.setSize(300,200);

       mainFrame.setVisible(true);

    }

}

JScrollPane

                                                                                                         

     JOptionPane类提供了若干了showXxxDialog静态方法,可用来产生简单的标准对话框

方法名

描述

showConfirmDialog

询问一个确认问题,如 yes/no/cancel。

showInputDialog

提示要求某些输入。

showMessageDialog

告知用户某事已发生。

showOptionDialog

上述三项的大统一 (Grand Unification)。

public static int showConfirmDialog( parentComponent,
                                     message,
                                     title,
                                    int optionType,
                                    int messageType)
                             throws 

调用一个由 optionType参数确定其中选项数的对话框,messageType参数确定要显示的图标。messageType参数主要用于提供来自外观的默认图标。

参数:

parentComponent - 确定在其中显示对话框的 Frame;如果为 null或者 parentComponent不具有 Frame,则使用默认的 Frame

message - 要显示的 Object

title - 对话框的标题字符串

optionType - 指定可用于对话框的选项的整数:YES_NO_OPTIONYES_NO_CANCEL_OPTION OK_CANCEL_OPTION

messageType - 指定此消息种类的整数;主要用于确定来自可插入外观的图标:ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGE PLAIN_MESSAGE

返回:

指示用户所选选项的整数

示例:

显示一个错误对话框,该对话框显示的 message 为 'alert':

JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);

显示一个内部信息对话框,其 message 为 'information':

JOptionPane.showInternalMessageDialog(frame, "information",

"information", JOptionPane.INFORMATION_MESSAGE);

显示一个信息面板,其 options 为 "yes/no",message 为 'choose one':

JOptionPane.showConfirmDialog(null,

"choose one", "choose one", JOptionPane.YES_NO_OPTION);

显示一个内部信息对话框,其 options 为 "yes/no/cancel",message 为 'please choose one',并具有 title 信息:

JOptionPane.showInternalConfirmDialog(frame,

"please choose one", "information",

JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

显示一个警告对话框,其 options 为 OK、CANCEL,title 为 'Warning',message 为 'Click OK to continue':

Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",

JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,

null, options, options[0]);

显示一个要求用户键入 String 的对话框:

String inputValue = JOptionPane.showInputDialog("Please input a value");

显示一个要求用户选择 String 的对话框:

Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,

"Choose one", "Input",

JOptionPane.INFORMATION_MESSAGE, null,

possibleValues, possibleValues[0]);

编程举例:使用JOptionPane类,在程序开始运行时,弹出一个对话框提示用户,在主框架窗口的关闭按钮被单击时,弹出一个对话框询问用户是否真的要结束程序的运行。

import java.awt.event.*;

import javax.swing.*;

publicclass TestJFrame extends JFrame

{

    privatestaticfinallongserialVersionUID = 1L;

    public TestJFrame()

    {

this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

       ImageIcon iconMessage=new ImageIcon("images""message.gif");

       JOptionPane.showMessageDialog(this,"程序已运行...","Running",

              JOptionPane.INFORMATION_MESSAGE,iconMessage);

       JScrollPane jsp=new JScrollPane();

       JTextArea jta=new JTextArea(20,30);

       jsp.getViewport().add(jta);

       this.getContentPane().add(jsp);

       addWindowListener(new WindowAdapter()

       {

           publicvoid windowClosing(WindowEvent e)

           {

              ImageIcon iconExit=new ImageIcon("images""exit.jpg");

              if(JOptionPane.showConfirmDialog(TestJFrame.this, "确认要退出吗?",

                     "退出", JOptionPane.OK_CANCEL_OPTION,

                     JOptionPane.QUESTION_MESSAGE,iconExit)==JOptionPane.YES_OPTION)

              {

                  e.getWindow().dispose();

              }

           }

       });

    }

    publicstaticvoid main(String[] args)

    {

       // TODO Auto-generated method stub

       TestJFrame mainFrame=new TestJFrame();

       mainFrame.setTitle("TestJOptionPane");

       mainFrame.setSize(300,200);

       mainFrame.setVisible(true);

    }

}

阅读(3110) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~