开发一个Swing功能时的一点总结
对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); } } }
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70489.html