From 04a02e77da0985892f439a4e318e718403bb4c52 Mon Sep 17 00:00:00 2001 From: mengao <283296419@qq.com> Date: Fri, 15 Sep 2017 10:39:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E6=9F=A5=E6=89=BE=E5=9B=BE?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/fr/design/constants/UIConstants.java | 1 + .../design/gui/itextfield/UISearchTextField.java | 2 +- .../com/fr/design/images/data/black_search.png | Bin 0 -> 374 bytes 3 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 designer_base/src/com/fr/design/images/data/black_search.png diff --git a/designer_base/src/com/fr/design/constants/UIConstants.java b/designer_base/src/com/fr/design/constants/UIConstants.java index 00c2b72dc6..4d5a5cea5b 100644 --- a/designer_base/src/com/fr/design/constants/UIConstants.java +++ b/designer_base/src/com/fr/design/constants/UIConstants.java @@ -161,6 +161,7 @@ public interface UIConstants { public static final Icon DELETE_ICON = BaseUtils.readIcon("com/fr/design/images/m_file/close.png"); public static final Icon EDIT_ICON = BaseUtils.readIcon("com/fr/design/images/m_file/edit.png"); public static final Icon SEARCH_ICON = BaseUtils.readIcon("/com/fr/design/images/data/search.png"); + public static final Icon BLACK_SEARCH_ICON = BaseUtils.readIcon("/com/fr/design/images/data/black_search.png"); public static final Icon CLEAR_ICON = BaseUtils.readIcon("/com/fr/design/images/data/source/delete.png"); public static final Icon LIST_EDIT_ICON = BaseUtils.readIcon("/com/fr/design/images/control/edit.png"); public static final Icon LIST_EDIT_WHITE_ICON = BaseUtils.readIcon("/com/fr/design/images/control/edit_white.png"); diff --git a/designer_base/src/com/fr/design/gui/itextfield/UISearchTextField.java b/designer_base/src/com/fr/design/gui/itextfield/UISearchTextField.java index 5da10afad0..85189c8c30 100644 --- a/designer_base/src/com/fr/design/gui/itextfield/UISearchTextField.java +++ b/designer_base/src/com/fr/design/gui/itextfield/UISearchTextField.java @@ -1 +1 @@ -package com.fr.design.gui.itextfield; import com.fr.design.constants.UIConstants; import com.fr.design.gui.ilable.UILabel; import com.fr.general.Inter; import javax.swing.*; import javax.swing.text.Document; import java.awt.*; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.geom.RoundRectangle2D; /** * Created by IntelliJ IDEA. * Author : Richer * Version: 7.0.3 * Date: 13-1-4 * Time: 上午11:07 * 带图标的文本框,可以用作搜索等用途 */ public class UISearchTextField extends UITextField { private UILabel iconLabel = new UILabel(UIConstants.SEARCH_ICON); private UILabel clearLabel = new UILabel(UIConstants.CLEAR_ICON); private UILabel infoLabel = new UILabel(Inter.getLocText("Search")); private Dimension iconSize; private Dimension infoSize; private int iconPosition = SwingConstants.LEFT; private boolean showClearIcon = true; private CellRendererPane cellRendererPane = new CellRendererPane(); public UISearchTextField() { initTextField(); } public UISearchTextField(String text) { super(text); initTextField(); } public UISearchTextField(int columns) { super(columns); initTextField(); } public UISearchTextField(String text, int columns) { super(text, columns); initTextField(); } public UISearchTextField(Document doc, String text, int columns) { super(doc, text, columns); initTextField(); } private void initTextField() { iconLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); iconLabel.setToolTipText(Inter.getLocText("Search")); clearLabel.setToolTipText(Inter.getLocText("Clear")); clearLabel.setOpaque(false); iconSize = iconLabel.getPreferredSize(); infoSize = infoLabel.getPreferredSize(); infoLabel.setEnabled(false); this.addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent e) { setMouseComponent(null); } public void mouseClicked(MouseEvent e) { if (mouseCom == clearLabel) { setText(""); } else if (mouseCom == iconLabel) { handleIconClick(); } } }); this.addMouseMotionListener(new MouseMotionListener() { public void mouseMoved(MouseEvent e) { JComponent com = getComponentAtPoint(e.getPoint()); setMouseComponent(com); } public void mouseDragged(MouseEvent e) { } }); this.addFocusListener(new FocusListener() { public void focusLost(FocusEvent e) { repaint(); } public void focusGained(FocusEvent e) { repaint(); } }); } private void handleIconClick() { } public String getToolTipText(MouseEvent event) { event.getPoint(); if (mouseCom == null) { return null; } if (mouseCom == this) { return super.getToolTipText(event); } return mouseCom.getToolTipText(event); } private int iconGap = 3; public Insets getInsets() { Insets insets = super.getInsets(); if (iconPosition == SwingConstants.LEFT) { insets.left += iconSize.width + iconGap + 2; if (this.getText() != null && this.getText().length() != 0 && showClearIcon) { insets.right += iconSize.width + iconGap + 2; } else { insets.right += iconGap; } } else { insets.left += iconGap; if (this.getText() != null && this.getText().length() != 0 && showClearIcon) { insets.right += iconSize.width + iconGap + 2; } else { insets.right += iconGap; } } return insets; } private JComponent getComponentAtPoint(Point point) { int x = (int) point.getX(); int y = (int) point.getY(); if (!contains(x, y)) { return null; } if (iconPosition == SwingConstants.LEFT) { if (x < iconSize.width + iconGap + 2) { return iconLabel; } if (getText() != null && getText().length() != 0 && showClearIcon) { if (x > getWidth() - iconSize.width - iconGap) { return clearLabel; } } } else { if (this.getText() == null || this.getText().length() == 0) { if (x > getWidth() - iconSize.width - iconGap - 2) { return iconLabel; } } if (getText() != null && getText().length() != 0 && showClearIcon) { if (x > getWidth() - iconSize.width - iconGap - 2) { return clearLabel; } } } return this; } private JComponent mouseCom; private void setMouseComponent(JComponent com) { if (mouseCom == com) { return; } if (mouseCom != null && mouseCom instanceof UILabel) { mouseCom.setBorder(null); } mouseCom = com; if (mouseCom != null && mouseCom instanceof UILabel) { mouseCom.setBorder(BorderFactory.createLineBorder(Color.ORANGE, 1)); } this.repaint(); if (mouseCom == null || mouseCom == clearLabel) { setCursor(Cursor.getDefaultCursor()); return; } if (mouseCom == this) { setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); return; } if (mouseCom == iconLabel) { setCursor(iconLabel.getCursor()); } } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; int y = (int) (this.getHeight() / 2.0 - iconSize.getHeight() / 2); if (iconPosition == SwingConstants.LEFT) { cellRendererPane.paintComponent(g2d, iconLabel, this, iconGap, y, iconSize.width, iconSize.height); if (this.getText() != null && this.getText().length() != 0 && showClearIcon) { cellRendererPane.paintComponent(g2d, clearLabel, this, this.getWidth() - iconSize.width - iconGap, y, iconSize.width, iconSize.height); } } else { if (this.getText() == null || this.getText().length() == 0) { cellRendererPane.paintComponent(g2d, iconLabel, this, this.getWidth() - iconSize.width - iconGap, y, iconSize.width, iconSize.height); } else { if (showClearIcon) { cellRendererPane.paintComponent(g2d, clearLabel, this, this.getWidth() - iconSize.width - iconGap, y, iconSize.width, iconSize.height); } } } if (!this.isFocusOwner()) { if (this.getText() == null || this.getText().length() == 0) { int x = iconGap + 2; if (iconPosition == SwingConstants.LEFT) { x = iconSize.width + iconGap + 2; } y = (int) (this.getHeight() / 2.0 - infoSize.getHeight() / 2); cellRendererPane.paintComponent(g2d, infoLabel, this, x, y, infoSize.width, infoSize.height); } } } public Dimension getPreferredSize() { Insets s = super.getInsets(); Insets t = this.getInsets(); Dimension preferredSize = super.getPreferredSize(); preferredSize.setSize(preferredSize.width - (t.left + t.right - s.left - s.right), preferredSize.height + 2); return preferredSize; } protected void paintBorder(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (this.isFocusOwner()) { g2d.setStroke(new BasicStroke(1.5f)); } else { g2d.setStroke(new BasicStroke(1f)); } RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, this.getWidth() - 2, this.getHeight() - 2, 4, 4); g2d.setColor(UIConstants.SHADOW_GREY); g2d.draw(rect); } public int getIconPosition() { return iconPosition; } public void setIconPosition(int iconPosition) { this.iconPosition = iconPosition; this.invalidate(); this.repaint(); } public boolean isShowClearIcon() { return showClearIcon; } public void setShowClearIcon(boolean showClearIcon) { this.showClearIcon = showClearIcon; this.repaint(); } public static void main(String[] args) { JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT)); UISearchTextField comp = new UISearchTextField(20); p.add(comp); comp = new UISearchTextField(20); comp.setShowClearIcon(false); p.add(comp); comp = new UISearchTextField(20); comp.setIconPosition(SwingConstants.RIGHT); p.add(comp); comp = new UISearchTextField(20); comp.setShowClearIcon(false); comp.setIconPosition(SwingConstants.RIGHT); p.add(comp); p.add(new JTextField(20)); JFrame frame = new JFrame(); frame.setTitle("UITextField"); frame.setContentPane(p); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } } \ No newline at end of file +package com.fr.design.gui.itextfield; import com.fr.design.constants.UIConstants; import com.fr.design.gui.ilable.UILabel; import com.fr.general.Inter; import javax.swing.*; import javax.swing.text.Document; import java.awt.*; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.geom.RoundRectangle2D; /** * Created by IntelliJ IDEA. * Author : Richer * Version: 7.0.3 * Date: 13-1-4 * Time: 上午11:07 * 带图标的文本框,可以用作搜索等用途 */ public class UISearchTextField extends UITextField { private UILabel iconLabel = new UILabel(UIConstants.BLACK_SEARCH_ICON); private UILabel clearLabel = new UILabel(UIConstants.CLEAR_ICON); private UILabel infoLabel = new UILabel(Inter.getLocText("Search")); private Dimension iconSize; private Dimension infoSize; private int iconPosition = SwingConstants.LEFT; private boolean showClearIcon = true; private CellRendererPane cellRendererPane = new CellRendererPane(); public UISearchTextField() { initTextField(); } public UISearchTextField(String text) { super(text); initTextField(); } public UISearchTextField(int columns) { super(columns); initTextField(); } public UISearchTextField(String text, int columns) { super(text, columns); initTextField(); } public UISearchTextField(Document doc, String text, int columns) { super(doc, text, columns); initTextField(); } private void initTextField() { iconLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); iconLabel.setToolTipText(Inter.getLocText("Search")); clearLabel.setToolTipText(Inter.getLocText("Clear")); clearLabel.setOpaque(false); iconSize = iconLabel.getPreferredSize(); infoSize = infoLabel.getPreferredSize(); infoLabel.setEnabled(false); this.addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent e) { setMouseComponent(null); } public void mouseClicked(MouseEvent e) { if (mouseCom == clearLabel) { setText(""); } else if (mouseCom == iconLabel) { handleIconClick(); } } }); this.addMouseMotionListener(new MouseMotionListener() { public void mouseMoved(MouseEvent e) { JComponent com = getComponentAtPoint(e.getPoint()); setMouseComponent(com); } public void mouseDragged(MouseEvent e) { } }); this.addFocusListener(new FocusListener() { public void focusLost(FocusEvent e) { repaint(); } public void focusGained(FocusEvent e) { repaint(); } }); } private void handleIconClick() { } public String getToolTipText(MouseEvent event) { event.getPoint(); if (mouseCom == null) { return null; } if (mouseCom == this) { return super.getToolTipText(event); } return mouseCom.getToolTipText(event); } private int iconGap = 3; public Insets getInsets() { Insets insets = super.getInsets(); if (iconPosition == SwingConstants.LEFT) { insets.left += iconSize.width + iconGap + 2; if (this.getText() != null && this.getText().length() != 0 && showClearIcon) { insets.right += iconSize.width + iconGap + 2; } else { insets.right += iconGap; } } else { insets.left += iconGap; if (this.getText() != null && this.getText().length() != 0 && showClearIcon) { insets.right += iconSize.width + iconGap + 2; } else { insets.right += iconGap; } } return insets; } private JComponent getComponentAtPoint(Point point) { int x = (int) point.getX(); int y = (int) point.getY(); if (!contains(x, y)) { return null; } if (iconPosition == SwingConstants.LEFT) { if (x < iconSize.width + iconGap + 2) { return iconLabel; } if (getText() != null && getText().length() != 0 && showClearIcon) { if (x > getWidth() - iconSize.width - iconGap) { return clearLabel; } } } else { if (this.getText() == null || this.getText().length() == 0) { if (x > getWidth() - iconSize.width - iconGap - 2) { return iconLabel; } } if (getText() != null && getText().length() != 0 && showClearIcon) { if (x > getWidth() - iconSize.width - iconGap - 2) { return clearLabel; } } } return this; } private JComponent mouseCom; private void setMouseComponent(JComponent com) { if (mouseCom == com) { return; } if (mouseCom != null && mouseCom instanceof UILabel) { mouseCom.setBorder(null); } mouseCom = com; if (mouseCom != null && mouseCom instanceof UILabel) { mouseCom.setBorder(BorderFactory.createLineBorder(Color.ORANGE, 1)); } this.repaint(); if (mouseCom == null || mouseCom == clearLabel) { setCursor(Cursor.getDefaultCursor()); return; } if (mouseCom == this) { setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); return; } if (mouseCom == iconLabel) { setCursor(iconLabel.getCursor()); } } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; int y = (int) (this.getHeight() / 2.0 - iconSize.getHeight() / 2); if (iconPosition == SwingConstants.LEFT) { cellRendererPane.paintComponent(g2d, iconLabel, this, iconGap, y, iconSize.width, iconSize.height); if (this.getText() != null && this.getText().length() != 0 && showClearIcon) { cellRendererPane.paintComponent(g2d, clearLabel, this, this.getWidth() - iconSize.width - iconGap, y, iconSize.width, iconSize.height); } } else { if (this.getText() == null || this.getText().length() == 0) { cellRendererPane.paintComponent(g2d, iconLabel, this, this.getWidth() - iconSize.width - iconGap, y, iconSize.width, iconSize.height); } else { if (showClearIcon) { cellRendererPane.paintComponent(g2d, clearLabel, this, this.getWidth() - iconSize.width - iconGap, y, iconSize.width, iconSize.height); } } } if (!this.isFocusOwner()) { if (this.getText() == null || this.getText().length() == 0) { int x = iconGap + 2; if (iconPosition == SwingConstants.LEFT) { x = iconSize.width + iconGap + 2; } y = (int) (this.getHeight() / 2.0 - infoSize.getHeight() / 2); cellRendererPane.paintComponent(g2d, infoLabel, this, x, y, infoSize.width, infoSize.height); } } } public Dimension getPreferredSize() { Insets s = super.getInsets(); Insets t = this.getInsets(); Dimension preferredSize = super.getPreferredSize(); preferredSize.setSize(preferredSize.width - (t.left + t.right - s.left - s.right), preferredSize.height + 2); return preferredSize; } protected void paintBorder(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (this.isFocusOwner()) { g2d.setStroke(new BasicStroke(1.5f)); } else { g2d.setStroke(new BasicStroke(1f)); } RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, this.getWidth() - 2, this.getHeight() - 2, 4, 4); g2d.setColor(UIConstants.SHADOW_GREY); g2d.draw(rect); } public int getIconPosition() { return iconPosition; } public void setIconPosition(int iconPosition) { this.iconPosition = iconPosition; this.invalidate(); this.repaint(); } public boolean isShowClearIcon() { return showClearIcon; } public void setShowClearIcon(boolean showClearIcon) { this.showClearIcon = showClearIcon; this.repaint(); } public static void main(String[] args) { JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT)); UISearchTextField comp = new UISearchTextField(20); p.add(comp); comp = new UISearchTextField(20); comp.setShowClearIcon(false); p.add(comp); comp = new UISearchTextField(20); comp.setIconPosition(SwingConstants.RIGHT); p.add(comp); comp = new UISearchTextField(20); comp.setShowClearIcon(false); comp.setIconPosition(SwingConstants.RIGHT); p.add(comp); p.add(new JTextField(20)); JFrame frame = new JFrame(); frame.setTitle("UITextField"); frame.setContentPane(p); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } } \ No newline at end of file diff --git a/designer_base/src/com/fr/design/images/data/black_search.png b/designer_base/src/com/fr/design/images/data/black_search.png new file mode 100644 index 0000000000000000000000000000000000000000..b9898952b5915bc08910f63662716feb03fa2eee GIT binary patch literal 374 zcmV-+0g3*JP)Px$FiAu~R5%f(Qo%~YKoH&8V5OeKWAhb~Z6Ju?cSz5k^&p7;K+!|{5kkMgo9H1D z1O7t~DPBBzv677YD7y>;mX