码迷,mamicode.com
首页 > Windows程序 > 详细

开发一个Swing功能时的一点总结

时间:2015-06-26 23:33:56      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

 

对JTextField进行效验,有两个途径:
(1)是使用javax.swing.InputVerifier在获取焦点时进行校验
(2)在点击“确定”按钮的监听事件中对控件的值进行校验

鉴于涉及的业务比较多,代码结构已经确定,如果在“确定”按钮的监听事件中进行效验,需要增加一个步骤,并且并不是所有的业务都需要这个效验,
就倾向于使用javax.swing.InputVerifier进行,这样做有两个好处,(1)分离业务逻辑与前端 (2)代码更优雅

javax.swing.InputVerifier用的不多,用了之后发现这个控件的特性和以前UE的不同:
“校验器并非问题很安全。如果点击了某个按钮,而这个按钮在无效构件再次获得焦点之前通知了它的动作监听器,那么这个动作监听器就会未通过校验的构件中得到一个无效的结果。这种行为的原因在于:用户可能希望点击Cancel按钮,而无需订正无效输入

还有个问题,如果控件JTextField在java.awt.Container中没有获得焦点,则相关校验就不起作用

(Abstract)java.awt.FocusTraversalPolicy调整焦点顺序(没有生效)
或在Container上增加监听,在Container执行setVisile(true)后
addWindowFocusListener(new WindowAdapter() {
    public void windowGainedFocus(WindowEvent e) {
        textField.requestFocusInWindow();
    }
});

进行这样的处理后,在win7上程序出现一些意外的异常情况,并且由于增加监听,也增加了程序的处理逻辑,进行了结构的调整

鉴于以上的情况,最终在“确定”按钮的监听中的处理流程中增加constraintCheck的逻辑


Tutorials上的一个Sample:

package misc;

import java.awt.*;
import java.awt.event.*;

import java.util.Vector;

import javax.swing.*;

/*
 * FocusTraversalDemo.java requires no other files.
 */
