1、AWT组件开发java
一、AWTdocker
AWT是抽象窗口工具箱的缩写,它为编写图形用户界面提供了用户接口,经过这个接口就能够继承不少方法,省去了不少工做。AWT还能使应用程序更好地同用户进行交互。编程
AWT中的容器是一种特殊的组件,他能够包含其余组件,便可以把组件方法容器中。Container类是用来存放其余组件的Component类的子类,Frame类又是Component的子类。Frame类用于建立具备标题栏和边界的窗口。这里经过继承Frame类来创建本身的界面。浏览器
- public class test extendsFrame{
-
- public test() throws HeadlessException {
- this.setTitle("第一个窗口程序");
-
- this.setBounds(100, 100, 250, 250);
-
- this.setVisible(true);
- }
- public static void main(String[] str) {
- new test();
- }
- }
上面是窗口中一些必不可少的东西,下面是一些窗口的基础应用:网络
>setTitle(“窗口”):定义窗口名称框架
>add(button):把按钮添加到窗口中less
>setBackground(Color):设置窗口的背景颜色jsp
>setResizable(boolean):设置窗口大小是否能够改变ide
>setAlwaysOnTop(boolean):设置窗口是否总在最上面函数
>setBounds(x, y, w, h):设置窗口起始位置和大小
>setVisible(boolean):设置窗口可见
若是想建立多个窗口,只须要在主方法main()中建立多个对象就好了。
二、布局管理器
Java中的图形界面在布局管理上采用容器和布局管理相分离的方案,也就是说容器只是把组件放进来,但它无论怎样放置。至于如何放置须要用到布局管理器。Java中有几种布局管理器,分别是:FlowLayout , BorderLayout, GridLayout和GardLayout。
1)、FlowLayout
FlowLayout布局管理器是默认的布局管理器,它将组件按照从左到右、从上到下的顺序来安排,并在默认状况下使组件尽可能居中放置。下面的代码要添加到test()类中:
this.setLayout(new FlowLayout());
//将按钮组件放入容器中
this.add(new Button("肯定"));
this.add(new Button("取消"));
你们能够经过建立许多按钮来观察FlowLayout对组件的摆放规律,下面是使用一个按钮监听事件来实现按钮的添加:
- public class test extendsFrame implements ActionListener{
- int i;
- Button b = new Button("Add");
- public test() throws HeadlessException {
- this.setTitle("第一个窗口程序");
- this.setLayout(new FlowLayout());
- this.add(b);
- b.addActionListener(this);
- this.setBounds(100, 100, 250, 250);
- this.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e){
- i++;
- Button bi = newButton("Button" + i);
- this.add(bi);
- this.setVisible(true);
- }
- public static void main(String[] str) {
- new test();
- }
- }
在本程序中,因为按钮的大小不一样,其总体看起来不太整齐,但这更说明了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()方法来选择容器中指定的组件。它的第一个参数是管理组件的容器,第二个参数是标识要显示组件字符串,该字符串与将组件添加到容器时使用的字符串相同。其用法以下:
- public class test extendsFrame implements ActionListener{
- Panel p = new Panel();
- Button bf = new Button("First");
- Button bl = new Button("Last");
- Button bn = new Button("next");
- Button bp = newButton("previous");
- Button bg = new Button("Go");
- TextField tf = new TextField();
-
- CardLayout cl = new CardLayout();
- public test() throws HeadlessException {
- this.setTitle("CardLayout布局管理器");
- this.setLayout(null);
- this.add(p);
-
- p.setLayout(cl);
-
- for (int i = 1; i <= 10; i++) {
- Button btemp = newButton("Button" + i);
- p.add(btemp, "" + i);
- }
-
- p.setBounds(10, 40, 100, 100);
- this.add(bf);
- bf.addActionListener(this);
- bf.setBounds(120, 40, 60, 20);
- this.add(bl);
- bl.addActionListener(this);
- bl.setBounds(120, 70, 60, 20);
- this.add(bn);
- bn.addActionListener(this);
- bn.setBounds(120, 100, 60, 20);
- this.add(bp);
- bp.addActionListener(this);
- bp.setBounds(120, 130, 60, 20);
- this.add(bg);
- bg.addActionListener(this);
- bg.setBounds(60, 160, 40, 20);
- this.add(tf);
- tf.setBounds(20, 160, 40, 20);
-
- this.setBounds(200, 200, 210, 220);
- this.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e){
- if (e.getSource() == bn) {
- cl.next(p);
- }
- if (e.getSource() == bp) {
- cl.previous(p);
- }
- if (e.getSource() == bf) {
- cl.first(p);
- }
- if (e.getSource() == bl) {
- cl.last(p);
- }
- if (e.getSource() == bg) {
- cl.show(p, tf.getText().trim());
- tf.setText("");
- }
- }
- public static void main(String[] args){
- new test();
- }
三、组件和监听接口
组件和监听接口是分不开的。组件和监听接口在AWT中有不少,咱们只对复杂的、难以理解的进行讲解,其余的你们能够经过API自行学习。
1)、按钮和ActionListener
ActionListener是由处理ActionEvent事件的监听器对象实现的。当单击按钮、在文本区域中按回车键、选择菜单项、双击列表项都会触发监听器。该接口中的方法为:
public voidactoinPerformed(ActionEvent e)
下面是一个实现ActionListener接口的实例:
- public class mytest01extends Frame implements ActionListener{
- Button b1 = new Button("进入社区");
- Button b2 = new Button("退出");
- public mytest01() throws HeadlessException{
- this.setTitle("论坛");
- this.add(b1);
- this.add(b2 , BorderLayout.SOUTH);
-
- b1.addActionListener(this);
- b2.addActionListener(this);
- this.setBounds(100, 100, 200, 300);
- this.setVisible(true);
- }
-
- public void actionPerformed(ActionEvent e){
- if (e.getSource() == b1) {
- System.out.println("进入社区");
- }
- else{
- System.out.println("退出");
- }
- }
- public static void main(String[] args){
- new mytest01();
- }
- }
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)
这些接口很容易可是有点繁琐,下面的实例程序很是清楚:
- public class mytest01extends Frame implements WindowListener{
- public mytest01() throws HeadlessException{
- this.setTitle("WindowListener");
- this.addWindowListener(this);
- this.setBounds(100, 100, 200, 300);
- this.setVisible(true);
- }
-
- public void windowOpened(WindowEvent e) {
- System.out.println("打开");
- }
- public void windowClosing(WindowEvent e) {
- System.out.println("菜单关闭");
- this.dispose();
- }
- public void windowClosed(WindowEvent e) {
- System.out.println("释放");
- }
- public void windowIconified(WindowEvent e){
- System.out.println("最小化");
- }
- public void windowDeiconified(WindowEvente) {
- System.out.println("最大化");
- }
- public void windowActivated(WindowEvent e){
- System.out.println("激活");
- }
- public void windowDeactivated(WindowEvente) {
- System.out.println("失去焦点");
- }
- public static void main(String[] args){
- new mytest01();
- }
- }
3)、文本组件和TextListener
文本组件就像把窗口空白处当作记事本同样,它是一个用来写内容的组件。TextListener用来肯定什么时候文本值改变。该接口还能够用到不少地方,其接口方法以下:
public voidtextValueChanged(TextEvent e)
下面经过实例来了解TextListener监听事件的使用
- public class mytest01extends Frame implements TextListener{
- TextArea ta = newTextArea("saasdfgadsfg");
- public mytest01() throws HeadlessException{
- this.setTitle("textArea");
- this.add(ta);
-
- Font f = new Font("宋体", Font.ITALIC+ Font.BOLD, 50);
- ta.setFont(f);
- ta.addTextListener(this);
- ta.setForeground(Color.red);
- this.setBounds(100, 100, 300, 300);
- this.setVisible(true);
- }
- @Override
- public void textValueChanged(TextEvent e) {
- System.out.println(ta.getText());
- }
- public static void main(String[] args){
- new mytest01();
- }
- }
上面的这段程序使用TextListener监听文本的输入、删除等操做,就像记事本同样能够对文本进行修改、录入。
2、Swing界面编程
随着Java的发展,AWT已经渐渐被淘汰,它已经不能适应发展的须要,不能知足开发功能强大的用户界面的须要。这时Swing出现了,它是创建在AWT之上的组件集,在不一样的平台上都能保持组件的界面样式,所以获得了很是普遍的应用。
一、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组件的行为和外观,访问技术等。
二、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类的基本程序:
- public class test extendsJFrame{
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(2, 2));
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public static void main(String[] args){
- new test();
- }
- }
三、经过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的实例应用:
- public class test extendsJFrame{
- JLabel j1 = new JLabel("a");
- JLabel j2 = new JLabel("b");
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(2, 2));
- this.add(j1);
- this.add(j2);
-
- ImageIcon i1 = new ImageIcon("D:\\桌面\\桌面\\安卓开发工具\\素材\\图标\\a1.png");
- j1.setIcon(i1);
-
- Icon i2 = new MyIconlmp();
- j2.setIcon(i2);
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
-
- class MyIconlmp implements Icon{
- public void paintIcon(Component c,Graphics g, int x, int y) {
- for (int i = 0; i < 3; i++) {
- g.setColor(new Color(30*i,50*i, 60*i));
- g.fillOval(10, 10 + 100*i, 120,80);
- }
- }
-
- public int getIconWidth() {
- return 200;
- }
-
- public int getIconHeight() {
- return 200;
- }
- }
- public static void main(String[] args){
- new test();
- }
- }
四、按钮
JButton类用来定义按钮。JButton类的经常使用方法以下:
>addActionListener():注册点击事件监听器;
>setText():设置按钮文字;
>setIcon():设置按钮图标。
经过组建的setMnemonic()方法能够设置组件Mnemonic助记符。经过组件的setTipText能够设置组建的ToolTip提示信息。
在Swing中,JButton是有AbstractButton类派生的。AbstractButton类派生了两个组件:JButton和JtoggleButton组件。JButton是Swing的按钮,而ToggleButton组件是单选按钮和复选框的基类。下面是一个按钮的应用程序。
- public class test extendsJFrame implements ActionListener{
- JButton jb = new JButton("Clickme!");
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(2, 2));
- this.add(jb);
- jb.addActionListener(this);
- jb.setMnemonic('b');
- jb.setToolTipText("I am abutton");
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public static void main(String[] args){
- new test();
- }
- @Override
- public void actionPerformed(ActionEvent e){
- this.jb.setText("Clicked");
- }
- }
五、复选框
在Swing中,用JcheckBox类来定义复选框。复选框和单选按钮有点相似,可是一组复选框中能够有任何数量的复选框被选中。复选框能够为每一次的单击操做添加一个事件。但日常只会监听事件,由于它们让客户来肯定该单击操做时选中仍是取消选中复选框。
复选框又被称为检测盒。JcheckBox提供选中/未选中两种状态,当用户单击复选框时改变复选框原来设置的状态。
六、弹出式菜单
JPopupMenu是一种Menu的组件,所以在使用JPopupMenu时都须要一个Container来放置JPopupMenu。
经过JpopupMenu类能够定义弹出式菜单,其重要方法有:
>add(JmenuItem e):(往菜单中增长菜单项)
>show():(显示菜单)
经过JMenuItem类来定义菜单项,经过addActionListener()为菜单项增长事件处理。
JpopupMenu是一个可弹出并显示一系列选项的小窗口,可用于用户在菜单栏上选择选项时显示菜单,还能够用于当用户选择菜单项并激活时显示“右拉式(pull-right)“菜单。经过下面的程序进一步了解相关的知识:
- public class test extendsJFrame {
-
- JPopupMenu jpm = new JPopupMenu();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(2, 2));
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.jiaPopMenu();
- }
- public void jiaPopMenu(){
- JMenuItem item = newJMenuItem("A");
-
- item.addActionListener(newActionListener() {
- public voidactionPerformed(ActionEvent e) {
- System.out.println("AClicked!!!");
- }
- });
- jpm.add(item);
-
- item = new JMenuItem("B");
- item.addActionListener(newActionListener() {
- public voidactionPerformed(ActionEvent e) {
- System.out.println("BClicked");
- }
- });
- jpm.add(item);
-
- this.addMouseListener(newMouseAdapter() {
- public voidmouseReleased(MouseEvent e) {
- if (e.isPopupTrigger()) {
- jpm.show(e.getComponent(),e.getX(), e.getY());
- }
- }
- });
- }
- public static void main(String[] args){
- new test();
- }
- }
使用JPopupMenu组件实现右键快捷菜单功能,并使用ActionListener的对象来将菜单激活,当右击时便可弹出菜单。
七、单选按钮
单选按钮的实质就是在一组按钮中一次只能有一个按钮被选中。单选按钮的外观相似复选框,可是复选框没有对能够选择的选项数目进行限制。对于每一组的单选按钮,必须建立一个ButtonGroup对象实例而且将每个单选按钮添加到该ButtonGroup对象中。下面的实例讲解的单选按钮的基本用法:
- public class test extendsJFrame {
-
- JRadioButton r1 = newJRadioButton("No.1");
- JRadioButton r2 = newJRadioButton("No.2");
-
- ButtonGroup bg = new ButtonGroup();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(3, 1));
- this.setBounds(10, 10, 600, 400);
- this.setVisible(true);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.Add();
- }
- public void Add(){
-
- bg.add(r1);
- bg.add(r2);
-
- this.add(r1);
- this.add(r2);
-
- r2.setSelected(true);
- }
- public static void main(String[] args){
- new test();
- }
- }
八、下拉列表框
下拉列表框可让人感受到整个界面很简洁,在大的网页中都能感受到这一点。列表能够有许多选项,因此它们一般被放置在一个滚动窗格中。组合框与下拉列表框类似,区别在于使用组合框时用户能够不从列表中选择项目,还能够选择一个项目。在某些版本的组合框中,还能够输入本身的选择,如浏览器的地址栏。
JComboBox方法不少,它们主要用来管理列表中的数据:
>addItem():添加一个项目到JComboBox
>get/setSelectedIndex():获取/设置JComboBox中选中项目的索引
>get/setSelectedItem():获取/设置选中的对象
>removeAllItems():从JComboBox删除全部对象
>remoteItem():从JComboBox删除特定对象
下面是一个下拉别表框的实例,从中能够更详细的了解到下拉列表框的使用:
- public class test extendsJFrame {
-
- JComboBox jcb = new JComboBox();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(3, 1));
- this.setBounds(10, 10, 600, 400);
-
- this.Add();
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- String[] str = new String[10];
- for (int i = 0; i < 10; i++) {
-
- str[i] = "选项" + i;
- }
-
-
-
-
-
-
- jcb = new JComboBox(str);
- this.add(jcb);
- }
- public static void main(String[] args){
- new test();
- }
- }
九、选项卡
当今博客盛行的时代,选项卡常常被用到。在博客中,用户常常会改变北京样式,用不一样的风格体现个性。而这些彻底能够用选项卡来制做。
使用JTabbedPane类能够把几个组件放在一个组件中,如面板。用户能够经过选择对应于目标组件的选项卡来选择要查看的组件。要建立一个选项卡窗格,只要实例化一个JTabbedPane对象,建立要显示的租金啊,而后再将这些组件添加到选项卡窗格中便可。
当建立一个要添加到选项卡窗格的组件时,不管当前可见的是哪一个子选项卡,每个子选项都将得到相同的显示空间。只不过当前显示窗格的高度会比其余窗格稍高一点,以进行区分。下面是一个应用实例:
- public class test extendsJFrame {
-
- JTabbedPane jtb = newJTabbedPane(JTabbedPane.BOTTOM);
-
- JPanel jp1 = new JPanel();
- JPanel jp2 = new JPanel();
- JPanel jp3 = new JPanel();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(1, 1));
- this.setBounds(10, 10, 600, 400);
-
- this.Add();
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
-
- jp1.setBackground(Color.green);
- jp2.setBackground(Color.red);
- jp3.setBackground(Color.BLUE);
- this.add(jtb);
-
- jtb.add("绿色背景", jp1);
- jtb.add("红色背景", jp2);
- jtb.add("蓝色背景", jp3);
- }
- public static void main(String[] args){
- new test();
- }
- }
十、滑杆
在应用程序中JSlider支持数值变化。它是一种迅速而简单的方式,不只能让用户以可视形式得到他们当前选择的反馈,还能获得能够接受的值的范围。
JSlider类定义了滑杆组件,它的重要方法有:
>setMaxorTickSpacing():设置主刻度
>setMinorTickSpacing():设置次刻度
>setPaintTicks():设置是否绘制刻度
>setPaintLabels():设置是否绘制标签
>addChangeListener():刻度变化事件处理
>getValue():获取当前滑块位置值
>setValue():设置滑块初始位置
下面是具体应用实例:
- public class test extendsJFrame implements ChangeListener{
- JSlider js = new JSlider();
- JLabel jl = new JLabel("20");
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(4, 1));
- this.setBounds(10, 10, 600, 400);
-
- this.Add();
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- js.setMaximum(100);
- js.setMinimum(0);
- js.setOrientation(JSlider.HORIZONTAL);
- js.setValue(20);
- js.setMajorTickSpacing(20);
- js.setMinorTickSpacing(5);
- js.setPaintTicks(true);
- js.setPaintLabels(true);
- this.add(js);
- this.add(jl);
- js.addChangeListener(this);
-
- }
- public static void main(String[] args){
- new test();
- }
- public void stateChanged(ChangeEvent e) {
- jl.setText(js.getValue() +"");
- }
- }
十一、滚动条
在Swing中,组件中的内容超出其区域时,就须要使用滚动条。它和网页的滚动条类似。JscrollPane的方法以下:
>getHorizontalScrollBar():返回水平的JscrollBar组件
>getVerticalScrollBar():返回垂直的JscrollBar组件
>get/setHorizontalScrollBarPolicy():Always、Never或As Needed
>get/setVerticalScrollBarPolicy():与水平函数相同
下面是对方法的演示:
- public class test extendsJFrame {
- JLabel jl = new JLabel();
- JScrollPane jsp = new JScrollPane(jl);
- JScrollBar jsb =jsp.getVerticalScrollBar();
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(1, 2));
- this.setBounds(10, 10, 600, 400);
-
- this.Add();
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- this.add(jsp);
- jl.setIcon(new ImageIcon("D:\\桌面\\桌面\\image062_s.jpg"));
-
- jsb.addAdjustmentListener(newAdjustmentListener() {
- @Override
- public voidadjustmentValueChanged(AdjustmentEvent e) {
- System.out.println(jsb.getModel() + "");
- }
- });
- System.out.println("处理滚动条的四个基本属性的数据模型:minimum、maximum、value 和extent。" + jsb.getModel());
- }
- public static void main(String[] args){
- new test();
- }
- }
十二、列表框
列表框是一个很是有用的组件,也愈来愈受到重视。尤为是它具备多选能力,在选择选项时能够按住Shift键进行多选。
JList是一个有用的组件,其相似于一组复选框或一组单选按钮。经过设置,容许对列表框中的项目进行多项选择。JList的方法以下:
>getSelectedIndex():获取选中的第一个索引
>getSelectionMode():设置单项选择仍是多项选择
>getListData():设置在列表框中使用数据的模型
>getSelectedValue():获取选中的第一个值
列表框常常与滚动条搭配,由于若是没有滚动条,列表框中的内容可能没法彻底显示,下面是一个实例:
- public class test extendsJFrame {
-
- JList jl = new JList(new String[]{"北京" , "天津" , "上海" , "大连" , "青岛" , "武汉" , "西安"});
- JScrollPane jsp = new JScrollPane(jl);
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(4, 2));
- this.setBounds(10, 10, 600, 400);
-
- this.Add();
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
- this.add(jsp);
- }
- public static void main(String[] args){
- new test();
- }
- }
1三、菜单
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。下面是一个关于菜单的应用实例:
- public class test extendsJFrame {
-
- JMenuBar jmb = new JMenuBar();
-
- JMenu jm1 = new JMenu("文件");
- JMenu jm2 = new JMenu("编辑");
- JMenu jm3 = new JMenu("新建");
-
- JMenuItem jmi1 = newJMenuItem("word");
- JMenuItem jmi2 = new JMenuItem("复制");
- JMenuItem jmi3 = new JMenuItem("粘贴");
- public test() throws HeadlessException {
- this.setLayout(new GridLayout(1, 1));
- this.setBounds(10, 10, 600, 400);
-
- this.Add();
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void Add(){
-
- jmb.add(jm1);
- jmb.add(jm2);
- jm1.add(jm3);
- jm3.add(jmi1);
- jm2.add(jmi2);
- jm2.add(jmi3);
- this.setJMenuBar(jmb);
- }
- public static void main(String[] args){
- new test();
- }
- }
包含关系为:JmenuBar包含JMenu,JMenu能够包含JMenu和JMenuItem