zoukankan      html  css  js  c++  java
  • 一个ButtonDemo的实现过程。

    来自JDK API 1.6.0:

     

    Try this: 

    1. Click the Launch button to run the Button Demo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.Launches the ButtonDemo example

    2. Click the left button. It disables the middle button (and itself, since it is no longer useful) and enables the right button.
    3. Click the right button. It enables the middle button and the left button, and disables itself.

    As the ButtonDemo example shows, a Swing button can display both text and an image. In ButtonDemo, each button has its text in a different place, relative to its image. The underlined letter(下划线字母,提供了一种ALT+大写字母的快捷键方式。一种(mnemonic)助记的方式) in each button's text shows the mnemonic — the keyboard alternative — for each button. In most look and feels, the user can click a button by pressing the Alt key and the mnemonic. For example, Alt-M would click the Middle button in ButtonDemo.

    When a button is disabled, the look and feel automatically generates the button's disabled appearance. However, you could provide an image to be substituted for the normal image. For example, you could provide gray versions of the images used in the left and right buttons.

    How you implement event handling depends on the type of button you use and how you use it. Generally, you implement an action listener, which is notified every time the user clicks the button. For check boxes you usually use an item listener, which is notified when the check box is selected or deselected.

    婷婷总结:Button是需要action listener的,而check boxes是需要item listener.监听程序是不一样的。addItemListener().

    Below is the code from ButtonDemo.java that creates the buttons in the previous example and reacts to button clicks. The bold code is the code that would remain if the buttons had no images.

    //In initialization code:
        ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
        ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
        ImageIcon rightButtonIcon = createImageIcon("images/left.gif");
    
        b1 = new JButton("Disable middle button", leftButtonIcon);
        b1.setVerticalTextPosition(AbstractButton.CENTER);
        b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
        b1.setMnemonic(KeyEvent.VK_D);
        b1.setActionCommand("disable");
    
        b2 = new JButton("Middle button", middleButtonIcon);
        b2.setVerticalTextPosition(AbstractButton.BOTTOM);
        b2.setHorizontalTextPosition(AbstractButton.CENTER);
        b2.setMnemonic(KeyEvent.VK_M);
    
        b3 = new JButton("Enable middle button", rightButtonIcon);
        //Use the default text position of CENTER, TRAILING (RIGHT).
        b3.setMnemonic(KeyEvent.VK_E);
        b3.setActionCommand("enable");
        b3.setEnabled(false);
    
        //Listen for actions on buttons 1 and 3.
        b1.addActionListener(this);
        b3.addActionListener(this);
    
        b1.setToolTipText("Click this button to disable "
                          + "the middle button.");
        b2.setToolTipText("This middle button does nothing "
                          + "when you click it.");
        b3.setToolTipText("Click this button to enable the "
                          + "middle button.");
        ...
    }
    
    public void actionPerformed(ActionEvent e) {
        if ("disable".equals(e.getActionCommand())) {
            b2.setEnabled(false);
            b1.setEnabled(false);
            b3.setEnabled(true);
        } else {
            b2.setEnabled(true);
            b1.setEnabled(true);
            b3.setEnabled(false);
        }
    } 
    
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = ButtonDemo.class.getResource(path);
        ...//error handling omitted for clarity...
        return new ImageIcon(imgURL);
    }
    

    How to Use JButton Features

    Ordinary buttons — JButton objects — have just a bit more functionality than the AbstractButton class provides: You can make a JButton be the default button.

  • 相关阅读:
    浅谈ES6
    iframe的应用
    vue时时监听input输入框中 输入内容 写法
    点击模态框滑动出来 抽屉
    this.$router 和this.$route 的区别
    iview框架 两侧弹框 出现第二层弹框 一闪而过的问题
    input框中的必填项之取消当前input框为必填项
    v-model 的修饰符
    单页面开发和多页面开发的优缺点
    原型继承+借用构造函数继承 的一些理解
  • 原文地址:https://www.cnblogs.com/meihao1989/p/3183551.html
Copyright © 2011-2022 走看看