public class FocusTraversalDemo extends JPanel
                        implements ActionListener {

    static JFrame frame;
    JLabel label;
    JCheckBox togglePolicy;
    static MyOwnFocusTraversalPolicy newPolicy;

    public FocusTraversalDemo() {
        super(new BorderLayout());

        JTextField tf1 = new JTextField("Field 1");
        JTextField tf2 = new JTextField("A Bigger Field 2");
        JTextField tf3 = new JTextField("Field 3");
        JTextField tf4 = new JTextField("A Bigger Field 4");
        JTextField tf5 = new JTextField("Field 5");
        JTextField tf6 = new JTextField("A Bigger Field 6");
        JTable table = new JTable(4,3);
        togglePolicy = new JCheckBox("Custom FocusTraversalPolicy");
        togglePolicy.setActionCommand("toggle");
        togglePolicy.addActionListener(this);
        togglePolicy.setFocusable(false);  //Remove it from the focus cycle.
        //Note that HTML is allowed and will break this run of text
        //across two lines.
        label = new JLabel("<html>Use Tab (or Shift-Tab) to navigate from component to component.<p>Control-Tab (or Control-Shift-Tab) allows you to break out of the JTable.</html>");

        JPanel leftTextPanel = new JPanel(new GridLayout(3,2));
        leftTextPanel.add(tf1, BorderLayout.PAGE_START);
        leftTextPanel.add(tf3, BorderLayout.CENTER);
        leftTextPanel.add(tf5, BorderLayout.PAGE_END);
        leftTextPanel.setBorder(BorderFactory.createEmptyBorder(0,0,5,5));
        JPanel rightTextPanel = new JPanel(new GridLayout(3,2));
        rightTextPanel.add(tf2, BorderLayout.PAGE_START);
        rightTextPanel.add(tf4, BorderLayout.CENTER);
        rightTextPanel.add(tf6, BorderLayout.PAGE_END);
        rightTextPanel.setBorder(BorderFactory.createEmptyBorder(0,0,5,5));
        JPanel tablePanel = new JPanel(new GridLayout(0,1));
        tablePanel.add(table, BorderLayout.CENTER);
        tablePanel.setBorder(BorderFactory.createEtchedBorder());
        JPanel bottomPanel = new JPanel(new GridLayout(2,1));
        bottomPanel.add(togglePolicy, BorderLayout.PAGE_START);
        bottomPanel.add(label, BorderLayout.PAGE_END);

        add(leftTextPanel, BorderLayout.LINE_START);
        add(rightTextPanel, BorderLayout.CENTER);
        add(tablePanel, BorderLayout.LINE_END);
        add(bottomPanel, BorderLayout.PAGE_END);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        Vector<Component> order = new Vector<Component>(7);
        order.add(tf1);
        order.add(tf2);
        order.add(tf3);
        order.add(tf4);
        order.add(tf5);
        order.add(tf6);
        order.add(table);
        newPolicy = new MyOwnFocusTraversalPolicy(order);
    }

    //Turn the custom focus traversal policy on/off,
    //according to the checkbox
    public void actionPerformed(ActionEvent e) {
        if ("toggle".equals(e.getActionCommand())) {
            frame.setFocusTraversalPolicy(togglePolicy.isSelected() ?
                    newPolicy : null);
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        frame = new JFrame("FocusTraversalDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new FocusTraversalDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal‘s use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        
    //Schedule a job for the event-dispatching thread:
        //creating and showing this application‘s GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static class MyOwnFocusTraversalPolicy
                  extends FocusTraversalPolicy
    {
        Vector<Component> order;

        public MyOwnFocusTraversalPolicy(Vector<Component> order) {
            this.order = new Vector<Component>(order.size());
            this.order.addAll(order);
        }
        public Component getComponentAfter(Container focusCycleRoot,
                                           Component aComponent)
        {
            int idx = (order.indexOf(aComponent) + 1) % order.size();
            return order.get(idx);
        }

        public Component getComponentBefore(Container focusCycleRoot,
                                            Component aComponent)
        {
            int idx = order.indexOf(aComponent) - 1;
            if (idx < 0) {
                idx = order.size() - 1;
            }
            return order.get(idx);
        }

        public Component getDefaultComponent(Container focusCycleRoot) {
            return order.get(0);
        }

        public Component getLastComponent(Container focusCycleRoot) {
            return order.lastElement();
        }

        public Component getFirstComponent(Container focusCycleRoot) {
            return order.get(0);
        }
    }
}

 



public abstract class InputVerifier extends Object
此类的用途是通过带文本字段的 GUI 帮助客户端支持流畅的焦点导航。在允许用户导航到文本字段以外之前,这类 GUI 常常需要确保用户输入的文本是有效的(例如,文本具有正确的格式)。为做到这一点,客户端要使用 JComponent 的 setInputVerifier 方法创建 InputVerifier 的子类,并将其子类的实例附加到想要验证其输入的 JComponent 中。在将焦点转移到另一个请求它的 Swing 组件之前,要调用输入验证器的 shouldYieldFocus 方法。只在该方法返回 true 时才转移焦点。

以下示例有两个文本字段,其中第一个字段期望用户输入字符串 "pass"。如果在第一个文本字段中输入该字符串,那么用户可以通过在第二个文本字段上单击或按下 TAB 前进到第二个文本字段。不过,如果将其他字符串输入到第一个文本字段中,则用户无法将焦点转移到第二个文本字段。

import java.awt.*;
 import java.util.*;
 import java.awt.event.*;
 import javax.swing.*;
 
 // This program demonstrates the use of the Swing InputVerifier class.
 // It creates two text fields; the first of the text fields expects the
 // string "pass" as input, and will allow focus to advance out of it
 // only after that string is typed in by the user.

 public class VerifierTest extends JFrame {
     public VerifierTest() {
         JTextField tf1 = new JTextField ("Type \"pass\" here");
           getContentPane().add (tf1, BorderLayout.NORTH);
           tf1.setInputVerifier(new PassVerifier());
 
           JTextField tf2 = new JTextField ("TextField2");
           getContentPane().add (tf2, BorderLayout.SOUTH);
 
           WindowListener l = new WindowAdapter() {
               public void windowClosing(WindowEvent e) { 
                   System.exit(0); 
               }
           };
           addWindowListener(l);
     }
 
     class PassVerifier extends InputVerifier {
         public boolean verify(JComponent input) {
               JTextField tf = (JTextField) input;
               return "pass".equals(tf.getText());
         }
     }
 
     public static void main(String[] args) {
         Frame f = new VerifierTest();
           f.pack();
           f.setVisible(true);
     }
 }

 







 

开发一个Swing功能时的一点总结

标签:

原文地址:http://www.cnblogs.com/softidea/p/4603364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!